0
 StaticCredentialsProvider provider = StaticCredentialsProvider.create(AwsBasicCredentials.create("Access key ID", "Access secret key"));
S3Client s3 = S3Client.builder().credentialsProvider(provider).build())
List<Bucket> buckets = s3.listBuckets().buckets();
for (Bucket bucket : buckets) {
  System.out.println("bucket name: " + bucket.name());
}

I am trying this code to connect to S3 storage to get the bucket details but always getting the error Unable to load region from any of the providers in the chain software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain. Not sure what is wrong here. Please help.

1

1 Answer 1

0

Simply you need to tell the S3 service what region your buckets reside in:

StaticCredentialsProvider provider = StaticCredentialsProvider.create(
        AwsBasicCredentials.create("Access key ID", "Access secret key"));

S3Client s3 = S3Client.builder().credentialsProvider(provider)
        .region(Region.EU_WEST_2)  // <<<<<<<<<<<<<<<<<<<<<<<<< you need a region here
        .build();

List<Bucket> buckets = s3.listBuckets().buckets();
for (Bucket bucket : buckets) {
    System.out.println("bucket name: " + bucket.name());
}
Sign up to request clarification or add additional context in comments.

7 Comments

Hello Andrew, Thanks for your reply. Here, i don't want to mention the specific region here. wanted to connect the default region of my account.
BTW, i am not using the amazon S3 but using a S3 compatible service provider
"wanted to connect the default region of my account" - yes! but how does the S3 library know this when you use the explicit StaticCredentialsProvider? When using CLI for AWS, for example, you ALSO have define a default. This is implicilty found in ~/.aws/config. (Other CredentialsProviders are available that use environment variables to acquire such default from configuration files like the CLI uses.)
Thanks for your response. I am able to get it work with the following code. **bold S3Client s3Client = S3Client.builder() .endpointProvider(new S3EndpointProvider() { @Override public CompletableFuture<Endpoint> resolveEndpoint(S3EndpointParams endpointParams) { return CompletableFuture.completedFuture(Endpoint.builder() .url(URI.create("x2u2.or.idrivee2-61.com")) .build()); } }).region(Region.US_EAST_1) .credentialsProvider(() -> AwsBasicCredentials.create("", "")) .build();
However, i am getting the below error while creating the bucket. An error occurred when parsing the HTTP request PUT at '/' Bucket creation code : CreateBucketRequest bucketRequest = CreateBucketRequest.builder() .bucket(bucketName) .build(); s3Client.createBucket(bucketRequest);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.