简体   繁体   English

如何解决对AWS Java SDK DynamoDB的调用导致ExpiredTokenException?

[英]How do I cure a call to the AWS Java SDK DynamoDB resulting in an ExpiredTokenException?

I have a long running AWS Java SDK DynamoDB application which behaves normally when I start it up. 我有一个长期运行的AWS Java SDK DynamoDB应用程序,当我启动它时它会正常运行。 However, after some number of hours (around 12), I start receiving the same Exception over and over again with ANY call to the DynamoDB API. 但是,经过几个小时(大约12个小时),我开始一遍又一遍地接收相同的Exception ,并且任何对DynamoDB API的调用。 If I restart the server, the Exception disappears...only to reappear again later. 如果我重新启动服务器, Exception消失...只是稍后再次出现。

The exact ExpiredTokenException error text is: 确切的ExpiredTokenException错误文本是:
The security token included in the request is expired (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ExpiredTokenException ; Request ID: DEMTN0Q5BMPH5IQD9TUQMNO5SFVV4KQNSO5AEMVJF66Q9ASUAAJG) 请求中包含的安全令牌已过期(Service:AmazonDynamoDBv2; Status Code:400; Error Code: ExpiredTokenException ; Request ID:DEMTN0Q5BMPH5IQD9TUQMNO5SFVV4KQNSO5AEMVJF66Q9ASUAAJG)

Summary: 摘要:
Pass an instance of AWSCredentialsProvider (as opposed to AWSCredentials ) to AmazonDynamoDBClient 's constructor as this enables automatic refreshing of expired AWSCredentials (if the particular AWSCredentialsProvider has implemented the refresh functionality...which is the case with all the standard AWS provided ones). AWSCredentialsProvider的实例(而不是AWSCredentials )传递给AmazonDynamoDBClient的构造函数,因为这样可以自动刷新过期的AWSCredentials (如果特定的AWSCredentialsProvider已实现刷新功能......所有AWS提供的标准都是这种情况)。

Details: 细节:
To resolve the AWS Java SDK DynamoDB related ExpiredTokenException which starts with the prefix " The security token included in the request is expired (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ExpiredTokenException; Request ID: ... ", you must alter your code to provide an instance of AWSCredentialsProvider (and stop using an instance of AWSCredentials - ie sans the "Provider" suffix) to the AmazonDynamoDBClient 's constructor. By handing the AmazonDynamoDBClient 's constructor an instance of AWSCredentialsProvider , you give it the ability to "automatically refresh the credentials" if/when the AWSCredentials expire (which I found in this AWS forum thread which requires an account to access). 要解决AWS Java SDK与DynamoDB相关的ExpiredTokenException,它以前缀“ 请求中包含的安全令牌已过期(服务:AmazonDynamoDBv2;状态代码:400;错误代码:ExpiredTokenException;请求ID:... ”) 开头 ,您必须更改你的代码提供的实例AWSCredentialsProvider (并停止使用的实例AWSCredentials -即SAN的“提供者”的后缀)的AmazonDynamoDBClient '的构造函数通过发放了AmazonDynamoDBClient的构造的一个实例AWSCredentialsProvider ,你给它的能力,如果/当AWSCredentials过期时(我在此AWS论坛帖子中找到 ,需要帐户访问),“自动刷新凭据”。

To provide an explicit example in code, here's a generalization of what the code is producing the ExpiredTokenException : 为了在代码中提供一个显式示例,这里是代码产生ExpiredTokenException

AWSCredentialsProvider aWSCredentialsProvider =
  new SystemPropertiesCredentialsProvider();
    //the above line may be substituted for any valid
    //*Provider implementation
AWSCredentials aWSCredentials =
  aWSCredentialsProvider.getCredentials();
AmazonDynamoDBClient amazonDynamoDBClient =
  new AmazonDynamoDBClient(aWSCredentials);
...
amazonDynamoDBClient.listTables();
  //the above line is where the ExpiredTokenException is eventually thrown

And here is a generalization of the code eliminating the ExpiredTokenException : 以下是消除ExpiredTokenException的代码的ExpiredTokenException

AWSCredentialsProvider aWSCredentialsProvider =
  new SystemPropertiesCredentialsProvider();
    //substitute the above line for any valid *Provider implementation
AmazonDynamoDBClient amazonDynamoDBClient =
  new AmazonDynamoDBClient(aWSCredentialsProvider);
    //the above line is now passing an instance of AWSCredentialsProvider
    //as opposed to AWSCredentials
...
amazonDynamoDBClient.listTables();
  //the above line is now enabled, via the AWSCredentialsProvider, to 
  //automatically refresh the AWSCredentials if/when they have expired

Given how much I climbed all over the AWS Java SDK Javadocs and their provided examples (upon which I based most of my own code), I didn't once notice this specific nuance called out. 考虑到我在AWS Java SDK Javadocs及其提供的示例(基于我的大部分自己的代码)上爬了多少,我没有注意到这个特定的细微差别。 Hence, the very detailed answer I'm providing for those who come after me (which likely will include myself, LOL). 因此,我为那些追随我的人提供了非常详细的答案(可能包括我自己,LOL)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在AWS DynamoDB中处理空的java字符串集 - How do I handle an empty java set of strings in AWS DynamoDB 使用来自 java aws sdk 的 DynamoDb 增强客户端,您如何使用复合 keyConditionExpression 进行查询? - With DynamoDb enhanced client from java aws sdk, how do you query using a compound keyConditionExpression? 如何将 Java 枚举与 Amazon DynamoDB 和 AWS SDK v2 一起使用? - How can I use Java Enums with Amazon DynamoDB and AWS SDK v2? 如何使用AWS Java SDK以降序从DynamoDb获取项目? - How to get items from DynamoDb in Descending order with AWS Java SDK? 如何写入 AWS DynamoDB 表? - How do I write to an AWS DynamoDB table? 注释的AWS DynamoDB Java SDK替代品 - AWS DynamoDB Java SDK alternative to annotations 如何使用Java SDK查看dynamoDB中的更新或插入是否成功? - how do I find out if an update or insert was successful in dynamoDB using the Java SDK? 通过Java SDK删除AWS DynamoDB - AWS DynamoDB Deleting via Java SDK 如何从Spring Java Elastic Beanstalk应用程序连接到我的AWS DynamoDB实例? - How do I connect to my AWS DynamoDB instance from a Spring Java Elastic Beanstalk app? 如何解决Hibernate异常的原因“调用setter时发生IllegalArgumentException”? - How do I cure the cause of Hibernate exception “IllegalArgumentException occurred while calling setter”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM