Cross-Region access for Amazon S3
When you work with Amazon Simple Storage Service (Amazon S3) buckets, you usually know the AWS Region for the bucket. The Region you work with is determined when you create the S3 client.
However, sometimes you might need to work with a specific bucket, but you don't know if it's located in the same Region that's set for the S3 client.
Instead of making more calls to determine the bucket Region, you can use the SDK to enable access to S3 buckets across different Regions.
Setup
Support for cross-Region access became available with version 2.20.111
of
the SDK. Use this version or a later one in your Maven build file for the s3
dependency as shown in the following snippet.
<dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>s3</artifactId> <version>
2.27.21
</version> </dependency>
Next, when you create your S3 client, enable cross-Region access as shown in the snippet. By default, access is not enabled.
S3AsyncClient client = S3AsyncClient.builder() .crossRegionAccessEnabled(true) .build();
How the SDK provides cross-Region access
When you reference an existing bucket in a request, such as when you use the
putObject
method, the SDK initiates a request to the Region configured for
the client.
If the bucket does not exist in that specific Region, the error response includes the actual Region where the bucket resides. The SDK then uses the correct Region in a second request.
To optimize future requests to the same bucket, the SDK caches this Region mapping in the client.
Considerations
When you enable cross-Region bucket access, be aware that the first API call might result in increased latency if the bucket isn't in the client's configured Region. However, subsequent calls benefit from cached Region information, resulting in improved performance.
When you enable cross-Region access, access to the bucket is not affected. The user must be authorized to access the bucket in whatever Region it resides.