Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy an Amazon s3 object to another folder in another bucket.

Tags:

java

amazon-s3

I want to copy all the data from on bucket to another directory in another bucket on amazon s3 with java.

Like I have a bucket name sourceBucket and a destBucket and there is another directory in destBucket. i want to copy all the data from sourceBucket to that directory. And i am using java. I am unable to find the solution

like image 674
ap.singh Avatar asked Jan 12 '23 07:01

ap.singh


2 Answers

Try :

ObjectListing objectListing = s3.listObjects(new ListObjectsRequest()
                                .withBucketName(bucketName)
                                .withPrefix(""));

for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {s3client.copyObject(sourceBucketName, objectSummary.getKey(), 
                    destinationBucketName, objectSummary.getKey());}
like image 54
Aneesh Avatar answered Feb 06 '23 22:02

Aneesh


s3 doesn't have folders. The "structure" you see is a key. So just copy your objects with proper keys. From Programmer Guide: Code Samples:

S3Object[] filteredObjects = s3Service.listObjects("sourceBucket", "appData/", null);
for(S3Object object: filteredObjects ){
    s3Service.copyObject("sourceBucket", "newAppData/" + object.getKey().substring(object.getKey().indexOf("/"), "destBucket", object, false);
}
like image 45
agad Avatar answered Feb 07 '23 00:02

agad