public void TestCleanup() { try { var objRequest = new ListObjectsRequest() { BucketName = this.bucketName }; using (var objResponse = client.ListObjects(objRequest)) { var delRequest = new DeleteObjectsRequest() { BucketName = this.bucketName, Quiet = true }; delRequest.AddKeys(objResponse.S3Objects.Select(o => new KeyVersion(o.Key)).ToArray()); using (var delResponse = client.DeleteObjects(delRequest)) { } } var deleteRequest = new DeleteBucketRequest() { BucketName = this.bucketName }; using (var deleteResponse = client.DeleteBucket(deleteRequest)) { } } catch (Exception ex) { this.TestContext.WriteLine("Warning: Could not cleanup bucket: {0}. {1}", this.bucketName, ex); } }
/// <summary> /// Deletes all keys in a given bucket, then deletes the bucket itself. /// </summary> /// <param name="client">The client to use.</param> /// <param name="bucketName">The bucket to delete.</param> public static void DeleteBucketRecursive(this AmazonS3 client, string bucketName) { while (true) { // attempt to delete the bucket try { var deleteRequest = new DeleteBucketRequest() { BucketName = bucketName }; using (var deleteResponse = client.DeleteBucket(deleteRequest)) { // deletion was successful return; } } catch (AmazonS3Exception ex) { if (ex.ErrorCode != "BucketNotEmpty") { throw ex; } } var objRequest = new ListObjectsRequest() { BucketName = bucketName }; using (var objResponse = client.ListObjects(objRequest)) { var delRequest = new DeleteObjectsRequest() { BucketName = bucketName, Quiet = true }; // select the objects to delete (up to the supported limit of 1000) var objToDelete = objResponse.S3Objects.Take(1000).Select(o => new KeyVersion(o.Key)); delRequest.AddKeys(objToDelete.ToArray()); using (var delResponse = client.DeleteObjects(delRequest)) { } } } }
public static void CleanBucket(string bucket, string aws_id, string aws_secret) { // set up client using (AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(aws_id, aws_secret)) { // check to ensure that the bucket actually exists var dirinfo = new Amazon.S3.IO.S3DirectoryInfo(client, bucket); if (dirinfo.Exists) { Console.WriteLine("Bucket \"{0}\" already exists. Erasing. Sorry if this isn't what you wanted, dude.", bucket); // get a list of the bucket's objects var lor = new ListObjectsRequest { BucketName = bucket }; using (ListObjectsResponse r = client.ListObjects(lor)) { if (r.S3Objects.Count > 0) { List <KeyVersion> objects = r.S3Objects.Select(obj => new KeyVersion(obj.Key)).ToList(); // batch-delete all the objects in the bucket DeleteObjectsRequest dor = new DeleteObjectsRequest { BucketName = bucket, Keys = objects }; client.DeleteObjects(dor); } } } else { Console.WriteLine("Creating new bucket \"{0}\"", bucket); // bucket doesn't exist; make a new one PutBucketRequest pbr = new PutBucketRequest { BucketName = bucket }; client.PutBucket(pbr); } } }
public void DeleteFiles(IEnumerable <string> filePaths) { var batches = filePaths .BatchesOf(MultiObjectLimit); foreach (var batch in batches) { var request = new DeleteObjectsRequest { BucketName = BucketName, }; foreach (var filePath in batch) { request.AddKey(filePath); } AmazonS3.DeleteObjects(request); } }
/// <summary> /// Delete a folder /// </summary> /// <param name="prefix">prefix</param> public void DeleteFolder(string prefix) { // Get all object with specified prefix var listRequest = new ListObjectsRequest(); listRequest.WithBucketName(_bucketName).WithPrefix(prefix); var deleteRequest = new DeleteObjectsRequest(); deleteRequest.BucketName = _bucketName; do { ListObjectsResponse listResponse = _client.ListObjects(listRequest); // Add all object with specified prefix to delete request. foreach (S3Object entry in listResponse.S3Objects) { deleteRequest.AddKey(entry.Key); } if (listResponse.IsTruncated) { listRequest.Marker = listResponse.NextMarker; } else { listRequest = null; } }while (listRequest != null); // Delete all the object with specified prefix. if (deleteRequest.Keys.Count > 0) { var deleteResponse = _client.DeleteObjects(deleteRequest); deleteResponse.Dispose(); } }
/// <summary> /// Deletes an S3 bucket which contains objects. /// An S3 bucket which contains objects cannot be deleted until all the objects /// in it are deleted. The function deletes all the objects in the specified /// bucket and then deletes the bucket itself. /// </summary> /// <param name="bucketName">The bucket to be deleted.</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> /// <param name="deleteOptions">Options to control the behavior of the delete operation.</param> /// <param name="updateCallback">The callback which is used to send updates about the delete operation.</param> /// <param name="asyncCancelableResult">An IAsyncCancelableResult that can be used to poll or wait for results, or both; /// this value is also needed when invoking EndDeleteS3BucketWithObjects. IAsyncCancelableResult can also /// be used to cancel the operation while it's in progress.</param> private static void DeleteS3BucketWithObjectsInternal(string bucketName, AmazonS3 s3Client, S3DeleteBucketWithObjectsOptions deleteOptions,Action<S3DeleteBucketWithObjectsUpdate> updateCallback, AsyncCancelableResult asyncCancelableResult) { // Validations. if (s3Client == null) { throw new ArgumentNullException("s3Client", "The s3Client cannot be null!"); } if (string.IsNullOrEmpty(bucketName)) { throw new ArgumentNullException("bucketName", "The bucketName cannot be null or empty string!"); } var listVersionsRequest = new ListVersionsRequest { BucketName=bucketName }; ListVersionsResponse listVersionsResponse; // Iterate through the objects in the bucket and delete them. do { // Check if the operation has been canceled. if (asyncCancelableResult.IsCancelRequested) { // Signal that the operation is canceled. asyncCancelableResult.SignalWaitHandleOnCanceled(); return; } // List all the versions of all the objects in the bucket. listVersionsResponse = s3Client.ListVersions(listVersionsRequest); if (listVersionsResponse.Versions.Count==0) { // If the bucket has no objects break the loop. break; } var keyVersionList = new List<KeyVersion>(listVersionsResponse.Versions.Count); for (int index = 0; index < listVersionsResponse.Versions.Count; index++) { keyVersionList.Add(new KeyVersion( listVersionsResponse.Versions[index].Key, listVersionsResponse.Versions[index].VersionId )); } try { // Delete the current set of objects. var deleteObjectsResponse = s3Client.DeleteObjects(new DeleteObjectsRequest { BucketName=bucketName, Quiet=deleteOptions.QuietMode, Keys=keyVersionList }); if (!deleteOptions.QuietMode) { // If quiet mode is not set, update the client with list of deleted objects. InvokeS3DeleteBucketWithObjectsUpdateCallback( updateCallback, new S3DeleteBucketWithObjectsUpdate { DeletedObjects = deleteObjectsResponse.DeletedObjects } ); } } catch (DeleteObjectsException deleteObjectsException) { if (deleteOptions.ContinueOnError) { // Continue the delete operation if an error was encountered. // Update the client with the list of objects that were deleted and the // list of objects on which the delete failed. InvokeS3DeleteBucketWithObjectsUpdateCallback( updateCallback, new S3DeleteBucketWithObjectsUpdate { DeletedObjects = deleteObjectsException.ErrorResponse.DeletedObjects, DeleteErrors = deleteObjectsException.ErrorResponse.DeleteErrors } ); } else { // Re-throw the exception if an error was encountered. throw; } } // Set the markers to get next set of objects from the bucket. listVersionsRequest.KeyMarker = listVersionsResponse.NextKeyMarker; listVersionsRequest.VersionIdMarker = listVersionsResponse.NextVersionIdMarker; } // Continue listing objects and deleting them until the bucket is empty. while (listVersionsResponse.IsTruncated); for (int attempts = 0; true; attempts++) { try { // Bucket is empty, delete the bucket. s3Client.DeleteBucket( new DeleteBucketRequest { BucketName = bucketName } ); break; } catch (AmazonS3Exception e) { if (!string.Equals(e.ErrorCode, S3Constants.BucketNotEmpty) || attempts >= 3) throw; Thread.Sleep(5 * 1000); } } // Signal that the operation is completed. asyncCancelableResult.SignalWaitHandleOnCompleted(); }