[英]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"
}
當我嘗試對Readable
( ReadStream
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.