DynamoDBLocal拒绝我的凭据,尽管文档指出有效的凭据是不必要的:
用于DynamoDB的AWS sdk要求您的应用程序配置指定访问键值和AWS区域值…为了在本地运行,这些不必是有效的AWS值。
在本例中,我将我的凭据~/.aws/credentials
设置为:
[default]
aws_access_key_id = BogusAwsAccessKeyId
aws_secret_access_key = BogusAwsSecretAccessKey
运行DynamoDBLocal:
java -Djava.library.path=./DynamoDBLoc_lib -jar DynamoDBLocal.jar
通过点击http://localhost:8000/shell/
检查它是否正常工作然后运行我的测试Java应用程序:
DefaultAWSCredentialsProviderChain credentialProvider = new DefaultAWSCredentialsProviderChain();
AWSCredentials awsCredentials = credentialProvider.getCredentials();
log.info("creds "{}", "{}"", awsCredentials.getAWSAccessKeyId(), awsCredentials.getAWSSecretKey());
AmazonDynamoDBClient client = new AmazonDynamoDBClient(credentialProvider);
client.withEndpoint("http://localhost:8000");
client.withRegion(Regions.US_WEST_2);
dynamoDB = new DynamoDB(client);
try {
TableCollection<ListTablesResult> tables = dynamoDB.listTables();
while (tables.iterator().hasNext()) { // <-- exception thrown here
log.info(tables.iterator().next().getTableName());
}
} catch (Exception e) {
log.error("", e);
}
,输出如下:
creds "BogusAwsAccessKeyId", "BogusAwsSecretAccessKey"amazonserviceexception:请求中包含的安全令牌无效。(服务:AmazonDynamoDBv2;状态码:400;错误码:UnrecognizedClientException
关于为什么它与凭证的有效性有关,有什么想法吗?
在您的代码中,您在调用setEndpoint()
之后调用withRegion()
。对withRegion()
的调用将端点设置为DynamoDB的us-west-2端点,这就是身份验证失败的原因(因为它实际上是前往DynamoDB的us-west-2区域)。删除withRegion()
行