How to specify region name in AWS S3 Java API?

Viewed 2768

I am using AWS Java API for S3. The access key, secret key, bucket name, and region name are all provided to my application as environment variables.

I am not sure how to set the region name...I'm currently getting the AmazonS3Client

return new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey))

Can someone please tell me how to set the region name?

3 Answers

You can do it by client.setRegion(region) something like:

AmazonS3Client client = new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey)); client.setRegion(region); return client;

Alternatively, new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey)) picks the Region information from the following, if you haven't provided (it tries searching from 1 to 3) :

1.) EnvironmentVariableCredentialsProvider()

2.) SystemPropertiesCredentialsProvider()

3.) EC2 instance

But this method is deprecated now, you should use now:

AmazonS3 amazonS3 = AmazonS3ClientBuilder.standard() .withRegion(Regions.US_EAST_1) .build();

Related