@Bean
public DynamoDbClient amazonDynamoDB() {
final AssumeRoleRequest assumeRoleRequest = AssumeRoleRequest.builder().roleSessionName(UUID.randomUUID().toString()).roleArn("roleArn").build();
final StsAssumeRoleCredentialsProvider stsAssumeRoleCredentialsProvider = StsAssumeRoleCredentialsProvider.builder().refreshRequest(assumeRoleRequest).build();
return DynamoDbClient.builder().credentialsProvider(stsAssumeRoleCredentialsProvider).region(Region.EU_WEST_1)
.build();
}
获取错误
原因:org.springframework.beans.BeanInstantiationException: 无法实例化 [software.amazon.awssdk.services.dynamodb.DynamoDbClient]:工厂 方法“ amazonDynamoDB”抛出异常;嵌套异常为 java.lang.NullPointerException:STS客户端不能为null。在 spring.beans@5.2.7.RELEASE/org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) 在 spring.beans@5.2.7.RELEASE/org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:650) ...省略了58个共同的框架 java.lang.NullPointerException:STS客户端不能为null。
答案 0 :(得分:0)
假设角色请求需要设置sts客户端并且它起作用了
@Bean
@Primary
public DynamoDbClient amazonDynamoDB() {
final AssumeRoleRequest assumeRoleRequest = AssumeRoleRequest.builder().roleSessionName(UUID.randomUUID().toString()).roleArn("roleArn").build();
final StsClient stsClient = StsClient.builder().region(Region.EU_WEST_1).build();
final StsAssumeRoleCredentialsProvider stsAssumeRoleCredentialsProvider = StsAssumeRoleCredentialsProvider.builder().stsClient(stsClient).refreshRequest(assumeRoleRequest).build();
return DynamoDbClient.builder().credentialsProvider(stsAssumeRoleCredentialsProvider).region(Region.EU_WEST_1)
.build();
}