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

1

1

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.

Interfaith answered 11/6, 2020 at 18:5 Comment(0)
A
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

Antony answered 12/6, 2020 at 6:38 Comment(3)
Thanks, I forgot to say that I am using Electron, so fs is working ok.Interfaith
not sure how stream works in electron, i used electron for a toy project long ago when it was first published, since then, i didn't use it really. I found this issue in their repo, not sure but it might be related: github.com/electron/electron/issues/21018Antony
I finally used s3-upload-stream library, it works. I will check the code and try to find why ...Interfaith

© 2022 - 2024 — McMap. All rights reserved.