在Node中本地使用dynamodb时“无法从任何提供程序加载凭据”

时间:2017-04-10 11:57:14

标签: javascript node.js amazon-web-services amazon-dynamodb aws-sdk

我在本地设置一个dynamodb来测试我的Node应用程序。要设置它,我只需要复制here中的代码并根据我的需要进行调整。
这是代码:

var AWS = require("aws-sdk");

var config = ({
  "apiVersion": "2012-08-10",
  "accessKeyId": "abcde",
  "secretAccessKey": "abcde",
  "region": "us-west-2",
  "endpoint": "http://localhost:8001",
});

var dynamodb = new AWS.DynamoDB(config);

var params = {
    TableName : "Movies",
    KeySchema: [       
        { AttributeName: "year", KeyType: "HASH"},  //Partition key
        { AttributeName: "title", KeyType: "RANGE" }  //Sort key
    ],
    AttributeDefinitions: [       
        { AttributeName: "year", AttributeType: "N" },
        { AttributeName: "title", AttributeType: "S" }
    ],
    ProvisionedThroughput: {       
        ReadCapacityUnits: 10, 
        WriteCapacityUnits: 10
    }
};

dynamodb.createTable(params, function(err, data) {
    if (err) {
        console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2));
    }
});

这会引发错误但我不明白为什么:

Unable to create table. Error JSON: {
  "message": "Missing credentials in config",
  "code": "CredentialsError",
  "time": "2017-04-10T11:45:26.748Z",
  "retryable": true,
  "originalError": {
    "message": "Could not load credentials from any providers",
    "code": "CredentialsError",
    "time": "2017-04-10T11:45:26.747Z",
    "retryable": true,
    "originalError": {
      "message": "Connection timed out after 1000ms",
      "code": "TimeoutError",
      "time": "2017-04-10T11:45:26.747Z",
      "retryable": true
    }
  }
}

感谢任何帮助!

5 个答案:

答案 0 :(得分:9)

更改为以下代码,使用虚拟accessKeyIdsecretAccessKey,然后运行

var AWS = require("aws-sdk");

AWS.config.update({
  region: 'us-west-1',
  accessKeyId: 'accessKeyId',
  secretAccessKey: 'secretAccessKey',
  endpoint: new AWS.Endpoint('http://localhost:8000'),
});

var dynamodb = new AWS.DynamoDB();

var params = {
    TableName : "Movies",
    KeySchema: [
        { AttributeName: "year", KeyType: "HASH"},  //Partition key
        { AttributeName: "title", KeyType: "RANGE" }  //Sort key
    ],
    AttributeDefinitions: [
        { AttributeName: "year", AttributeType: "N" },
        { AttributeName: "title", AttributeType: "S" }
    ],
    ProvisionedThroughput: {
        ReadCapacityUnits: 10,
        WriteCapacityUnits: 10
    }
};

dynamodb.createTable(params, function(err, data) {
    if (err) {
        console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2));
    }
});

答案 1 :(得分:7)

根据错误消息,您的凭据未在config中设置。

我正在考虑设置凭据,然后使用服务。

 const aws = require('aws-sdk');
 aws.config = new aws.Config();
 aws.config.accessKeyId = "xxxxxxxxxxxxxx";
 aws.config.secretAccessKey = "xxxxxxxxxx";
 aws.config.region = "region";

现在使用Dynamodb

var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});

<强> 更新

你也可以用文件

设置凭证
aws.config.loadFromPath('./AwsConfig.json'); 

希望它有效!!

答案 2 :(得分:4)

显然我弄明白了这个问题。使用json文件设置凭据仍然导致错误。仅使用没有标志-inMemory的配置对象也会导致错误。在本地启动dynamodb时,硬编码脚本中的凭证和使用-inMemory标志的组合似乎解决了这个问题。我真的不明白为什么。

答案 3 :(得分:0)

您可能在Web服务DynamoDB和本地版本之间感到困惑。您可以在AWS documentation中找到两者之间的主要差异。

如果您想使用本地版本的DynamoDB,您可以在AWS documentation中找到要安装和运行的最新信息。

完成后,请确保通过运行以下命令运行本地DynamoDB实例:

java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb -inMemory

可能会避免您的错误。

答案 4 :(得分:0)

DynamoDB local适用于带有虚拟凭据的端口8000

注意:

端口号 8000 。我使用下面的虚拟凭证。

var creds = new AWS.Credentials('akid', 'secret', 'session');

AWS.config.update({
    region: "us-west-2",
    endpoint: "http://localhost:8000",
    credentials: creds
});

启动命令: -

java -Djava.library.path=DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb

<强> OS: -