"Could not load credentials from any providers" 在 Node 中本地使用 dynamodb

"Could not load credentials from any providers" while using dynamodb locally in Node

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

如有任何帮助,我们将不胜感激!

根据错误消息,您的凭据未在 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'); 

希望有用!!

您可能混淆了 Web 服务 DynamoDB 和本地版本。您可以在 AWS documentation.

中找到两者之间的主要区别

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

完成后,确保 运行 本地 DynamoDB 实例 运行ning:

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

你的错误可能会被避免。

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

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:-

Windows

更改为以下代码,有虚拟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));
    }
});

我有同样的错误。在我的例子中,问题是它在本地 运行 从命令行运行良好,但在 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 样式凭证。

结论:男孩女孩的一切都使用打字稿!

在 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

这对我有用

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