[英]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.