0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Java AmazonS3 AssumeRole 自分用メモ

Posted at

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;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?