Uploading object to S3 with Java AWS SDK and access point

Viewed 1708

I'm working with AWS Java SDK and I want to upload objects to a bucket using access point instead of bucket name. Is it possible?

I want to do it by using access point because the bucket's public access is closed but the access is granted through an access point.

1 Answers

Yes, you can perform PutObject using the access point.

Here is the list of all the supported operations using access points.

Simply use the access point ARN in place of the bucket name. arn:aws:s3:<bucket-region>:<accountNumber>:accesspoint:<access-point-name>

Works fine with both Java SDK v1 and v2. Tested on com.amazonaws:aws-java-sdk-s3:1.11.1004 and software.amazon.awssdk:s3:2.16.47

Sample code AWS Java SDK 1.x:

String bucketName = "arn:aws:s3:<bucket-region>:<accountNumber>:accesspoint:<access-point-name>";
String filePath = "/Users/user/sample.txt";
String key = Paths.get(filePath).getFileName().toString();
final AmazonS3 s3 = AmazonS3ClientBuilder
   .standard()
   .withRegion(Regions.<bucket-region>)
   .build();
try {
   s3.putObject(bucketName, key, new File(filePath));
} catch (AmazonServiceException e) {
   // handle exception or throw
   throw e;
}

Sample code AWS Java SDK 2.x:

String bucketName = "arn:aws:s3:<bucket-region>:<accountNumber>:accesspoint:<access-point-name>";
String filePath = "/Users/user/sample.txt";
String key = Paths.get(filePath).getFileName().toString();
S3Client s3Client = S3Client
   .builder()
   .region(Region.<bucket-region>)
   .build();
try {
    PutObjectRequest putObjectRequest = PutObjectRequest
       .builder()
       .bucket(bucketName)
       .key(key)
       .build();
    PutObjectResponse putObjectResponse = s3Client.putObject(putObjectRequest, Paths.get(filePath));
    System.out.println(putObjectResponse.sdkHttpResponse().isSuccessful());
} catch (AmazonServiceException e) {
    // handle exception or throw
    throw e;
}
Related