如何将 STSAssumeRoleSessionCredentialsProvider 与 roleSessionName 和 roleArn 结合使用用于适用于 Java 2.x 的 AWS 开发工具包进行配置?

R M*_*R M 6 file-upload amazon-s3 amazon-web-services aws-java-sdk aws-java-sdk-2.x

目前,对于 AWS SDK for Java 1.x,我使用以下代码。

@Configuration
@ImportResource("classpath:aws-context.xml")
public class AmazonS3Config {
 @Bean(destroyMethod = "shutdown")
 public AmazonS3Client amazonS3Client(@Value("${aws.s3.roleSessionName}") String roleSessionName, 
 @Value("${aws.s3.roleArn}") String role) {
 AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard();
 builder.withRegion(Regions.US_EAST_1).withCredentials(new 
 STSAssumeRoleSessionCredentialsProvider.Builder(role, roleSessionName).build());
 return (AmazonS3Client)builder.build();
   }
Run Code Online (Sandbox Code Playgroud)

如何对适用于 Java 2.x 的 AWS 开发工具包执行相同操作?

谢谢

GSS*_*ain 9

STSAssumeRoleSessionCredentialsProviderSDK V2 中的等效项是StsAssumeRoleCredentialsProvider.

因此,V2 中等效的 S3Client 初始化如下所示

public S3Client s3Client(@Value("${aws.s3.roleSessionName}") String roleSessionName,  
        @Value("${aws.s3.roleArn}") String role) {

 return S3Client
       .builder()
       .region(Region.US_EAST_1)
       .credentialsProvider(StsAssumeRoleCredentialsProvider
               .builder()
               .refreshRequest(() -> AssumeRoleRequest
                       .builder()
                       .roleArn(role)
                       .roleSessionName(roleSessionName)
                       .build())
               .build())
       .build();
Run Code Online (Sandbox Code Playgroud)

在 V2 中,maven/gradle 依赖组和包名称已更改为software.amazon.awssdk。确保包含S3和 的依赖项STS是更改日志,是迁移指南。