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 的 AWS 开发工具包类在单个 HTTP 请求中删除多个对象的过程。

删除多个对象 (不受版本控制的存储桶)

1

创建 AmazonS3Client 类的实例。

2

创建 DeleteObjectsRequest 类的实例,并提供您要删除的对象键的列表。

3

执行 AmazonS3Client.DeleteObjects 方法。

如果一个或多个对象无法删除,Amazon S3 将引发 DeleteObjectsException

下面的 C# 代码示例演示了上述步骤。

Copy
DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(); multiObjectDeleteRequest.BucketName = bucketName; multiObjectDeleteRequest.AddKey("<object Key>", null); // version ID is null. multiObjectDeleteRequest.AddKey("<object Key>", null); multiObjectDeleteRequest.AddKey("<object Key>", null); try { DeleteObjectsResponse response = client.DeleteObjects(multiObjectDeleteRequest); Console.WriteLine("Successfully deleted all the {0} items", response.DeletedObjects.Count); } catch (DeleteObjectsException e) { // Process exception. }

DeleteObjectsRequest 还可以采用 KeyVersion 对象列表作为参数。对于不进行版本控制的存储桶,版本 ID 为空。

Copy
List<KeyVersion> keys = new List<KeyVersion>(); KeyVersion keyVersion = new KeyVersion { Key = key, VersionId = null // For buckets without versioning. }; keys.Add(keyVersion); List<KeyVersion> keys = new List<KeyVersion>(); ... DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest { BucketName = bucketName, Objects = keys // This includes the object keys and null version IDs. };

在发生异常的情况下,您可以查看 DeleteObjectsException 以确定哪些对象未能删除及其原因,如下面的 C# 代码示例中所示。

Copy
DeleteObjectsResponse errorResponse = e.Response; Console.WriteLine("No. of objects successfully deleted = {0}", errorResponse.DeletedObjects.Count); Console.WriteLine("No. of objects failed to delete = {0}", errorResponse.DeleteErrors.Count); Console.WriteLine("Printing error data..."); foreach (DeleteError deleteError in errorResponse.DeleteErrors) { Console.WriteLine("Object Key: {0}\t{1}\t{2}", deleteError.Key, deleteError.Code, deleteError.Message); }

下面的任务将引导您完成从受版本控制的存储桶删除对象的过程。

删除多个对象 (启用了版本的存储桶)

1

创建 AmazonS3Client 类的实例。

2

创建 DeleteObjectsRequest 类的实例并提供对象键的列表,同时选择性地提供要删除的对象的版本 ID。

如果您指定了要删除的对象的版本 ID,Amazon S3 将删除特定的对象版本。如果您没有指定要删除的对象的版本 ID,则 Amazon S3 将添加一个删除标记。有关更多信息,请参阅 每个请求删除一个对象。

3

执行 AmazonS3Client.DeleteObjects 方法。

下面的 C# 代码示例演示了上述步骤。

Copy
List<KeyVersion> keysAndVersions = new List<KeyVersion>(); // provide a list of object keys and versions. DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest { BucketName = bucketName, Objects = keysAndVersions }; try { DeleteObjectsResponse response = client.DeleteObjects(multiObjectDeleteRequest); Console.WriteLine("Successfully deleted all the {0} items", response.DeletedObjects.Count); } catch (DeleteObjectsException e) { // Process exception. }

例 1:多对象删除 (不受版本控制的存储桶)

下面的 C# 代码示例使用多对象 API 来从未受版本控制的存储桶删除对象。该示例首先将示例对象上传到存储桶,然后使用 DeleteObjects 方法删除单个请求中的对象。在 DeleteObjectsRequest 中,该示例将仅指定对象键名称,因为版本 ID 为空。

有关如何创建和测试有效示例的信息,请参阅 运行 Amazon S3 .NET 代码示例

Copy
using System; using System.Collections.Generic; using Amazon.S3; using Amazon.S3.Model; namespace s3.amazon.com.docsamples { class DeleteMultipleObjects { static string bucketName = "*** Provide a bucket name ***"; static IAmazonS3 client; public static void Main(string[] args) { using (client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1)) { var keysAndVersions = PutObjects(3); // Delete the objects. MultiObjectDelete(keysAndVersions); } Console.WriteLine("Click ENTER to continue....."); Console.ReadLine(); } static void MultiObjectDelete(List<KeyVersion> keys) { // a. multi-object delete by specifying the key names and version IDs. DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest { BucketName = bucketName, Objects = keys // This includes the object keys and null version IDs. }; multiObjectDeleteRequest.AddKey("AWSSDKcopy2.dll", null); try { DeleteObjectsResponse response = client.DeleteObjects(multiObjectDeleteRequest); Console.WriteLine("Successfully deleted all the {0} items", response.DeletedObjects.Count); } catch (DeleteObjectsException e) { PrintDeletionReport(e); } } private static void PrintDeletionReport(DeleteObjectsException e) { // var errorResponse = e.ErrorResponse; DeleteObjectsResponse errorResponse = e.Response; Console.WriteLine("x {0}", errorResponse.DeletedObjects.Count); Console.WriteLine("No. of objects successfully deleted = {0}", errorResponse.DeletedObjects.Count); Console.WriteLine("No. of objects failed to delete = {0}", errorResponse.DeleteErrors.Count); Console.WriteLine("Printing error data..."); foreach (DeleteError deleteError in errorResponse.DeleteErrors) { Console.WriteLine("Object Key: {0}\t{1}\t{2}", deleteError.Key, deleteError.Code, deleteError.Message); } } static List<KeyVersion> PutObjects(int number) { List<KeyVersion> keys = new List<KeyVersion>(); for (int i = 0; i < number; i++) { string key = "ExampleObject-" + new System.Random().Next(); PutObjectRequest request = new PutObjectRequest { BucketName = bucketName, Key = key, ContentBody = "This is the content body!", }; PutObjectResponse response = client.PutObject(request); KeyVersion keyVersion = new KeyVersion { Key = key, // For non-versioned bucket operations, we only need object key. // VersionId = response.VersionId }; keys.Add(keyVersion); } return keys; } } }

例 2:多对象删除 (启用了版本的存储桶)

下面的 C# 代码示例使用多对象 API 来从受版本控制的存储桶删除对象。除了演示 DeleteObjects 多对象删除 API 的用法外,它还展示了版本控制功能在受版本控制的存储桶中的工作方式。

测试示例之前,您必须创建一个示例存储桶并在示例中提供存储桶名称。可以使用 AWS 管理控制台创建存储桶。

该示例将执行以下操作:

  1. 对存储桶启用版本控制功能。

  2. 执行受版本控制的删除。

    该示例首先会上传示例对象。作为响应,Amazon S3 将为您上传的每个示例对象返回版本 ID。然后,该示例会使用多对象删除 API 删除这些对象。在请求中,它会指定对象键和版本 ID (即,受版本控制的删除)。

  3. 执行不受版本控制的删除。

    该示例将上传新的示例对象。然后,它会使用多对象 API 删除对象。但是,在请求中,它仅指定对象键。在此例中,Amazon S3 会添加删除标记并且对象会从您的存储桶中消失。

  4. 删除这些删除标记。

    为了演示删除标记的工作方式,示例将删除这些删除标记。在多对象删除请求中,示例会指定上一步骤中其在响应中收到的删除标记的对象键和版本 ID。此操作使对象再度出现在存储桶中。

有关如何创建和测试有效示例的信息,请参阅 运行 Amazon S3 .NET 代码示例

Copy
using System; using System.Collections.Generic; using Amazon.S3; using Amazon.S3.Model; namespace s3.amazon.com.docsamples { class DeleteMultipleObjectsVersionedBucket { static string bucketName = "*** Provide a bucket name ***"; static IAmazonS3 client; public static void Main(string[] args) { using (client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1)) { // 1. Enable versioning on the bucket. EnableVersioningOnBucket(bucketName); // 2a. Upload the sample objects. var keysAndVersions1 = PutObjects(3); // 2b. Delete the specific object versions. VersionedDelete(keysAndVersions1); // 3a. Upload the sample objects. var keysAndVersions2 = PutObjects(3); // 3b. Delete objects using only keys. Amazon S3 creates a delete marker and // returns its version Id in the response. List<DeletedObject> deletedObjects = NonVersionedDelete(keysAndVersions2); // 3c. Additional exercise - using a multi-object versioned delete, remove the // delete markers received in the preceding response. This results in your objects // reappearing in your bucket. RemoveMarkers(deletedObjects); } Console.WriteLine("Click ENTER to continue....."); Console.ReadLine(); } private static void PrintDeletionReport(DeleteObjectsException e) { var errorResponse = e.Response; Console.WriteLine("No. of objects successfully deleted = {0}", errorResponse.DeletedObjects.Count); Console.WriteLine("No. of objects failed to delete = {0}", errorResponse.DeleteErrors.Count); Console.WriteLine("Printing error data..."); foreach (DeleteError deleteError in errorResponse.DeleteErrors) { Console.WriteLine("Object Key: {0}\t{1}\t{2}", deleteError.Key, deleteError.Code, deleteError.Message); } } static void EnableVersioningOnBucket(string bucketName) { PutBucketVersioningRequest setBucketVersioningRequest = new PutBucketVersioningRequest { BucketName = bucketName, VersioningConfig = new S3BucketVersioningConfig { Status = VersionStatus.Enabled } }; client.PutBucketVersioning(setBucketVersioningRequest); } static void VersionedDelete(List<KeyVersion> keys) { // a. Perform a multi-object delete by specifying the key names and version IDs. DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest { BucketName = bucketName, Objects = keys // This includes the object keys and specific version IDs. }; try { Console.WriteLine("Executing VersionedDelete..."); DeleteObjectsResponse response = client.DeleteObjects(multiObjectDeleteRequest); Console.WriteLine("Successfully deleted all the {0} items", response.DeletedObjects.Count); } catch (DeleteObjectsException e) { PrintDeletionReport(e); } } static List<DeletedObject> NonVersionedDelete(List<KeyVersion> keys) { // Create a request that includes only the object key names. DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(); multiObjectDeleteRequest.BucketName = bucketName; foreach (var key in keys) { multiObjectDeleteRequest.AddKey(key.Key); } // Execute DeleteObjects - Amazon S3 add delete marker for each object // deletion. The objects disappear from your bucket. // You can verify that using the Amazon S3 console. DeleteObjectsResponse response; try { Console.WriteLine("Executing NonVersionedDelete..."); response = client.DeleteObjects(multiObjectDeleteRequest); Console.WriteLine("Successfully deleted all the {0} items", response.DeletedObjects.Count); } catch (DeleteObjectsException e) { PrintDeletionReport(e); throw; // Some deletes failed. Investigate before continuing. } // This response contains the DeletedObjects list which we use to delete the delete markers. return response.DeletedObjects; } private static void RemoveMarkers(List<DeletedObject> deletedObjects) { List<KeyVersion> keyVersionList = new List<KeyVersion>(); foreach (var deletedObject in deletedObjects) { KeyVersion keyVersion = new KeyVersion { Key = deletedObject.Key, VersionId = deletedObject.DeleteMarkerVersionId }; keyVersionList.Add(keyVersion); } // Create another request to delete the delete markers. var multiObjectDeleteRequest = new DeleteObjectsRequest { BucketName = bucketName, Objects = keyVersionList }; // Now, delete the delete marker to bring your objects back to the bucket. try { Console.WriteLine("Removing the delete markers ....."); var deleteObjectResponse = client.DeleteObjects(multiObjectDeleteRequest); Console.WriteLine("Successfully deleted all the {0} delete markers", deleteObjectResponse.DeletedObjects.Count); } catch (DeleteObjectsException e) { PrintDeletionReport(e); } } static List<KeyVersion> PutObjects(int number) { List<KeyVersion> keys = new List<KeyVersion>(); for (int i = 0; i < number; i++) { string key = "ObjectToDelete-" + new System.Random().Next(); PutObjectRequest request = new PutObjectRequest { BucketName = bucketName, Key = key, ContentBody = "This is the content body!", }; PutObjectResponse response = client.PutObject(request); KeyVersion keyVersion = new KeyVersion { Key = key, VersionId = response.VersionId }; keys.Add(keyVersion); } return keys; } } }