AWS SDK 配置中缺少凭证

Mat*_*ara 8 amazon-web-services node.js amazon-dynamodb aws-cli aws-sdk

错误:可能未处理的承诺拒绝(id:0):CredentialsError:配置中缺少凭证,如果使用 AWS_CONFIG_FILE,请设置 AWS_SDK_LOAD_CONFIG=1

我在 React Native (0.64.0) 项目中使用 "aws-sdk": "^2.918.0" 。我正在使用节点 v14.17.0 和 Windows 10 版本 21H1 操作系统构建 19043.985 上运行该程序:

// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');

AWS.config.update({
  region: 'us-east-1',
  maxRetries: 3,
  httpOptions: {timeout: 30000, connectTimeout: 5000},
});

// Create the DynamoDB service object
var ddb = new AWS.DynamoDB({
  apiVersion: '2012-08-10',
});

console.log('ddb: ', ddb);
Run Code Online (Sandbox Code Playgroud)

〜/.aws/config

[default]
region=us-east-1
output=json

[profile myName]
region = us-east-1
output = json
Run Code Online (Sandbox Code Playgroud)

〜/.aws/凭证

[default]
aws_access_key_id=XXX...
aws_secret_access_key=XXX...

[mateo\.lara]
aws_access_key_id=XXX...
aws_secret_access_key=XXX...
Run Code Online (Sandbox Code Playgroud)

配置数据

使用 ddb 查询数据库时,尽管已在 AWS CLI 中配置了我的凭证(aws-cli/2.2.6 Python/3.8.8 Windows/10 exe/AMD64 提示/关闭),但我还是收到此 CredentialsError。你知道问题可能是什么吗?以及如何解决呢?

- - - - - - - - - - - - - - - - - - - 编辑 - - - - - - --------------------------

按照@nishkaush的建议,我尝试将 AWS_SDK_LOAD_CONFIG=1 添加到配置文件,并且尝试删除凭证文件并将 aws_access_key_id 和 aws_secret_access_key 添加到配置文件,但没有成功。

这是 ddb 日志的输出:

ddb:  {"CALL_EVENTS_BUBBLE": [Function CALL_EVENTS_BUBBLE], "MONITOR_EVENTS_BUBBLE": [Function EVENTS_BUBBLE], "_clientId": 1, "_events": {"apiCall": [[Function CALL_EVENTS_BUBBLE]], "apiCallAttempt": [[Function EVENTS_BUBBLE]]}, "config": {"apiVersion": "2012-08-10", "apiVersions": {}, "clientSideMonitoring": 
false, "computeChecksums": true, "convertResponseTypes": true, "correctClockSkew": false, "credentialProvider": null, "credentials": null, "customUserAgent": null, "dynamoDbCrc32": true, "endpoint": "dynamodb.us-east-1.amazonaws.com", "endpointCacheSize": 1000, "endpointDiscoveryEnabled": undefined, "hostPrefixEnabled": true, "httpOptions": {"connectTimeout": 5000, "timeout": 30000}, "logger": null, "maxRedirects": 10, "maxRetries": 3, "paramValidation": true, "region": "us-east-1", "retryDelayOptions": {}, "s3BucketEndpoint": false, "s3DisableBodySigning": true, "s3ForcePathStyle": false, "s3UsEast1RegionalEndpoint": "legacy", 
"s3UseArnRegion": undefined, "signatureCache": true, "signatureVersion": "v4", "sslEnabled": true, "stsRegionalEndpoints": "legacy", "systemClockOffset": 0, "useAccelerateEndpoint": false}, "endpoint": {"host": "dynamodb.us-east-1.amazonaws.com", "hostname": "dynamodb.us-east-1.amazonaws.com", "href": "https://dynamodb.us-east-1.amazonaws.com/", "path": "/", "pathname": "/", "port": 443, "protocol": "https:"}, "isGlobalEndpoint": false}
Run Code Online (Sandbox Code Playgroud)

Mat*_*ara 9

首先,感谢 nishkaush 和 Jatin Mehrotra 的帮助。最后,解决问题的唯一方法是将“accessKeyId”和“secretAccessKey”直接添加到我的 AWS.config.update 中。这绝不是最终的解决方案,您应该避免将凭据直接放入代码中(安全原因)。请注意,如果您打算使用此解决方案,您应该通过 .env 文件设置您的凭据并将其添加到 gitignore 以保护您的凭据。我本想使用共享凭据文件配置我的凭据,但我的时间不多了,而且问题仍然存在。如果有帮助,请检查:

AWS Node.js 入门

AWS 从共享凭证文件加载 Node.js 中的凭证

话虽如此,问题可能与我正在使用 React Native 相关,无论如何,这是解决方案:

AWS.config.update({
  maxRetries: 3,
  httpOptions: {timeout: 30000, connectTimeout: 5000},
  region: 'us-east-1',
  accessKeyId: 'XXXX...',
  secretAccessKey: 'XXXX...',
});
Run Code Online (Sandbox Code Playgroud)