Javascript 保存PDF响应的原始响应/二进制数据
我在node中得到一个响应,以获取一个PDF,我希望保存二进制数据并将其上载到S3,然后出于历史目的,我可以随心所欲地提取它。现在我在尝试提取PDF原始数据/二进制数据时遇到了问题,因此我可以将其保存为PDF并上传,我想我离此越来越近了。这是我得到的回应。身体Javascript 保存PDF响应的原始响应/二进制数据,javascript,node.js,pdf,amazon-s3,Javascript,Node.js,Pdf,Amazon S3,我在node中得到一个响应,以获取一个PDF,我希望保存二进制数据并将其上载到S3,然后出于历史目的,我可以随心所欲地提取它。现在我在尝试提取PDF原始数据/二进制数据时遇到了问题,因此我可以将其保存为PDF并上传,我想我离此越来越近了。这是我得到的回应。身体 PassThrough { _readableState: ReadableState { objectMode: false, highWaterMark: 16384, buffer: BufferList
PassThrough {
_readableState: ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: null,
pipesCount: 0,
flowing: null,
ended: false,
endEmitted: false,
reading: false,
sync: false,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
emitClose: true,
autoDestroy: false,
destroyed: false,
defaultEncoding: 'utf8',
awaitDrainWriters: null,
multiAwaitDrain: false,
readingMore: false,
decoder: null,
encoding: null,
[Symbol(kPaused)]: null
},
readable: true,
_events: [Object: null prototype] {
prefinish: [Function: prefinish],
unpipe: [Function: onunpipe],
error: [ [Function: onerror], [Function] ],
close: [Function: bound onceWrapper] { listener: [Function: onclose] },
finish: [Function: bound onceWrapper] { listener: [Function: onfinish] }
},
_eventsCount: 5,
_maxListeners: undefined,
_writableState: WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: true,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: true,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
afterWriteTickInfo: null,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
emitClose: true,
autoDestroy: false,
bufferedRequestCount: 0,
corkedRequestsFree: {
next: null,
entry: null,
finish: [Function: bound onCorkedFinish]
}
},
writable: true,
allowHalfOpen: true,
_transformState: {
afterTransform: [Function: bound afterTransform],
needTransform: false,
transforming: false,
writecb: null,
writechunk: null,
writeencoding: null
},
[Symbol(kCapture)]: false
}
这是我的代码:
platform.on(platform.events.loginSuccess, async function(e) {
let resp = await platform.get('/restapi/v1.0/account/~/extension/'+accountId+'/message-store', {
messageType: ['Fax'],
dateFrom: ['2021-05-01'],
dateTo: ['2021-05-16']
})
let jsonObj = await resp.json()
let attachId = jsonObj.records[0].id
let getMessageContent = await platform.get('/restapi/v1.0/account/~/extension/'+accountId+'/message-store/'+attachId+'/content/'+attachId, {
contentDisposition: ['Inline']
})
我知道我得到的回复是正确的,因为如果我在postman中运行该URL并使用预览选项,我可以看到pdf(参见屏幕截图)。我错过了什么
这对我来说很合适。response.body是一个包含二进制数据的可读文件,我猜Postman会自动读取此流并显示结果 好消息是,这种类型的流正是S3在上传文件时作为请求主体所需要的,这意味着您无需将其保存在任何地方—您只需将其直接传递给S3即可。根据您的实现,您的代码可能如下所示:
const result=wait s3Client.upload({
水桶:“我的水桶”,
关键字:“myfilename.pdf”,
Body:response.Body,
}).promise()代码>