NodeJS AWS SDK配置中缺少凭据

ecfsfe2w  于 2023-01-12  发布在  Node.js
关注(0)|答案(5)|浏览(172)

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

我正在使用“aws-sdk”:在react原生(0.64.0)项目中的“^2.918.0”。我正在使用节点v14.17.0和Windows 10版本21 H1操作系统内部版本号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);

~/.aws/配置

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

[profile myName]
region = us-east-1
output = json

~/.aws/凭证

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

[mateo\.lara]
aws_access_key_id=XXX...
aws_secret_access_key=XXX...

当我使用ddb查询数据库时,尽管我已经在AWS CLI(aws-cli/2.2.6 Python/3.8.8 Windows/10 exe/AMD 64 prompt/off)中配置了我的凭据,但我还是收到了这个凭据错误。您知道可能是什么问题吗?如何解决?
------------------------------编辑-------------------
根据@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}
enyaitl3

enyaitl31#

首先,感谢nishkaush和Jatin Mehrotra的帮助。最后,我解决问题的唯一方法是将“accessKeyId”和“secretAccessKey”直接添加到我的AWS.config.update中。这绝不是最终的解决方案,您应该避免将凭据直接添加到代码中(安全原因)。请注意,如果您打算使用此解决方案,你应该通过一个.env文件设置你的凭证,并将它添加到一个gitignore来保护你的凭证。我希望使用共享凭据文件配置我的凭据,但时间不多,问题仍然存在。如果有任何帮助,请检查:
AWS Getting Started in Node.js
AWS Loading Credentials in Node.js from the Shared Credentials File
话虽如此,这个问题可能与我正在使用react native这一事实有关,无论如何这里是解决方案:

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

eni9jsuy2#

在您的~/.aws/config文件中,您是否也可以尝试设置以下内容:

..
..
AWS_SDK_LOAD_CONFIG=1

或者,您可以尝试删除~/.aws/credentials文件,并将aws_access_key_idaws_secret_access_key移动到~/.aws/config文件,如下所示:

[default]
region=us-east-1
output=json
aws_access_key_id = XXX
aws_secret_access_key = XXX
f87krz0w

f87krz0w3#

您还可以设置

s3.config.s3UseArnRegion = true;

如果出现与s3UseArnRegion undefined相关的错误,则为条件

aurhwmvo

aurhwmvo4#

在我的例子中,错误是由于不正确的凭据引起的。由于解析错误,我的identityPoolID值被错误地存储为undefined

rn0zuynd

rn0zuynd5#

我也遇到过同样的问题,我通过
1.将credentials添加到文件~/.aws/credentials中。

[default]
aws_access_key_id = XXXXX
aws_secret_access_key = XXXXX

1.在文件~/.aws/config中添加AWS_SDK_LOAD_CONFIG

[default]
AWS_SDK_LOAD_CONFIG=1

相关问题