繁体   English   中英

使用 AWS SDK (S3.putObject) 上传一个 Readable stream 到 S3 (node.js)

[英]Using AWS SDK (S3.putObject) to upload a Readable stream to S3 (node.js)

我的目标是将Readable的 stream 上传到 S3。

问题是 AWS api 似乎只接受ReadStream作为 stream 参数。

例如,以下代码片段工作正常:

const readStream = fs.createReadStream("./file.txt") // a ReadStream

await s3.putObject({
    Bucket: this.bucket,
    Key: this.key,
    Body: readStream,
    ACL: "bucket-owner-full-control"
}

当我尝试对ReadableReadStream extends stream.Readable )执行相同操作时,问题就开始了。

以下代码段失败

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"
}

我从 AWS sdk 得到的错误是: NotImplemented: A header you provided implies functionality that is not implemented

*** 请注意,我更喜欢Readable而不是ReadStream ,因为我希望允许传递不一定源自文件的流 - 例如内存中的字符串。 因此,一种可能的解决方案是将Readable转换为Readstream以与 SDK 一起使用。

任何帮助都感激不尽!

鉴于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
}

希望这有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM