Menu
Amazon Simple Storage Service
Developer Guide (API Version 2006-03-01)

Copy an Object Using the AWS SDK for Java Multipart Upload API

The following task guides you through using the Java SDK to copy an Amazon S3 object from one source location to another, such as from one bucket to another. You can use the code demonstrated here to copy objects greater than 5 GB. For objects less than 5 GB, use the single operation copy described in Copy an Object Using the AWS SDK for Java.

Copying Objects

1

Create an instance of the AmazonS3Client class by providing your AWS credentials.

2

Initiate a multipart copy by executing the AmazonS3Client.initiateMultipartUpload method. Create an instance of InitiateMultipartUploadRequest. You will need to provide a bucket name and a key name.

3

Save the upload ID from the response object that the AmazonS3Client.initiateMultipartUpload method returns. You will need to provide this upload ID for each subsequent multipart upload operation.

4

Copy all the parts. For each part copy, create a new instance of the CopyPartRequest class and provide part information including source bucket, destination bucket, object key, uploadID, first byte of the part, last byte of the part, and the part number.

5

Save the response of the CopyPartRequest method in a list. The response includes the ETag value and the part number. You will need the part number to complete the multipart upload.

6

Repeat tasks 4 and 5 for each part.

7

Execute the AmazonS3Client.completeMultipartUpload method to complete the copy.

The following Java code sample demonstrates the preceding tasks.

Copy
// Step 1: Create instance and provide credentials. AmazonS3Client s3Client = new AmazonS3Client(new PropertiesCredentials( LowLevel_LargeObjectCopy.class.getResourceAsStream( "AwsCredentials.properties"))); // Create lists to hold copy responses List<CopyPartResult> copyResponses = new ArrayList<CopyPartResult>(); // Step 2: Initialize InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest(targetBucketName, targetObjectKey); InitiateMultipartUploadResult initResult = s3Client.initiateMultipartUpload(initiateRequest); // Step 3: Save upload Id. String uploadId = initResult.getUploadId(); try { // Get object size. GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest(sourceBucketName, sourceObjectKey); ObjectMetadata metadataResult = s3Client.getObjectMetadata(metadataRequest); long objectSize = metadataResult.getContentLength(); // in bytes // Step 4. Copy parts. long partSize = 5 * (long)Math.pow(2.0, 20.0); // 5 MB long bytePosition = 0; for (int i = 1; bytePosition < objectSize; i++) { // Step 5. Save copy response. CopyPartRequest copyRequest = new CopyPartRequest() .withDestinationBucketName(targetBucketName) .withDestinationKey(targetObjectKey) .withSourceBucketName(sourceBucketName) .withSourceKey(sourceObjectKey) .withUploadId(initResult.getUploadId()) .withFirstByte(bytePosition) .withLastByte(bytePosition + partSize -1 >= objectSize ? objectSize - 1 : bytePosition + partSize - 1) .withPartNumber(i); copyResponses.add(s3Client.copyPart(copyRequest)); bytePosition += partSize; } // Step 7. Complete copy operation. CompleteMultipartUploadResult completeUploadResponse = s3Client.completeMultipartUpload(completeRequest); } catch (Exception e) { System.out.println(e.getMessage()); }

Example

The following Java code example copies an object from one Amazon S3 bucket to another. For instructions on how to create and test a working sample, see Testing the Java Code Examples.

Copy
import java.io.IOException; import java.util.ArrayList; import java.util.List; import com.amazonaws.auth.PropertiesCredentials; import com.amazonaws.services.s3.*; import com.amazonaws.services.s3.model.*; public class LowLevel_LargeObjectCopy { public static void main(String[] args) throws IOException { String sourceBucketName = "*** Source-Bucket-Name ***"; String targetBucketName = "*** Target-Bucket-Name ***"; String sourceObjectKey = "*** Source-Object-Key ***"; String targetObjectKey = "*** Target-Object-Key ***"; AmazonS3Client s3Client = new AmazonS3Client(new PropertiesCredentials( LowLevel_LargeObjectCopy.class.getResourceAsStream( "AwsCredentials.properties"))); // List to store copy part responses. List<CopyPartResult> copyResponses = new ArrayList<CopyPartResult>(); InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest(targetBucketName, targetObjectKey); InitiateMultipartUploadResult initResult = s3Client.initiateMultipartUpload(initiateRequest); try { // Get object size. GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest(sourceBucketName, sourceObjectKey); ObjectMetadata metadataResult = s3Client.getObjectMetadata(metadataRequest); long objectSize = metadataResult.getContentLength(); // in bytes // Copy parts. long partSize = 5 * (long)Math.pow(2.0, 20.0); // 5 MB long bytePosition = 0; for (int i = 1; bytePosition < objectSize; i++) { CopyPartRequest copyRequest = new CopyPartRequest() .withDestinationBucketName(targetBucketName) .withDestinationKey(targetObjectKey) .withSourceBucketName(sourceBucketName) .withSourceKey(sourceObjectKey) .withUploadId(initResult.getUploadId()) .withFirstByte(bytePosition) .withLastByte(bytePosition + partSize -1 >= objectSize ? objectSize - 1 : bytePosition + partSize - 1) .withPartNumber(i); copyResponses.add(s3Client.copyPart(copyRequest)); bytePosition += partSize; } CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest( targetBucketName, targetObjectKey, initResult.getUploadId(), GetETags(copyResponses)); CompleteMultipartUploadResult completeUploadResponse = s3Client.completeMultipartUpload(completeRequest); } catch (Exception e) { System.out.println(e.getMessage()); } } // Helper function that constructs ETags. static List<PartETag> GetETags(List<CopyPartResult> responses) { List<PartETag> etags = new ArrayList<PartETag>(); for (CopyPartResult response : responses) { etags.add(new PartETag(response.getPartNumber(), response.getETag())); } return etags; } }