Cherry
Cherry

Reputation: 33554

How enable force global bucket access in aws s3 sdk java 2.0?

Here is a link to the documentation for java 3 sdk version 1. Does version 2.0 has something similar or they removed such option?

Upvotes: 6

Views: 4098

Answers (3)

Mark
Mark

Reputation: 2428

This works for me when using java AWS SDK 2.16.98 and only requires the name of the bucket rather than the full arn.

private S3Client defaultClient;
private S3Client bucketSpecificClient;
private String bucketName = "my-bucket-in-some-region";

// this client seems to be able to look up the location of buckets from any region
defaultClient = S3Client.builder().endpointOverride(URI.create("https://s3.us-east-1.amazonaws.com")).region(Region.US_EAST_1).build();

public S3Client getClient() {
    if (bucketSpecificClient == null) {
        String bucketLocation = defaultClient.getBucketLocation(builder -> builder.bucket(this.bucketName)).locationConstraintAsString();

        Region region = bucketLocation.trim().equals("") ? Region.US_EAST_1 : Region.of(bucketLocation);
        bucketSpecificClient = S3Client.builder().region(region).build();
    }

    return bucketSpecificClient;
}

Now you can use bucketSpecificClient to perform operations on objects in the bucket my-bucket-in-some-region

Upvotes: 0

Amina
Amina

Reputation: 21

Yes! It is possible in AWS SDK v2 to execute S3 operations on regions other than the one configured in the client.

In order to do this, set useArnRegionEnabled to true on the client.

An example of this using Scala is:

  val s3Configuration = S3Configuration.builder.useArnRegionEnabled(true).build

  val client = S3Client
    .builder
    .credentialsProvider({$foo})
    .region(Region.EU_WEST_1)
    .overrideConfiguration({$foo})
    .serviceConfiguration(s3Configuration)
    .build

Here is the documentation: https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/s3/S3Configuration.Builder.html#useArnRegionEnabled-java.lang.Boolean-

Upvotes: 2

SKradel
SKradel

Reputation: 138

Not supported per here

In version 1.x, services such as Amazon S3, Amazon SNS, and Amazon SQS allowed access to resources across Region boundaries. This is no longer allowed in version 2.x using the same client. If you need to access a resource in a different region, you must create a client in that region and retrieve the resource using the appropriate client.

Upvotes: 0

Related Questions