Unsupported body payload object when trying to upload to Amazon S3 using stream.PassThrough

女生的网名这么多〃 提交于 2020-06-17 09:36:21

问题


After not finding any working solution to this problem for me, I am pasting my Angular Electron app code.

Component

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

And the service is :

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

The localPath is checked before with fs.stats, so the error is not because the file is not there. In fact I am able to use it on a s3.putObject, before realising the size of the uploads.


回答1:


You are using fs, a nodejs library, which is not available in browser. If you want to upload files directly from browser to s3, check this example given in aws documentation: https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/s3-example-photo-album.html



来源:https://stackoverflow.com/questions/62330721/unsupported-body-payload-object-when-trying-to-upload-to-amazon-s3-using-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!