Amazon Simple Storage Service
开发人员指南 (API Version 2006-03-01)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。请点击 Amazon AWS 入门,可查看中国地区的具体差异

使用适用于 .NET 的 AWS 开发工具包分段上传 API 复制对象

下面的任务将指导您使用 .NET 开发工具包将 Amazon S3 对象从一个源位置复制到另一个位置 (例如,从一个存储桶到另一个存储桶) 的过程。您可以使用此处演示的代码复制大于 5 GB 的对象。对于小于 5 GB 的对象,请使用单个操作复制,如 使用 适用于 .NET 的 AWS 开发工具包 复制对象 中所述。

复制对象

1

通过提供 AWS 凭证创建 AmazonS3Client 类的实例。

2

通过执行 AmazonS3Client.InitiateMultipartUpload 方法启动分段复制。创建 InitiateMultipartUploadRequest 的实例。您需要提供存储桶名称和键名称。

3

AmazonS3Client.InitiateMultipartUpload 方法返回的响应对象保存上传 ID。您将需要为随后的每个分段上传操作提供此上传 ID。

4

复制所有分段。对于每个分段复制,创建 CopyPartRequest 类的新实例并提供分段信息,包括源存储桶、目标存储桶、对象键、上传 ID、分段的第一个字节、分段的最后一个字节以及分段编号。

5

CopyPartRequest 方法的响应保存在列表中。此响应包括完成分段上传时您将需要的 ETag 值和分段编号。

6

为每个分段重复任务 4 和 5。

7

执行 AmazonS3Client.CompleteMultipartUpload 方法以完成复制。

以下 C# 代码示例演示了上述任务。

// 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); }

下面的 C# 代码示例将对象从一个 Amazon S3 存储桶复制到另一个存储桶。有关如何创建和测试有效示例的说明,请参阅 运行 Amazon S3 .NET 代码示例

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; } } }