-1

I am trying to use my code to access S3 using aws sdk and in a java service. But I receiving following exception.

Code I use to as follows.

  1. configure the builder.

    ClientConfiguration clientConfiguration = new ClientConfiguration();
    clientConfiguration.setRetryPolicy( PredefinedRetryPolicies.getDefaultRetryPolicyWithCustomMaxRetries(5));
    
     AmazonS3ClientBuilder s3ClientCommonBuilder = AmazonS3ClientBuilder.standard()
         .withClientConfiguration(clientConfiguration)
         .withForceGlobalBucketAccessEnabled(true)
         .withPathStyleAccessEnabled(true);
    
         s3ClientCommonBuilder.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(awsKey, awsSecret)));
    
    
     return s3ClientCommonBuilder;
    
  2. create the AwsS3Client

     AmazonS3ClientBuilder internalS3ClientBuilder = 
     createS3ClientCommonBuilder(s3bucket, awsKey, awsSecret);
     if (internalServiceEndpoint != null && !internalServiceEndpoint.isEmpty()) {
         internalS3ClientBuilder.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(internalServiceEndpoint, Region.EU_Ireland.getFirstRegionId()));
     }
    

And then at run time when the client is attempting to connect to s3 following exception is thrown.

1) Error in custom provider, 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.
while locating AwsS3ClientProvider
at org.test.ApiServerBootstrap.configure(ApiServerBootstrap.java:103)
while locating org.test.AWSS3Client
for the 3rd parameter of org.testDataUploader.<init>(DataUploader.java:60)
at org.test.DataUploader.class(DataUploader.java:49)
while locating org.test.DataUploader
for the 3rd parameter of org.test.StoreService.<init>(StoreService.java:56)
at org.test.services.v3.StoreService.class(StoreService.java:53)
while locating org.test.StoreService
for the 1st parameter of org.test.verticles.EventBusConsumerVerticle.<init> 
(EventBusConsumerVerticle.java:35)
at 
org.test.verticles.EventBusConsumerVerticle.class(EventBusConsumerVerticle.java:35)
while locating org.test.verticles.EventBusConsumerVerticle
for the 3rd parameter of org.test.ApiServer.<init>(ApiServer.java:52)
while locating org.test.ApiServer

1 error
at com.google.inject.internal.InternalProvisionException.toProvisionException(InternalProvisionException.java:226)
at com.google.inject.internal.InjectorImpl$1.get(InjectorImpl.java:1053)
at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1086)
at org.test.ApiServer.main(ApiServer.java:46)
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.AwsSyncClientBuilder.build(AwsSyncClientBuilder.java:46)
at org.test.AWSS3Client.create(AWSS3Client.java:99)

I have tested this with some other computers to verify it is my aws client configuration that is is wrong.

My .aws/config file looks as follows

$ cat .aws/config 
[default]
region = eu-west-1
output = json

my .aws/credintials look like follows.

$ cat .aws/credentials 
[default]
aws_access_key_id=XXXXXXXX.....
aws_secret_access_key=XXXXXXXX

my aws client verison is as follows.

$ aws --version
aws-cli/2.2.40 Python/3.8.8 Linux/5.11.0-34-generic exe/x86_64.ubuntu.20 prompt/off

I appreciate if some one can figure out what am I missing for the aws client to connect to s3.

Tharanga
  • 2,007
  • 4
  • 32
  • 50
  • Is your question about the AWS SDK for Java, which is where the error is from, or the AWS CLI which you actually provide details for? – Mark B Sep 22 '21 at 14:27
  • Exception is from AWS sdk for java. As I have mentioned this code is successfully build in a Ubuntu 16.04 Jenkins server without any errors which as identical aws credentials and config files to mine(I am using ubuntu 20.04 BTW). Only difference is Jenkins server has aws client 1.X installed where as in mine 2.2.X – Tharanga Sep 22 '21 at 21:28
  • The AWS CLI client installed on the server is completely irrelevant. That is not used by the SDK. Also this isn't a build time error, it is a runtime error, so saying it is built without errors on another server doesn't seem relevant. – Mark B Sep 22 '21 at 21:31
  • Thanks for the feed back. It looks my question is not asked properly. Updated the exact issue. @MarkB – Tharanga Sep 23 '21 at 05:55

1 Answers1

0

You want to make sure you are using the DefaultAWSCredentialsProviderChain() class when building your S3Client.

Also you can create an environment variable AWS_REGION=eu-west-1

David Webster
  • 2,208
  • 1
  • 16
  • 27
  • Your question is specific to using the SDK as you show a Java code exception. For the CLI the two parameters to set are AWS_REGION and AWS_DEFAULT_REGION. – David Webster Sep 22 '21 at 14:22