使用nodejs SDK v3上传流到Amazon s3



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

PutObjectRequest作为文档在这里应该接受一个ReadableStream,但每次我收到错误NotImplemented: A header you provided implies functionality that is not implemented-报头是Transfer-Encoding

这篇文章建议我提供一个空的主体,但它应该接受ReadableStream (Archiver提供一个Transform流)。

我已经创建了一个最小的(非)工作示例。我也试过用回调代替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()

问题是Archiver库使用了Node 10的readable-stream的静态版本,这与AWS SDK版本不兼容。

当它通过管道连接到Archiver实例时,它失败了。

最新更新