How do I promisify the AWS JavaScript SDK?

后端 未结 8 1080
面向向阳花
面向向阳花 2020-12-08 18:52

I want to use the aws-sdk in JavaScript using promises.

Instead of the default callback style:

dynamodb.getItem(params, function(err, data) {
  if (         


        
相关标签:
8条回答
  • 2020-12-08 19:11

    This solution works best for me:

    // Create a promise object
    var putObjectPromise = s3.putObject({Bucket: 'bucket', Key: 'key'}).promise(); 
    
    // If successful, do this:
    putObjectPromise.then(function(data) {
     console.log('PutObject succeeded'); })
    
    // If the promise failed, catch the error:
    .catch(function(err) { 
    console.log(err); });
    
    0 讨论(0)
  • 2020-12-08 19:13

    Way overdue, but there is a aws-sdk-promise npm module that simplifies this.

    This just adds a promise() function which can be used like this:

    ddb.getItem(params).promise().then(function(req) {
        var x = req.data.Item.someField;
    });
    

    EDIT: It's been a few years since I wrote this answer, but since it seems to be getting up-votes lately, I thought I'd update it: aws-sdk-promise is deprecated, and newer (as in, the last couple of years) versions of aws-sdk includes built-in promise support. The promise implementation to use can be configured through config.setPromisesDependency().

    For example, to have aws-sdk return Q promises, the following configuration can be used:

    const AWS = require('aws-sdk')
    const Q = require('q')
    
    AWS.config.setPromisesDependency(Q.Promise)
    

    The promise() function will then return Q promises directly (when using aws-sdk-promise, you had to wrap each returned promise manually, e.g. with Q(...) to get Q promises).

    0 讨论(0)
  • 2020-12-08 19:13

    Folks, I've not been able to use the Promise.promisifyAll(Object.getPrototypeOf(dynamoDb));

    However, the following worked for me:

    this.DYNAMO = Promise.promisifyAll(new AWS.DynamoDB());
    ...
    return this.DYNAMO.listTablesAsync().then(function (tables) {
        return tables;
    });
    

    or

    var AWS = require('aws-sdk');
    var S3 = Promise.promisifyAll(new AWS.S3());
    
    return S3.putObjectAsync(params);
    
    0 讨论(0)
  • 2020-12-08 19:20

    You can use a promise library that does promisification, e.g. Bluebird.

    Here is an example of how to promisify DynamoDB.

    var Promise = require("bluebird");
    
    var AWS = require('aws-sdk');
    var dynamoDbConfig = {
      accessKeyId: process.env.AWS_ACCESS_KEY_ID,
      secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
      region: process.env.AWS_REGION
    };
    var dynamoDb = new AWS.DynamoDB(dynamoDbConfig);
    Promise.promisifyAll(Object.getPrototypeOf(dynamoDb));
    

    Not you can add Async to any method to get the promisified version.

    0 讨论(0)
  • 2020-12-08 19:21

    CascadeEnergy/aws-promised

    We have an always in progress npm module aws-promised which does the bluebird promisify of each client of the aws-sdk. I'm not sure it's preferable to using the aws-sdk-promise module mentioned above, but here it is.

    We need contributions, we've only taken the time to promisify the clients we actually use, but there are many more to do, so please do it!

    0 讨论(0)
  • 2020-12-08 19:26

    The 2.3.0 release of the AWS JavaScript SDK added support for promises: http://aws.amazon.com/releasenotes/8589740860839559

    0 讨论(0)
提交回复
热议问题