Amazon Simple Storage Service
开发人员指南 (API 版本 2006-03-01)
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 Amazon AWS 入门

使用适用于 .NET 的 AWS 开发工具包删除多个对象

适用于 .NET 的 AWS 开发工具包提供了用于删除多个对象的便利方法:DeleteObjects。对于要删除的每个对象,您可指定对象的键名称和版本。如果存储桶不受版本控制,您可为版本 ID 指定 null。如果出现异常,请查看 DeleteObjectsException 响应以确定哪些对象未删除以及为何如此。

例 从不受版本控制的存储桶中删除多个对象

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

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

using Amazon.S3; using Amazon.S3.Model; using System; using System.Collections.Generic; using System.Threading.Tasks; namespace Amazon.DocSamples.S3 { class DeleteMultipleObjectsNonVersionedBucketTest { private const string bucketName = "*** versioning-enabled bucket name ***"; // Specify your bucket region (an example region is shown). private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2; private static IAmazonS3 s3Client; public static void Main() { s3Client = new AmazonS3Client(bucketRegion); MultiObjectDeleteAsync().Wait(); } static async Task MultiObjectDeleteAsync() { // Create sample objects (for subsequent deletion). var keysAndVersions = await PutObjectsAsync(3); // a. multi-object delete by specifying the key names and version IDs. DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest { BucketName = bucketName, Objects = keysAndVersions // This includes the object keys and null version IDs. }; // You can add specific object key to the delete request using the .AddKey. // multiObjectDeleteRequest.AddKey("TickerReference.csv", null); try { DeleteObjectsResponse response = await s3Client.DeleteObjectsAsync(multiObjectDeleteRequest); Console.WriteLine("Successfully deleted all the {0} items", response.DeletedObjects.Count); } catch (DeleteObjectsException e) { PrintDeletionErrorStatus(e); } } private static void PrintDeletionErrorStatus(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 async Task<List<KeyVersion>> PutObjectsAsync(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 = await s3Client.PutObjectAsync(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; } } }

例 受版本控制的存储桶的多对象删除

下面的 C# 示例使用多对象删除 API 从受版本控制的存储桶中删除对象。该示例将执行以下操作:

  1. 创建示例对象然后删除它们,方法是为每个对象指定键名称和版本 ID。本操作将删除特定版本的对象。

  2. 通过仅指定键名称来创建示例对象然后删除它们。由于该示例不指定版本 ID,本操作仅添加删除标记。它不会删除特定版本的对象。在删除后,这些对象不会显示在 Amazon S3 控制台中。

  3. 通过指定删除标记的对象键和版本 ID 来移除删除标记。当本操作移除删除标记后,这些对象将再次显示在控制台中。

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

using Amazon.S3; using Amazon.S3.Model; using System; using System.Collections.Generic; using System.Threading.Tasks; namespace Amazon.DocSamples.S3 { class DeleteMultipleObjVersionedBucketTest { private const string bucketName = "*** versioning-enabled bucket name ***"; // Specify your bucket region (an example region is shown). private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2; private static IAmazonS3 s3Client; public static void Main() { s3Client = new AmazonS3Client(bucketRegion); DeleteMultipleObjectsFromVersionedBucketAsync().Wait(); } private static async Task DeleteMultipleObjectsFromVersionedBucketAsync() { // Delete objects (specifying object version in the request). await DeleteObjectVersionsAsync(); // Delete objects (without specifying object version in the request). var deletedObjects = await DeleteObjectsAsync(); // Additional exercise - remove the delete markers S3 returned in the preceding response. // This results in the objects reappearing in the bucket (you can // verify the appearance/disappearance of objects in the console). await RemoveDeleteMarkersAsync(deletedObjects); } private static async Task<List<DeletedObject>> DeleteObjectsAsync() { // Upload the sample objects. var keysAndVersions2 = await PutObjectsAsync(3); // Delete objects using only keys. Amazon S3 creates a delete marker and // returns its version ID in the response. List<DeletedObject> deletedObjects = await NonVersionedDeleteAsync(keysAndVersions2); return deletedObjects; } private static async Task DeleteObjectVersionsAsync() { // Upload the sample objects. var keysAndVersions1 = await PutObjectsAsync(3); // Delete the specific object versions. await VersionedDeleteAsync(keysAndVersions1); } 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 (var deleteError in errorResponse.DeleteErrors) { Console.WriteLine("Object Key: {0}\t{1}\t{2}", deleteError.Key, deleteError.Code, deleteError.Message); } } static async Task VersionedDeleteAsync(List<KeyVersion> keys) { // a. Perform a multi-object delete by specifying the key names and version IDs. var multiObjectDeleteRequest = new DeleteObjectsRequest { BucketName = bucketName, Objects = keys // This includes the object keys and specific version IDs. }; try { Console.WriteLine("Executing VersionedDelete..."); DeleteObjectsResponse response = await s3Client.DeleteObjectsAsync(multiObjectDeleteRequest); Console.WriteLine("Successfully deleted all the {0} items", response.DeletedObjects.Count); } catch (DeleteObjectsException e) { PrintDeletionReport(e); } } static async Task<List<DeletedObject>> NonVersionedDeleteAsync(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 = await s3Client.DeleteObjectsAsync(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 async Task RemoveDeleteMarkersAsync(List<DeletedObject> deletedObjects) { var 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 = await s3Client.DeleteObjectsAsync(multiObjectDeleteRequest); Console.WriteLine("Successfully deleted all the {0} delete markers", deleteObjectResponse.DeletedObjects.Count); } catch (DeleteObjectsException e) { PrintDeletionReport(e); } } static async Task<List<KeyVersion>> PutObjectsAsync(int number) { var keys = new List<KeyVersion>(); for (var 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!", }; var response = await s3Client.PutObjectAsync(request); KeyVersion keyVersion = new KeyVersion { Key = key, VersionId = response.VersionId }; keys.Add(keyVersion); } return keys; } } }