【问题标题】:Upload stream to Amazon s3 using nodejs SDK v3使用 nodejs SDK v3 将流上传到 Amazon s3
【发布时间】:2022-02-24 21:42:46
【问题描述】:

我想使用 AWS SDK v3 将流上传到 s3。

documented here 的 PutObjectRequest 应该接受 ReadableStream,但每次我收到错误 NotImplemented: A header you provided implies functionality that is not implemented - 报告的标头是 Transfer-Encoding

This SO article 建议我提供一个空主体,但它应该接受 ReadableStream(Archiver 提供一个转换流)。

我创建了一个最小(非)工作示例。我还尝试将client.send() 与回调一起使用,但响应相同。

import fs from 'fs'
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3'
import archiver from 'archiver'

async function run() {
  const archive = archiver('zip')

  const client = new S3Client({ region: process.env.AWS_REGION })

  // create a stream destination for aws
  // also pipe to aws
  try {
    const command = new PutObjectCommand({
      Bucket: process.env.AWS_DEFAULT_BUCKET,
      Key: 'testupload.zip',
      // set the content type as we're streaming it
      ContentType: 'application/zip',
      // body should take a readable stream
      Body: archive,
    })
    client.send(command, (err, data) => {
      console.log(data)
    })
  } catch (err) {
    console.error('Error uploading package to S3')
    console.error(err)
    res.status(501).json({ message: 'Error configuring upload to s3' })
  }

  archive.file('/fixtures/harambe.jpg', { name: 'harambe.jpg' })

  archive.finalize()
}

run()

【问题讨论】:

  • 您需要对流进行管道传输 - 您确定 const archive = archiver('zip') 为您提供了可读流吗?
  • @ErmiyaEskandary 根据 Archiver 库,它使用 readable-stream 库,它应该是节点 10 的“剪切”。也许问题出在这个底层库上?我将尝试使用普通可读流进行重现。
  • @ErmiyaEskandary 似乎确实是问题所在。感谢您对此进行分类——非常有帮助!我会和 Archiver 的开发者一起筹集资金。
  • :) 乔希别担心
  • @JoshKopecek 这似乎得到了回答,但我不确定答案是您需要通过管道传输流还是归档器('zip')没有为您提供可用的流。提供正确的答案可能对其他人有所帮助。

标签: node.js amazon-web-services amazon-s3


【解决方案1】:

问题在于 Archiver 库使用来自 Node 10 的静态版本 readable-stream,它与 AWS 开发工具包版本不兼容。

当它通过管道传输到 Archiver 实例时,它会失败。

【讨论】:

    猜你喜欢
    • 2021-12-21
    • 2015-03-17
    • 2018-09-07
    • 2016-01-30
    • 2020-10-02
    • 1970-01-01
    • 2015-04-30
    • 2012-02-14
    • 1970-01-01
    相关资源
    最近更新 更多