使用新DocumentClient()的正确方法

时间:2018-07-06 05:53:08

标签: amazon-web-services amazon-dynamodb

我在频繁使用DynamoDB的ec2实例中偶尔看到以下错误

{ message: 'Could not load credentials from any providers',
code: 'CredentialsError',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '169.254.169.254',
port: 80,
time: 2018-07-05T10:02:37.690Z,
originalError: 
{ code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '169.254.169.254',
port: 80,
message: 'connect ECONNREFUSED 169.254.169.254:80' } } }

我使用AWS sdk连接并从DynamoDB获取数据的方式如下所示

'use strict'
const BluebirdPromise = require('bluebird')
AWS.config.setPromisesDependency(BluebirdPromise)
const DocumentClient = AWS.DynamoDB.DocumentClient

class DynamoOne {

  get({
    key
  }) {
    return new DocumentClient().get({ // creating new instance every time i want to get Item
        Key: key,
        TableName: 'TABLE_NAME',
        ConsistentRead: consistent,
      })
      .promise()
      .catch(e => console.trace(key, e))
  }
}

new DynamoOne.get({
  id: 1
}).then((item) => {
  console.log(1, item)
})

这是使用它的正确方法吗?或者我应该一次创建new DocumentClient()并将其全部使用,例如以下代码

'use strict'
const BluebirdPromise = require('bluebird')
AWS.config.setPromisesDependency(BluebirdPromise)
const DocumentClient = new AWS.DynamoDB.DocumentClient() // creating new instance once

class DynamoTwo {

  get({
    key
  }) {
    return DocumentClient.get({ // reuse DocumentClient
        Key: key,
        TableName: 'TABLE_NAME',
        ConsistentRead: consistent,
      })
      .promise()
      .catch(e => console.trace(key, e))
  }

}


new DynamoTwo.get({
  id: 1
}).then((item) => {
  console.log(1, item)
})

DynamoOne或DynamoTwo哪个是正确的?

1 个答案:

答案 0 :(得分:0)

研究后,asking in github aws repo 我发现正确的方法是使用<a href="<?= base_url();?>index.php/login/signup" class="btn btn-success"><span class="glyphicon glyphicon-plus-sign"></span> Add Student Account</a>