The other day, I participated in an event
** "By the way, I wrote an article to encrypt and upload to an S3 bucket, but I used an access key to authenticate!" ** (Not in line with security best practices)
I noticed that, so I decided to supplement it.
** Encrypting data uploaded to S3 using AWS SDK for Java / SSE-KMS ** is a continuation.
· In IAM, click ** Create New Role **.

-Select ** "Amazon EC2" **.

-Check ** "Amazon S3 Full Access" ** and click ** [Next Step] **.

-Enter ** role name ** and click ** [Create Role] **.

-Select the ** encryption key ** used for S3 encryption.

-Click ** [Add] ** of ** Key User **.

-Select the ** IAM role you created ** and click ** [Attach] **.

-Assign the created IAM role to the ** target EC2 instance **.

-Enter / select the ** IAM role name ** and click ** Apply **.

-If the process is successful, start (start) the target EC2 instance and check that the IAM Role is applied.

For Java code, simply unspecify credentials.
change point
public class S3Access {
    private static final String ENDPOINT_URL = "https://s3-ap-northeast-1.amazonaws.com";
    private static final String REGION       = "ap-northeast-1";
//  private static final String ACCESS_KEY   = "【access key】";
//  private static final String SECRET_KEY   = "[Secret key]";
    private static final String KMS_KEY_ID   = "[KMS key ID]";
(Omission)
    //--------------------------------------------------
    //Client generation
    //--------------------------------------------------
    private AmazonS3 getClient(String bucketName) throws Exception {
//      //Authentication information
//      AWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
        //Client settings
        ClientConfiguration clientConfig = new ClientConfiguration();
        clientConfig.setProtocol(Protocol.HTTPS);  //protocol
        clientConfig.setConnectionTimeout(10000);   //Connection timeout(ms) 
        //Endpoint setting
        EndpointConfiguration endpointConfiguration = new EndpointConfiguration(ENDPOINT_URL, REGION);
        //Client generation
        AmazonS3 client = AmazonS3ClientBuilder.standard()
//                      .withCredentials(new AWSStaticCredentialsProvider(credentials))
                        .withClientConfiguration(clientConfig)
                        .withEndpointConfiguration(endpointConfiguration).build();
(Omission)
** Delete the line commented out with "//" at the beginning of the line **.
Recommended Posts