【发布时间】: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