ZiaUllahZia
ZiaUllahZia

Reputation: 1142

Amazon S3 putobject for uploading a file to S3, does not return a promise

I have a function that uses Amazon S3 putobject to upload a file to S3, but this does not return a promise which I need to await successful completion after which I can then load a modal with the results (success or failure).

I have tried various async await promise permutations on the s3 function but none work with the .on httpUploadProgress.

s3.putObject(params)
        .on("httpUploadProgress", (progressEvent, response) => {
          const percent = parseInt(
            (100 * progressEvent.loaded) / progressEvent.total
          )
          setuploadprog(percent)
          if (percent === 100) {
            setuploadsuccess(1)
          }
        })
        .send((err, data) => {
          if (err) {
            console.log(err, err.stack)
            setuploadsuccess(2)
          } else {
            console.log(data)
          }
        })

Upvotes: 0

Views: 794

Answers (2)

ZiaUllahZia
ZiaUllahZia

Reputation: 1142

...await s3
.putObject(params)

Solve the issue by myself.

Upvotes: 0

Bergi
Bergi

Reputation: 665121

Don't call send with a callback. Do use the promise method instead:

s3.putObject(params)
    .on("httpUploadProgress", (progressEvent, response) => {
        const percent = Math.floor(progressEvent.loaded / progressEvent.total * 100);
        setuploadprog(percent)
    })
    .promise()
    .then(data => {
        setuploadsuccess(1);
        return data;
    }, err => {
        setuploadsuccess(2);
        throw err;
    });

Upvotes: 1

Related Questions