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

问题描述 投票:0回答:9

我在本地设置 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
    }
  }
}

将不胜感激任何帮助!

javascript node.js amazon-web-services amazon-dynamodb aws-sdk
9个回答
67
投票

我也有同样的错误。就我而言,问题是它在本地从命令行运行得很好,但在 docker 中却不行。

基本上,如何在

aws-sdk
(版本 2)和
@aws-sdk/s3-client
(版本 3)之间将凭证传递到 AWS npm S3 客户端已发生变化。在 v2 的情况下,结构是这样的:

const credentials = {
    region: configuration.aws.region,
    accessKeyId: configuration.aws.accessKeyId,
    secretAccessKey: configuration.aws.secretAccessKey
};

在 v3 中:

const credentials = {
  region: configuration.aws.region,
  credentials: {
    accessKeyId: configuration.aws.accessKeyId,
    secretAccessKey: configuration.aws.secretAccessKey
  }
};

它在本地运行良好,因为我已经通过 CLI 向 AWS 进行了身份验证,并且它忽略了我传递给 v3 客户端的那些 v2 样式凭证。

结论:男孩和女孩的一切都使用 Typescript!


15
投票

更改为以下代码,有虚拟

accessKeyId
secretAccessKey
,然后运行它

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));
    }
});

10
投票

这对我有用

const sqsClient = new SQSClient({ 
      region: REGION,
      credentials: {
         accessKeyId: 'XXXXXXXXXXXXXXX',
         secretAccessKey: 'XXXXXXXXXXXXXXXXXXXXXX'
      }
});

9
投票

根据错误消息,您的凭据未在

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'); 

希望它有效!


6
投票

显然我找到了问题所在。使用 json 文件设置凭据仍然会导致错误。仅使用不带 -inMemory 标志的配置对象也会导致错误。在脚本中对凭据进行硬编码并在本地启动 dynamodb 时使用 -inMemory 标志的组合似乎可以解决此问题。虽然我真的不明白为什么。


2
投票

此问题的另一个选项/解决方案是在用户家中创建一个

credentials
文件。

类似:

~/.aws/credentials

内容将是:

[default]
aws_access_key_id = *************************
aws_secret_access_key = *********************************************
aws_session_token =**************************************************

1
投票

您可能对 Web 服务 DynamoDB 和本地版本感到困惑。您可以在AWS文档中找到两者之间的主要区别。

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

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

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

您的错误可能会避免。


1
投票

DynamoDB 本地在 端口 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

操作系统:-

Windows


1
投票

在node.js中,您可以通过设置这些值轻松地从

.env
值加载

AWS_ACCESS_KEY_ID

AWS_SECRET_ACCESS_KEY

AWS_SESSION_TOKEN(可选)

https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/loading-node-credentials-environment.html

© www.soinside.com 2019 - 2024. All rights reserved.