|
| 1 | +namespace DeleteMultipleObjectsExample |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Collections.Generic; |
| 5 | + using System.Threading.Tasks; |
| 6 | + using Amazon; |
| 7 | + using Amazon.S3; |
| 8 | + using Amazon.S3.Model; |
| 9 | + |
| 10 | + /// <summary> |
| 11 | + /// This example shows how to delete objects in a version-enabled Amazon |
| 12 | + /// Simple StorageService (Amazon S3) bucket. It was created using AWS |
| 13 | + /// SDK for .NET version 3.7 and .NET Core 5.0. |
| 14 | + /// </summary> |
| 15 | + public class DeleteMultipleObjects |
| 16 | + { |
| 17 | + public static async Task Main() |
| 18 | + { |
| 19 | + string bucketName = "doc-example-bucket"; |
| 20 | + |
| 21 | + // If the AWS region for your Amazon S3 bucket is different from |
| 22 | + // the AWS Region of the default user, define the AWS Region for |
| 23 | + // the S3 bucket and pass it to the client constructor like this: |
| 24 | + // RegionEndpoint bucketRegion = RegionEndpoint.USWest2; |
| 25 | + IAmazonS3 s3Client; |
| 26 | + |
| 27 | + s3Client = new AmazonS3Client(); |
| 28 | + await DeleteMultipleObjectsFromVersionedBucketAsync(s3Client, bucketName); |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// This method removes multiple versions and objects from a |
| 33 | + /// version-enabled S3 bucket. |
| 34 | + /// </summary> |
| 35 | + /// <param name="client">The initialized S3 client object used to call |
| 36 | + /// DeleteObjectVersionsAsync, DeleteObjectsAsync, and |
| 37 | + /// RemoveDeleteMarkersAsync.</param> |
| 38 | + /// <param name="bucketName">The name of the bucket from which to delete |
| 39 | + /// objects.</param> |
| 40 | + public static async Task DeleteMultipleObjectsFromVersionedBucketAsync(IAmazonS3 client, string bucketName) |
| 41 | + { |
| 42 | + // Delete objects (specifying object version in the request). |
| 43 | + await DeleteObjectVersionsAsync(client, bucketName); |
| 44 | + |
| 45 | + // Delete objects (without specifying object version in the request). |
| 46 | + var deletedObjects = await DeleteObjectsAsync(client, bucketName); |
| 47 | + |
| 48 | + // Additional exercise - remove the delete markers S3 returned from |
| 49 | + // the preceding response. This results in the objects reappearing |
| 50 | + // in the bucket (you can verify the appearance/disappearance of |
| 51 | + // objects in the console). |
| 52 | + await RemoveDeleteMarkersAsync(client, bucketName, deletedObjects); |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Creates and then deletes non-versioned S3 objects and then deletes |
| 57 | + /// them again. The method returns a list of the S3 objects deleted. |
| 58 | + /// </summary> |
| 59 | + /// <param name="client">The initialized S3 client object used to call |
| 60 | + /// PubObjectsAsync and NonVersionedDeleteAsync.</param> |
| 61 | + /// <param name="bucketName">The name of the bucket where the objects |
| 62 | + /// will be created and then deleted.</param> |
| 63 | + /// <returns>A list of DeletedObjects.</returns> |
| 64 | + public static async Task<List<DeletedObject>> DeleteObjectsAsync(IAmazonS3 client, string bucketName) |
| 65 | + { |
| 66 | + // Upload the sample objects. |
| 67 | + var keysAndVersions2 = await PutObjectsAsync(client, bucketName, 3); |
| 68 | + |
| 69 | + // Delete objects using only keys. Amazon S3 creates a delete marker and |
| 70 | + // returns its version ID in the response. |
| 71 | + List<DeletedObject> deletedObjects = await NonVersionedDeleteAsync(client, bucketName, keysAndVersions2); |
| 72 | + return deletedObjects; |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// This method creates several temporary objects and then delete them. |
| 77 | + /// </summary> |
| 78 | + public static async Task DeleteObjectVersionsAsync(IAmazonS3 client, string bucketName) |
| 79 | + { |
| 80 | + // Upload the sample objects. |
| 81 | + var keysAndVersions1 = await PutObjectsAsync(client, bucketName, 3); |
| 82 | + |
| 83 | + // Delete the specific object versions. |
| 84 | + await VersionedDeleteAsync(client, bucketName, keysAndVersions1); |
| 85 | + } |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Displays the list of information about deleted files to the console. |
| 89 | + /// </summary> |
| 90 | + /// <param name="e">Error information from the delete process.</param> |
| 91 | + private static void DisplayDeletionErrors(DeleteObjectsException e) |
| 92 | + { |
| 93 | + var errorResponse = e.Response; |
| 94 | + Console.WriteLine($"No. of objects successfully deleted = {errorResponse.DeletedObjects.Count}"); |
| 95 | + Console.WriteLine($"No. of objects failed to delete = {errorResponse.DeleteErrors.Count}"); |
| 96 | + Console.WriteLine("Printing error data..."); |
| 97 | + foreach (var deleteError in errorResponse.DeleteErrors) |
| 98 | + { |
| 99 | + Console.WriteLine($"Object Key: {deleteError.Key}\t{deleteError.Code}\t{deleteError.Message}"); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /// <summary> |
| 104 | + /// Delete multiple objects from a version-enabled bucket. |
| 105 | + /// </summary> |
| 106 | + /// <param name="client">The initialized S3 client object used to call |
| 107 | + /// DeleteObjectVersionsAsync, DeleteObjectsAsync, and |
| 108 | + /// RemoveDeleteMarkersAsync.</param> |
| 109 | + /// <param name="bucketName">The name of the bucket from which to delete |
| 110 | + /// objects.</param> |
| 111 | + /// <param name="keys">A list of key names for the objects to delete.</param> |
| 112 | + static async Task VersionedDeleteAsync(IAmazonS3 client, string bucketName, List<KeyVersion> keys) |
| 113 | + { |
| 114 | + var multiObjectDeleteRequest = new DeleteObjectsRequest |
| 115 | + { |
| 116 | + BucketName = bucketName, |
| 117 | + Objects = keys, // This includes the object keys and specific version IDs. |
| 118 | + }; |
| 119 | + |
| 120 | + try |
| 121 | + { |
| 122 | + Console.WriteLine("Executing VersionedDelete..."); |
| 123 | + DeleteObjectsResponse response = await client.DeleteObjectsAsync(multiObjectDeleteRequest); |
| 124 | + Console.WriteLine($"Successfully deleted all the {response.DeletedObjects.Count} items"); |
| 125 | + } |
| 126 | + catch (DeleteObjectsException ex) |
| 127 | + { |
| 128 | + DisplayDeletionErrors(ex); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /// <summary> |
| 133 | + /// Deletes multiple objects from a non-versioned S3 bucket. |
| 134 | + /// </summary> |
| 135 | + /// <param name="client">The initialized S3 client object used to call |
| 136 | + /// DeleteObjectVersionsAsync, DeleteObjectsAsync, and |
| 137 | + /// RemoveDeleteMarkersAsync.</param> |
| 138 | + /// <param name="bucketName">The name of the bucket from which to delete |
| 139 | + /// objects.</param> |
| 140 | + /// <param name="keys">A list of key names for the objects to delete.</param> |
| 141 | + /// <returns>A list of the deleted objects.</returns> |
| 142 | + static async Task<List<DeletedObject>> NonVersionedDeleteAsync(IAmazonS3 client, string bucketName, List<KeyVersion> keys) |
| 143 | + { |
| 144 | + // Create a request that includes only the object key names. |
| 145 | + DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(); |
| 146 | + multiObjectDeleteRequest.BucketName = bucketName; |
| 147 | + |
| 148 | + foreach (var key in keys) |
| 149 | + { |
| 150 | + multiObjectDeleteRequest.AddKey(key.Key); |
| 151 | + } |
| 152 | + |
| 153 | + // Execute DeleteObjectsAsync. |
| 154 | + // The DeleteObjectsAsync method adds a delete marker for each |
| 155 | + // object deleted. You can verify that the objects were removed by |
| 156 | + // using the S3 console. |
| 157 | + DeleteObjectsResponse response; |
| 158 | + try |
| 159 | + { |
| 160 | + Console.WriteLine("Executing NonVersionedDelete..."); |
| 161 | + response = await client.DeleteObjectsAsync(multiObjectDeleteRequest); |
| 162 | + Console.WriteLine("Successfully deleted all the {0} items", response.DeletedObjects.Count); |
| 163 | + } |
| 164 | + catch (DeleteObjectsException ex) |
| 165 | + { |
| 166 | + DisplayDeletionErrors(ex); |
| 167 | + throw; // Some delettions failed. Investigate before continuing. |
| 168 | + } |
| 169 | + |
| 170 | + // This response contains the DeletedObjects list which we use to delete the delete markers. |
| 171 | + return response.DeletedObjects; |
| 172 | + } |
| 173 | + |
| 174 | + /// <summary> |
| 175 | + /// Deletes the markers left after deleting the temporary objects. |
| 176 | + /// </summary> |
| 177 | + /// <param name="client">The initialized S3 client object used to call |
| 178 | + /// DeleteObjectVersionsAsync, DeleteObjectsAsync, and |
| 179 | + /// RemoveDeleteMarkersAsync.</param> |
| 180 | + /// <param name="bucketName">The name of the bucket from which to delete |
| 181 | + /// objects.</param> |
| 182 | + /// <param name="deletedObjects">A list of the objects that were deleted.</param> |
| 183 | + private static async Task RemoveDeleteMarkersAsync(IAmazonS3 client, string bucketName, List<DeletedObject> deletedObjects) |
| 184 | + { |
| 185 | + var keyVersionList = new List<KeyVersion>(); |
| 186 | + |
| 187 | + foreach (var deletedObject in deletedObjects) |
| 188 | + { |
| 189 | + KeyVersion keyVersion = new KeyVersion |
| 190 | + { |
| 191 | + Key = deletedObject.Key, |
| 192 | + VersionId = deletedObject.DeleteMarkerVersionId, |
| 193 | + }; |
| 194 | + keyVersionList.Add(keyVersion); |
| 195 | + } |
| 196 | + |
| 197 | + // Create another request to delete the delete markers. |
| 198 | + var multiObjectDeleteRequest = new DeleteObjectsRequest |
| 199 | + { |
| 200 | + BucketName = bucketName, |
| 201 | + Objects = keyVersionList, |
| 202 | + }; |
| 203 | + |
| 204 | + // Now, delete the delete marker to bring your objects back to the bucket. |
| 205 | + try |
| 206 | + { |
| 207 | + Console.WriteLine("Removing the delete markers ....."); |
| 208 | + var deleteObjectResponse = await client.DeleteObjectsAsync(multiObjectDeleteRequest); |
| 209 | + Console.WriteLine($"Successfully deleted the {deleteObjectResponse.DeletedObjects.Count} delete markers"); |
| 210 | + } |
| 211 | + catch (DeleteObjectsException ex) |
| 212 | + { |
| 213 | + DisplayDeletionErrors(ex); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + /// <summary> |
| 218 | + /// Create temporary S3 objects to show how object deletion wors in an |
| 219 | + /// S3 bucket with versioning enabled. |
| 220 | + /// </summary> |
| 221 | + /// <param name="client">The initialized S3 client object used to call |
| 222 | + /// PutObjectAsync to create temporary objects for the example.</param> |
| 223 | + /// <param name="bucketName">A string representing the name of the S3 |
| 224 | + /// bucket where we will create the temporary objects.</param> |
| 225 | + /// <param name="number">The number of temporary objects to create.</param> |
| 226 | + /// <returns>A list of the KeyVersion objects.</returns> |
| 227 | + static async Task<List<KeyVersion>> PutObjectsAsync(IAmazonS3 client, string bucketName, int number) |
| 228 | + { |
| 229 | + var keys = new List<KeyVersion>(); |
| 230 | + |
| 231 | + for (var i = 0; i < number; i++) |
| 232 | + { |
| 233 | + string key = "ObjectToDelete-" + new System.Random().Next(); |
| 234 | + PutObjectRequest request = new PutObjectRequest |
| 235 | + { |
| 236 | + BucketName = bucketName, |
| 237 | + Key = key, |
| 238 | + ContentBody = "This is the content body!", |
| 239 | + }; |
| 240 | + |
| 241 | + var response = await client.PutObjectAsync(request); |
| 242 | + KeyVersion keyVersion = new KeyVersion |
| 243 | + { |
| 244 | + Key = key, |
| 245 | + VersionId = response.VersionId, |
| 246 | + }; |
| 247 | + |
| 248 | + keys.Add(keyVersion); |
| 249 | + } |
| 250 | + |
| 251 | + return keys; |
| 252 | + } |
| 253 | + } |
| 254 | +} |
0 commit comments