codeLearner
codeLearner

Reputation: 53

Setting AWS S3 Region

I am trying to create an aws s3 bucket using the following java code.

AmazonS3 s3client = AmazonS3ClientBuilder.defaultClient();
    s3client.setRegion(Region.getRegion(Regions.AP_SOUTH_1));

But I am getting the following error:

"exception": "com.amazonaws.SdkClientException", "message": "Unable to find a region via the region provider chain. Must provide an explicit region in the builder or setup environment to supply a region."

Am I trying to set region in an incorrect way? Please advice.

Upvotes: 1

Views: 6342

Answers (2)

Rodel
Rodel

Reputation: 553

If you are not using any proxies and you already setup your credentials, you can use below code:

AmazonS3 s3client = AmazonS3ClientBuilder.standard()
.withRegion(Region.getRegion(Regions.AP_SOUTH_1));

But if you need to setup a proxy and manually set the credentials, you can use below code:

AWSCredentials cred = new BasicAWSCredentials(<accessKey>,<secretKey>);
AmazonS3 s3client = AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(cred))
.withClientConfiguration(<your configuration>)
.withRegion(Region.getRegion(Regions.AP_SOUTH_1));

Upvotes: 1

Ali
Ali

Reputation: 434

The reason you are getting the error is you have not setup AWS with Eclipse.

If you are using Eclipse as your IDE then read: http://docs.aws.amazon.com/toolkit-for-eclipse/v1/user-guide/welcome.html

Once the profile is setup then

AmazonS3 s3 = new AmazonS3Client(new ProfileCredentialsProvider());

Region apSouth1 = Region.getRegion(Regions.AP_SOUTH_1);

s3.setRegion(apSouth1);

Also make sure to import:

import com.amazonaws.regions.Region;

import com.amazonaws.regions.Regions;

Upvotes: 0

Related Questions