How to copy object from one S3 bucket to another using Java – AWS S3 Copy Object

2 weeks ago Lalit Bhagtani 0

AWS S3 Copy Object – In this tutorial, we will learn about how to copy an object from one S3 bucket to another S3 bucket using java language.

Project Setup

Create a simple maven project in your favorite IDE and add below mentioned dependency in your pom.xml file.

<dependency>
  <groupId>com.amazonaws</groupId>
  <artifactId>aws-java-sdk-s3</artifactId>
  <version>1.11.533</version>
</dependency>

For latest version of aws library, check this page.

S3 Connection

Create an object of AmazonS3 ( com.amazonaws.services.s3.AmazonS3 ) class for sending a client request to S3. To get instance of this class, we will use AmazonS3ClientBuilder builder class. It requires three important parameters :- 

  1. Region :- It is a region where S3 table will be stored.
  2. ACCESS_KEY :- It is a access key for using S3. You can generate this key, using aws management console.
  3. SECRET_KEY :- It is a secret key of above mentioned access key.

Here is a code example :-

AmazonS3 s3 = AmazonS3ClientBuilder.standard()
				.withRegion(Regions.AP_SOUTH_1)
				.withCredentials(new AWSStaticCredentialsProvider
                                                    (new BasicAWSCredentials("ACCESS_KEY","SECRET_KEY")))
				.build();

Copy Object

An AmazonS3.copyObject method copies an object from one S3 bucket to another S3 bucket. A source bucket name and object key, along with destination bucket name and object key are only information required for copying the object. While sending the copy object request, new Object metadata and Canned ACL can also be specified for new Object otherwise Object metadata and Canned ACL of an old object will be used. Steps to create and send CopyObjectRequest to S3 are as follows:-

  1. Instantiate CopyObjectRequest object by passing source bucket name, source Object Key, destination bucket name and destination Object Key.
  2. Set storage class as StandardInfrequentAccess.
  3. Set canned access control list as BucketOwnerFullControl, this provides full control to bucket owner.
  4. Create and Set new Object Metadata.
  5. Create and Set new Object Tag.
  6. Invoke the copyObject method on AmazonS3 object by passing CopyObjectRequest object as an argument.

Example

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.CopyObjectRequest;
import com.amazonaws.services.s3.model.CopyObjectResult;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.ObjectTagging;
import com.amazonaws.services.s3.model.StorageClass;
import com.amazonaws.services.s3.model.Tag;

public class CopyObject {

	public static void main(String[] args) {

		/* Create S3 Client Object */
		AmazonS3 s3 = AmazonS3ClientBuilder
				.standard()
				.withRegion(Regions.AP_SOUTH_1)
				.withCredentials(new AWSStaticCredentialsProvider(
                                     new BasicAWSCredentials("ACCESS_KEY","SECRET_KEY")))
				.build();
		
		try {
			
			/* Create an Object of CopyObjectRequest */
			/* Arguments: SourceBucketName, SourceObjectKey, DestinationBucketName, DestinationObjectKey */
			CopyObjectRequest request = new CopyObjectRequest("bucket-1", "s3.png", "bucket-2", "s3.png");
			
			/* Set StorageClass as Standard Infrequent Access */
			request.setStorageClass(StorageClass.StandardInfrequentAccess);
			
			/* Set Canned ACL as BucketOwnerFullControl */
			request.setCannedAccessControlList(CannedAccessControlList.BucketOwnerFullControl);
			
			/* Set Destination Metadata */
			Map<String, String> map = new HashMap<>();
			map.put("location", "London");
			
			ObjectMetadata metadata = new ObjectMetadata();
			metadata.setUserMetadata(map);
			
			request.setNewObjectMetadata(metadata);
			
			/* Set Destination Tags */
			List<Tag> tags = new ArrayList<>();
			tags.add(new Tag("aws", "s3"));
			tags.add(new Tag("imageType", "png"));
			request.setNewObjectTagging(new ObjectTagging(tags));
			
			/* Send Copy Object Request */
			CopyObjectResult result = s3.copyObject(request);
			
			System.out.println("Last Modified Date : " + result.getLastModifiedDate());
            
        } catch (AmazonServiceException e) {
        	
            System.out.println(e.getErrorMessage());
            
        } finally {
        	
        	if(s3 != null) {
        		s3.shutdown();
        	}        	
        }
	}
}

References :-

  1. CopyObjectRequest Java Docs
  2. CopyObjectResult Java Docs

That’s all for how to copy an object from one S3 bucket to another S3 bucket using java language (AWS S3 Copy). If you liked it, please share your thoughts in comments section and share it with others too.