Amazon Simple Storage Service
开发人员指南 (API Version 2006-03-01)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。点 击 Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

使用 适用于 .NET 的 AWS 开发工具包 复制对象

以下任务将引导您使用高级别的 .NET 类来上传文件。API 提供了 Upload 方法的多个变体 (重载),使您可以轻松地上传数据。

复制对象

1

创建 AmazonS3 类的实例。

2

执行 AmazonS3.CopyObject 之一。您需要提供诸如源存储桶、源键名称、目标存储桶和目标键名称等信息。您可以通过创建 CopyObjectRequest 类的实例提供此信息。

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

Copy
static IAmazonS3 client; client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1); CopyObjectRequest request = new CopyObjectRequest() { SourceBucket = bucketName, SourceKey = objectKey, DestinationBucket = bucketName, DestinationKey = destObjectKey }; CopyObjectResponse response = client.CopyObject(request);

下面的 C# 代码示例将复制对象。您需要更新代码,提供存储桶名称和对象键。有关如何创建和测试有效示例的说明,请参阅 运行 Amazon S3 .NET 代码示例

Copy
using System; using Amazon.S3; using Amazon.S3.Model; namespace s3.amazon.com.docsamples { class CopyObject { static string sourceBucket = "*** Bucket on which to enable logging ***"; static string destinationBucket = "*** Bucket where you want logs stored ***"; static string objectKey = "*** Provide key name ***"; static string destObjectKey = "*** Provide destination key name ***"; static IAmazonS3 client; public static void Main(string[] args) { using (client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1)) { Console.WriteLine("Copying an object"); CopyingObject(); } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } static void CopyingObject() { try { CopyObjectRequest request = new CopyObjectRequest { SourceBucket = sourceBucket, SourceKey = objectKey, DestinationBucket = destinationBucket, DestinationKey = destObjectKey }; CopyObjectResponse response = client.CopyObject(request); } catch (AmazonS3Exception s3Exception) { Console.WriteLine(s3Exception.Message, s3Exception.InnerException); } } } }