[英]Using AWS SDK (S3.putObject) to upload a Readable stream to S3 (node.js)
My goal is to upload a Readable
stream to S3.我的目标是将
Readable
的 stream 上传到 S3。
The problem is that AWS api seems to accept only a ReadStream
as a stream argument.问题是 AWS api 似乎只接受
ReadStream
作为 stream 参数。
For instance, the following snippet works just fine:例如,以下代码片段工作正常:
const readStream = fs.createReadStream("./file.txt") // a ReadStream
await s3.putObject({
Bucket: this.bucket,
Key: this.key,
Body: readStream,
ACL: "bucket-owner-full-control"
}
Problem starts when I try to do the same with a Readable
( ReadStream
extends stream.Readable
).当我尝试对
Readable
( ReadStream
extends stream.Readable
)执行相同操作时,问题就开始了。
The following snippet fails以下代码段失败
const { Readable } = require("stream")
const readable = Readable.from("data data data data") // a Readable
await s3.putObject({
Bucket: this.bucket,
Key: this.key,
Body: readable,
ACL: "bucket-owner-full-control"
}
the error I get from AWS sdk is: NotImplemented: A header you provided implies functionality that is not implemented
我从 AWS sdk 得到的错误是:
NotImplemented: A header you provided implies functionality that is not implemented
*** Note that I prefer a Readable
rather than a ReadStream
since I'd like to allow passing streams that are not necessarily originated from a file - an in-memory string for instance. *** 请注意,我更喜欢
Readable
而不是ReadStream
,因为我希望允许传递不一定源自文件的流 - 例如内存中的字符串。 So a possible solution could be converting a Readable
to a Readstream
to work with the SDK.因此,一种可能的解决方案是将
Readable
转换为Readstream
以与 SDK 一起使用。
Any help will be much appreciated!任何帮助都感激不尽!
Given the Readable
is no longer a file that has metadata such as MIME type and content length you'd need to update your putObject
to include those values:鉴于
Readable
不再是具有元数据(例如 MIME 类型和内容长度)的文件,您需要更新putObject
以包含这些值:
const { Readable } = require("stream")
const readable = Readable.from("data data data data") // a Readable
await s3.putObject({
Bucket: this.bucket,
Key: this.key,
Body: readable,
ACL: "bucket-owner-full-control",
ContentType: "text/plain",
ContentLength: 42 // calculate length of buffer
}
Hopefully that helps!希望这有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.