the below code copies the file from one region to another region. If the two different region shares same access and secret key
System.setProperty(SDKGlobalConfiguration.DISABLE_CERT_CHECKING_SYSTEM_PROPERTY, "true");
AmazonS3 s3ClientBuilder = null;
AmazonS3 s3desClientBuilder = null;
TransferManager transferManager = null;
try {
ClientConfiguration clientCfg = new ClientConfiguration();
clientCfg.setProtocol(Protocol.HTTPS);
clientCfg.setSignerOverride("S3SignerType");
AWSStaticCredentialsProvider credentialProvidor = new AWSStaticCredentialsProvider(
new BasicAWSCredentials(accessKey, secretKey));
s3ClientBuilder = AmazonS3ClientBuilder//
.standard()//
.withCredentials(credentialProvidor)//
.withEndpointConfiguration(new EndpointConfiguration(s3Endpoint, region.getName()))//
.withClientConfiguration(clientCfg)//
.build();
//list all bucket names in the s3region
List<Bucket> buckets = s3ClientBuilder.listBuckets();
System.out.println("Your Amazon S3 buckets are:");
for (Bucket b : buckets) {
System.out.println("* " + b.getName());
}
//List object and objectkey inside the bucket
ListObjectsRequest lor = new ListObjectsRequest()
.withBucketName(SOURCE_BUCKET_NAME)
.withPrefix("vivek/20210801");
ObjectListing objectListing = s3ClientBuilder.listObjects(lor);
for (S3ObjectSummary summary: objectListing.getObjectSummaries()) {
SOURCE_KEY=summary.getKey();
DESTINATION_KEY=SOURCE_KEY
s3desClientBuilder = AmazonS3ClientBuilder//
.standard()//
.withCredentials(credentialProvidor)//
.withEndpointConfiguration(new EndpointConfiguration(s3desEndpoint, region.getName()))//
.withClientConfiguration(clientCfg)//
.build();
transferManager = TransferManagerBuilder.standard()
.withS3Client(s3desClientBuilder)
.build();
Copy copy = transferManager.copy(new CopyObjectRequest(SOURCE_BUCKET_NAME, SOURCE_KEY,
DESTINATION_BUCKET_NAME, DESTINATION_KEY),
s3ClientBuilder, null);
copy.waitForCopyResult();
}
transferManager.shutdownNow();
s3ClientBuilder.shutdown();
s3desClientBuilder.shutdown();