【问题标题】:Unsupported body payload object when trying to upload to Amazon S3 using stream.PassThrough尝试使用 stream.PassThrough 上传到 Amazon S3 时不受支持的正文负载对象
【发布时间】:2020-10-01 11:18:05
【问题描述】:

在没有为我找到解决此问题的任何有效解决方案后,我正在粘贴我的 Angular Electron 应用代码。

组件

const pipeline = this.syncService.uploadSong(localPath, filename);
    pipeline.on('close', (data) => {
      // upload finished
    })
    pipeline.on('error', (err) => {
      console.error(err.toString());
    })

服务是:

  uploadSong(localPath: string, filename: string) {
    const {writeStream, promise} = this.uploadStream(filename);
    const readStream = fs.createReadStream(localPath);
    return readStream.pipe(writeStream);
  }

uploadStream(filename: string) {
    const stream = require('stream'); // "stream": "0.0.2",
    const pass = new stream.PassThrough();
    const params = {
      Body: pass,
      Bucket: this.s3Bucket,
      Key: filename,
    };
    const options = {partSize: 5 * 1024 * 1024, queueSize: 6}; 

    return {
      writeStream: pass,
      promise: this.s3.upload(params,options).promise() // ERROR Because the Body
    };
  }

之前用fs.stats检查过localPath,所以错误不是因为文件不存在。事实上,在意识到上传的大小之前,我可以在 s3.putObject 上使用它。

【问题讨论】:

    标签: angular amazon-s3 stream


    【解决方案1】:

    您正在使用 fs,一个 nodejs 库,它在浏览器中不可用。如果您想直接从浏览器上传文件到 s3,请查看 aws 文档中给出的示例:https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/s3-example-photo-album.html

    【讨论】:

    • 谢谢,我忘了说我使用的是 Electron,所以 fs 工作正常。
    • 不确定流在电子中是如何工作的,很久以前我在一个玩具项目中使用电子,当它第一次发布时,从那时起,我就没有真正使用它。我在他们的仓库中发现了这个问题,不确定但可能相关:github.com/electron/electron/issues/21018
    • 我终于使用了 s3-upload-stream 库,它可以工作。我会检查代码并尝试找出原因...
    猜你喜欢
    • 2019-11-28
    • 2021-03-12
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    相关资源
    最近更新 更多