Ad

AWS S3 Upload Not Working After Converting From Callback To Async-Await Format Using Promisify

I'm using upload method from aws-sdk to upload files to S3 bucket from React app in browser.

The original callback based upload method is as bellow:

var params = {Bucket: 'bucket', Key: 'key', Body: stream};
s3.upload(params, function(err, data) {
  console.log(err, data);
});

I wrapped it with promisify to work it like Async-await as bellow:

const AWS = require('aws-sdk');
const { promisify } = require('es6-promisify');

... <in my React component> ...
async uploadFile() {
try {
    var params = {
      Bucket: <BucketName>,
      Key: <KeyName>,
      Body: <File Stream>
    };
    const res = await uploadToS3Async(params);
    console.log(res);
  } catch (error) {
    console.log(error);
  }
}

Now when I'm calling uploadFile function on some event fired it produces following error:

TypeError: service.getSignatureVersion is not a function
    at ManagedUpload.bindServiceObject  
    at ManagedUpload.configure 
    at new ManagedUpload  
    at upload   
    at new Promise (<anonymous>)
Ad

Answer

You don't need to use es6-promisify

You can do:

try {
  const params = {Bucket: 'bucket', Key: 'key', Body: stream};  
  const data = await s3.upload(params).promise();
  console.log(data);
}
catch (err) {
  console.log(err);
}

https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/using-promises.html

Ad
source: stackoverflow.com
Ad