为 PDF 响应保存原始 response/binary 数据

Save raw response/binary data for a PDF response

我正在节点中返回一个响应以获取我想保存二进制数据并将其上传到 S3 的 PDF,然后我可以出于历史目的随意提取它。现在我在尝试提取 PDF 原始 data/binary 数据以便我可以将其另存为 PDF 并上传时遇到问题,我想我越来越接近了。这是我得到的 response.body。

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']
    })

我知道我得到的回复是正确的,因为如果我在邮递员中 运行 URL 并使用预览选项,我可以看到 pdf(见屏幕截图)。我错过了什么?

我觉得差不多。 response.body 是一个包含二进制数据的可读 node.js stream,我的猜测是 Postman 会自动读取这个流并显示结果。

好消息是,这种类型的流正是 S3 在上传文件时想要作为请求主体的,这意味着您不需要将其保存在任何地方 - 您可以直接将其传递给 S3。根据您的实施,您的代码可能如下所示:

    const result = await s3Client.upload({
        Bucket: 'MY_S3_BUCKET',
        Key: 'myfilename.pdf',
        Body: response.body,
    }).promise();

其中 response.body 是您在上面记录的流。这应该将 pdf 文件放在 S3 中以供下载。

但是如果你坚持保存文件,你也可以像这样将流传递给fs.writeFileSync方法:

fs.writeFileSync('my-pdf.pdf', response.body);