Reputation: 7647
I have below method to return AmazonS3 for upload documents. In local env, I have to connect to a s3 bucket in a different region but in other environments the s3 bucket and the application code is same aws region.
public AmazonS3 getAmazonS3Client() {
if ("local".equals(hostEnvironment)) {
final AssumeRoleRequest roleRequest = new AssumeRoleRequest()
.withRoleArn("arnrole").withRoleSessionName("s3Session");
final AssumeRoleResult assumeRoleResult = AWSSecurityTokenServiceAsyncClientBuilder.defaultClient()
.assumeRole(roleRequest);
final Credentials sessionCredentials = assumeRoleResult.getCredentials();
final BasicSessionCredentials basicSessionCredentials = new BasicSessionCredentials(
sessionCredentials.getAccessKeyId(), sessionCredentials.getSecretAccessKey(),
sessionCredentials.getSessionToken());
return AmazonS3Client.builder().withRegion("us-east-2").withCredentials
(new AWSStaticCredentialsProvider(basicSessionCredentials)).build();
} else {
return AmazonS3Client.builder().withRegion("us-east-2").withCredentials
(new InstanceProfileCredentialsProvider(true)).build();
}
}
I am getting below exception when running from local, what am I missing here?
Caused by: com.amazonaws.SdkClientException: 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. at com.amazonaws.client.builder.AwsClientBuilder.setRegion(AwsClientBuilder.java:462) at com.amazonaws.client.builder.AwsClientBuilder.configureMutableProperties(AwsClientBuilder.java:424) at com.amazonaws.client.builder.AwsAsyncClientBuilder.build(AwsAsyncClientBuilder.java:80) at com.amazonaws.services.securitytoken.AWSSecurityTokenServiceAsyncClientBuilder.defaultClient(AWSSecurityTokenServiceAsyncClientBuilder.java:45)
Upvotes: 2
Views: 5028
Reputation: 7647
After I set the region to AmazonS3Client,this works
AmazonS3Client amazonS3 = new AmazonS3Client(basicSessionCredentials); amazonS3.setRegion(RegionUtils.getRegion("us-east-2"));
Upvotes: 1