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

Copy an Object Using the AWS SDK for .NET Multipart Upload API

The following task guides you through using the .NET 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 that are 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 .NET.

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 the InitiateMultipartUploadRequest. You will need to provide a bucket name and 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 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 C# code sample demonstrates the preceding tasks.

Copy
// Step 1. Create instance and provide credentials. IAmazonS3 s3Client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1); // List to store upload part responses. List<UploadPartResponse> uploadResponses = new List<UploadPartResponse>(); List<CopyPartResponse> copyResponses = new List<CopyPartResponse>(); InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest { BucketName = targetBucket, Key = targetObjectKey }; // Step 2. Initialize. InitiateMultipartUploadResponse initResponse = s3Client.InitiateMultipartUpload(initiateRequest); // Step 3. Save Upload Id. String uploadId = initResponse.UploadId; try { // Get object size. GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest { BucketName = sourceBucket, Key = sourceObjectKey }; GetObjectMetadataResponse metadataResponse = s3Client.GetObjectMetadata(metadataRequest); long objectSize = metadataResponse.ContentLength; // in bytes // Copy parts. long partSize = 5 * (long)Math.Pow(2, 20); // 5 MB long bytePosition = 0; for (int i = 1; bytePosition < objectSize; i++) { CopyPartRequest copyRequest = new CopyPartRequest { DestinationBucket = targetBucket, DestinationKey = targetObjectKey, SourceBucket = sourceBucket, SourceKey = sourceObjectKey, UploadId = uploadId, FirstByte = bytePosition, LastByte = bytePosition + partSize - 1 >= objectSize ? objectSize - 1 : bytePosition + partSize - 1, PartNumber = i }; copyResponses.Add(s3Client.CopyPart(copyRequest)); bytePosition += partSize; } CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest { BucketName = targetBucket, Key = targetObjectKey, UploadId = initResponse.UploadId }; completeRequest.AddPartETags(copyResponses); CompleteMultipartUploadResponse completeUploadResponse = s3Client.CompleteMultipartUpload(completeRequest); } catch (Exception e) { Console.WriteLine(e.Message); }

Example

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

Copy
using System; using System.Collections.Generic; using Amazon.S3; using Amazon.S3.Model; namespace s3.amazon.com.docsamples { class CopyObjectUsingMPUapi { static string sourceBucket = "*** Source bucket name ***"; static string targetBucket = "*** Target bucket name ***"; static string sourceObjectKey = "*** Source object key ***"; static string targetObjectKey = "*** Target object key ***"; static void Main(string[] args) { IAmazonS3 s3Client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1); // List to store upload part responses. List<UploadPartResponse> uploadResponses = new List<UploadPartResponse>(); List<CopyPartResponse> copyResponses = new List<CopyPartResponse>(); InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest { BucketName = targetBucket, Key = targetObjectKey }; InitiateMultipartUploadResponse initResponse = s3Client.InitiateMultipartUpload(initiateRequest); String uploadId = initResponse.UploadId; try { // Get object size. GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest { BucketName = sourceBucket, Key = sourceObjectKey }; GetObjectMetadataResponse metadataResponse = s3Client.GetObjectMetadata(metadataRequest); long objectSize = metadataResponse.ContentLength; // in bytes // Copy parts. long partSize = 5 * (long)Math.Pow(2, 20); // 5 MB long bytePosition = 0; for (int i = 1; bytePosition < objectSize; i++) { CopyPartRequest copyRequest = new CopyPartRequest { DestinationBucket = targetBucket, DestinationKey = targetObjectKey, SourceBucket = sourceBucket, SourceKey = sourceObjectKey, UploadId = uploadId, FirstByte = bytePosition, LastByte = bytePosition + partSize - 1 >= objectSize ? objectSize - 1 : bytePosition + partSize - 1, PartNumber = i }; copyResponses.Add(s3Client.CopyPart(copyRequest)); bytePosition += partSize; } CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest { BucketName = targetBucket, Key = targetObjectKey, UploadId = initResponse.UploadId }; completeRequest.AddPartETags(copyResponses); CompleteMultipartUploadResponse completeUploadResponse = s3Client.CompleteMultipartUpload(completeRequest); } catch (Exception e) { Console.WriteLine(e.Message); } } // Helper function that constructs ETags. static List<PartETag> GetETags(List<CopyPartResponse> responses) { List<PartETag> etags = new List<PartETag>(); foreach (CopyPartResponse response in responses) { etags.Add(new PartETag(response.PartNumber, response.ETag)); } return etags; } } }