我的目標是將Readable流上傳到 S3。
問題是 AWS api 似乎只接受 aReadStream作為流引數。
例如,以下代碼段就可以正常作業:
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( ReadStreamextends 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
*** 請注意,我更喜歡 aReadable而不是 a ReadStream,因為我希望允許傳遞不一定源自檔案的流 - 例如記憶體中的字串。因此,可能的解決方案是將 a 轉換Readable為 aReadstream以使用 SDK。
任何幫助都感激不盡!
uj5u.com熱心網友回復:
鑒于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
}
希望這會有所幫助!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/467383.html
標籤:javascript 节点.js 亚马逊-s3 溪流
