AWS Local DynamoDB 请求中包含的安全令牌无效

AWS Local DynamoDB The security token included in the request is invalid

我是 AWS 的新手,我正在尝试通过 Java 程序对本地 DynamoDB 执行 CRUD 操作。 Java 程序是一个 AWS 示例。

我安装了 AWS CLI 并设置了以下配置 - 根据 AWS 文档,我不需要本地 DynamoDB 的真实 AWS 访问和密钥。

我在 ~/.aws/config 和 ~/.aws/credentials 中通过 运行ning aws configure 在 AWS CLI 中设置了以下值。

[default]
aws_access_key_id = ''
aws_secret_access_key = ''
[default]
region = ap-south-1

我有本地 DYnamoDB JAR 运行 这个。

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

我正在尝试 运行 的代码是这样的。

https://github.com/aws-samples/aws-dynamodb-examples/blob/master/src/main/java/com/amazonaws/codesamples/datamodeling/ObjectPersistenceCRUDExample.java

但是,我遇到了这个异常。

AmazonDynamoDBException: The security token included in the request is invalid.

完整的堆栈是这样的。

    Exception in thread "main" com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: The security token included in the request is invalid. (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: UnrecognizedClientException; Request ID: UPGRD2BRNUN6S1702EN6N6S8RJVV4KQNSO5AEMVJF66Q9ASUAAJG)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1695)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1350)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1101)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:758)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:732)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:714)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access0(AmazonHttpClient.java:674)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:656)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:520)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.doInvoke(AmazonDynamoDBClient.java:4192)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:4159)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.executeUpdateItem(AmazonDynamoDBClient.java:3868)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.updateItem(AmazonDynamoDBClient.java:3835)
    at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper$SaveObjectHandler.doUpdateItem(DynamoDBMapper.java:854)
    at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.executeLowLevelRequest(DynamoDBMapper.java:594)
    at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper$SaveObjectHandler.execute(DynamoDBMapper.java:733)
    at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.save(DynamoDBMapper.java:623)
    at com.amazonaws.services.dynamodbv2.datamodeling.AbstractDynamoDBMapper.save(AbstractDynamoDBMapper.java:123)
    at com.stackroute.Main.testCRUDOperations(Main.java:60
    at com.stackroute.Main.main(Main.java:17)

我们将不胜感激任何帮助。提前致谢。

据我所知,引用的代码中没有任何内容指向 DynamoDB 本地实例。看起来它正指向 DynamoDB。

您需要将端点更改为本地版本。看看this page。它有一个将端点更改为 localhost:8080.

的示例

准确地说,问题确实似乎指向 AWS 上的 DynamoDB,而不是本地的,尽管我无法验证原始评论的这一点,因为 link 现在已经损坏,至少这就是发生在我身上的事情。

解决方案是显式连接到本地 DynamoBD(需要安装) 这是实现它的代码:

AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(
            // we can use any region here
            new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2"))
            .build();

这是之前评论中提到的例子class的link,已删除(您需要点击DynamoDBLocalFixture.java class的Load Diff) : https://github.com/aws-samples/aws-dynamodb-examples/commit/461b0e85bfad58f4a62c63f38426c650896fb870#diff-b331539c5e8b0398d9bbb85c5b87a4cdfa32efbefb27f68f3402c223b243b178