AssumeRole
pom.xml
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sts</artifactId>
<version>2.25.1</version>
</dependency>
import software.amazon.awssdk.core.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.sts.StsClient;
import software.amazon.awssdk.services.sts.auth.StsAssumeRoleCredentialsProvider;
import software.amazon.awssdk.services.sts.model.AssumeRoleRequest;
private S3Client getS3Client() {
String region = "ap-northeast-1";
String role = "arn:aws:iam::XXXXXXXXXXXX:role/switchrole-test";
String sesionName = "Test";
String bucketName = "Test";
StsClinet stsClient = StsClient.builder()
.region(Region.of(region))
.build();
AssumeRoleRequest assumeRoleRequest = AssumeRoleRequset.builder()
.roleArn(role)
.roleSessionName(sessionName)
.build();
StsAssumeRoleCredentialsProvider provider = StsAssumeRoleCredentialsProvider.builder()
.sttsClient(stsClient)
.refreshRequest(assumeRoleRequest)
.build();
S3Client s3Client = S3Client.builder()
.credentialsProvider(provider)
.region(Region.of(region))
.build();
return s3Client;
}