Exemplo n.º 1
0
        /// <summary>
        /// Initiates the asynchronous execution of the DeleteS3BucketWithObjects operation.
        /// DeleteS3BucketWithObjects deletes an S3 bucket which contains objects.
        /// An S3 bucket which contains objects cannot be deleted until all the objects
        /// in it are deleted. This method 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">An callback that is invoked to send updates while delete operation is in progress.</param>
        /// <param name="token">token to check if the operation has been request to cancel.</param>
        /// <returns>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.</returns>
        public static Task DeleteS3BucketWithObjectsAsync(IAmazonS3 s3Client, string bucketName,
                                                          S3DeleteBucketWithObjectsOptions deleteOptions, Action <S3DeleteBucketWithObjectsUpdate> updateCallback, CancellationToken token)
        {
            var request = new S3DeleteBucketWithObjectsRequest
            {
                BucketName     = bucketName,
                DeleteOptions  = deleteOptions,
                UpdateCallback = updateCallback,
                S3Client       = s3Client
            };

            return(InvokeDeleteS3BucketWithObjects(request, token));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initiates the asynchronous execution of the DeleteS3BucketWithObjects operation.
        /// DeleteS3BucketWithObjects deletes an S3 bucket which contains objects.
        /// An S3 bucket which contains objects cannot be deleted until all the objects
        /// in it are deleted. This method 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">An callback that is invoked to send updates while delete operation is in progress.</param>
        /// <param name="callback">An AsyncCallback delegate that is invoked when the operation completes.</param>
        /// <param name="state">A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback procedure using the AsyncState property.</param>
        /// <returns>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.</returns>
        public static IAsyncCancelableResult BeginDeleteS3BucketWithObjects(IAmazonS3 s3Client, string bucketName,
                                                                            S3DeleteBucketWithObjectsOptions deleteOptions, Action <S3DeleteBucketWithObjectsUpdate> updateCallback, AsyncCallback callback, object state)
        {
            var asyncResult = new AsyncCancelableResult(callback, state);

            var request = new S3DeleteBucketWithObjectsRequest
            {
                AsyncCancelableResult = asyncResult,
                BucketName            = bucketName,
                DeleteOptions         = deleteOptions,
                UpdateCallback        = updateCallback,
                S3Client = s3Client
            };

            ThreadPool.QueueUserWorkItem(InvokeDeleteS3BucketWithObjects, request);
            return(asyncResult);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Initiates the asynchronous execution of the DeleteS3BucketWithObjects operation.
 /// DeleteS3BucketWithObjects deletes an S3 bucket which contains objects.
 /// An S3 bucket which contains objects cannot be deleted until all the objects
 /// in it are deleted. This method 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="callback">An AsyncCallback delegate that is invoked when the operation completes.</param>
 /// <param name="state">A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback procedure using the AsyncState property.</param>
 /// <returns>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.</returns>
 public static IAsyncCancelableResult BeginDeleteS3BucketWithObjects(IAmazonS3 s3Client, string bucketName,
                                                                     S3DeleteBucketWithObjectsOptions deleteOptions, AsyncCallback callback, object state)
 {
     return(BeginDeleteS3BucketWithObjects(s3Client, bucketName, deleteOptions, null, callback, state));
 }
Exemplo n.º 4
0
        /// <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. This method 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>
        public static void DeleteS3BucketWithObjects(IAmazonS3 s3Client, string bucketName, S3DeleteBucketWithObjectsOptions deleteOptions)
        {
            var result = BeginDeleteS3BucketWithObjects(s3Client, bucketName, deleteOptions, null, null);

            EndDeleteS3BucketWithObjects(result);
        }
Exemplo n.º 5
0
 /// <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. This method 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>
 public static void DeleteS3BucketWithObjects(IAmazonS3 s3Client, string bucketName, S3DeleteBucketWithObjectsOptions deleteOptions)
 {
     DeleteS3BucketWithObjectsAsync(s3Client, bucketName, deleteOptions, CancellationToken.None).Wait();
 }
Exemplo n.º 6
0
        /// <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();
        }
Exemplo n.º 7
0
 /// <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. This method 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>
 public static void DeleteS3BucketWithObjects(string bucketName, AmazonS3 s3Client, S3DeleteBucketWithObjectsOptions deleteOptions)
 {
     var result = BeginDeleteS3BucketWithObjects(bucketName, s3Client,deleteOptions, null, null);
     EndDeleteS3BucketWithObjects(result);
 }
Exemplo n.º 8
0
        /// <summary>
        /// Initiates the asynchronous execution of the DeleteS3BucketWithObjects operation. 
        /// DeleteS3BucketWithObjects deletes an S3 bucket which contains objects.
        /// An S3 bucket which contains objects cannot be deleted until all the objects 
        /// in it are deleted. This method 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">An callback that is invoked to send updates while delete operation is in progress.</param>
        /// <param name="callback">An AsyncCallback delegate that is invoked when the operation completes.</param>
        /// <param name="state">A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback procedure using the AsyncState property.</param>
        /// <returns>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.</returns>
        public static IAsyncCancelableResult BeginDeleteS3BucketWithObjects(string bucketName, AmazonS3 s3Client,
            S3DeleteBucketWithObjectsOptions deleteOptions,Action<S3DeleteBucketWithObjectsUpdate> updateCallback, AsyncCallback callback, object state)
        {
            var asyncResult = new AsyncCancelableResult(callback, state);

            var request = new S3DeleteBucketWithObjectsRequest
            {
                AsyncCancelableResult = asyncResult,
                BucketName = bucketName,
                DeleteOptions = deleteOptions,
                UpdateCallback=updateCallback,
                S3Client = s3Client
            };

            ThreadPool.QueueUserWorkItem(InvokeDeleteS3BucketWithObjects, request);
            return asyncResult;
        }
Exemplo n.º 9
0
 /// <summary>
 /// Initiates the asynchronous execution of the DeleteS3BucketWithObjects operation. 
 /// DeleteS3BucketWithObjects deletes an S3 bucket which contains objects.
 /// An S3 bucket which contains objects cannot be deleted until all the objects 
 /// in it are deleted. This method 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">An callback that is invoked to send updates while delete operation is in progress.</param>
 /// <param name="token">token to check if the operation has been request to cancel.</param>
 /// <returns>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.</returns>
 public static  Task DeleteS3BucketWithObjectsAsync(IAmazonS3 s3Client, string bucketName,
      S3DeleteBucketWithObjectsOptions deleteOptions, Action<S3DeleteBucketWithObjectsUpdate> updateCallback, CancellationToken token)
 {
     var request = new S3DeleteBucketWithObjectsRequest
     {
         BucketName = bucketName,
         DeleteOptions = deleteOptions,
         UpdateCallback = updateCallback,
         S3Client = s3Client
     };
     return InvokeDeleteS3BucketWithObjects(request, token);
 }
Exemplo n.º 10
0
        /// <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. This method 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>
        public static Task DeleteS3BucketWithObjectsAsync(IAmazonS3 s3Client, string bucketName, S3DeleteBucketWithObjectsOptions deleteOptions)
        {
            var cancelSource = new CancellationTokenSource();

            return(DeleteS3BucketWithObjectsAsync(s3Client, bucketName, deleteOptions, cancelSource.Token));
        }
Exemplo n.º 11
0
        /// <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="token">token to check if the operation has been request to cancel.</param>
        private static async Task DeleteS3BucketWithObjectsInternalAsync(IAmazonS3 s3Client, string bucketName,
                                                                         S3DeleteBucketWithObjectsOptions deleteOptions, Action <S3DeleteBucketWithObjectsUpdate> updateCallback,
                                                                         CancellationToken token)
        {
            // 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 (token.IsCancellationRequested)
                {
                    // Signal that the operation is canceled.
                    return;
                }

                // List all the versions of all the objects in the bucket.
                listVersionsResponse = await s3Client.ListVersionsAsync(listVersionsRequest, token).ConfigureAwait(false);

                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
                    {
                        Key       = listVersionsResponse.Versions[index].Key,
                        VersionId = listVersionsResponse.Versions[index].VersionId
                    });
                }

                try
                {
                    // Delete the current set of objects.
                    var deleteObjectsResponse = await s3Client.DeleteObjectsAsync(new DeleteObjectsRequest
                    {
                        BucketName = bucketName,
                        Objects    = keyVersionList,
                        Quiet      = deleteOptions.QuietMode
                    }, token).ConfigureAwait(false);

                    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.Response.DeletedObjects,
                            DeleteErrors   = deleteObjectsException.Response.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);

            const int maxRetries = 10;

            for (int retries = 1; retries <= maxRetries; retries++)
            {
                try
                {
                    // Bucket is empty, delete the bucket.
                    await s3Client.DeleteBucketAsync(new DeleteBucketRequest
                    {
                        BucketName = bucketName
                    }, token).ConfigureAwait(false);

                    break;
                }
                catch (AmazonS3Exception e)
                {
                    if (e.StatusCode != HttpStatusCode.Conflict || retries == maxRetries)
                    {
                        throw;
                    }
                    else
                    {
                        DefaultRetryPolicy.WaitBeforeRetry(retries, 5000);
                    }
                }
            }
        }
Exemplo n.º 12
0
 /// <summary>
 /// Initiates the asynchronous execution of the DeleteS3BucketWithObjects operation.
 /// DeleteS3BucketWithObjects deletes an S3 bucket which contains objects.
 /// An S3 bucket which contains objects cannot be deleted until all the objects
 /// in it are deleted. This method 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="token">token to check if the operation has been request to cancel.</param>
 /// <returns>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.</returns>
 public static Task DeleteS3BucketWithObjectsAsync(IAmazonS3 s3Client, string bucketName,
                                                   S3DeleteBucketWithObjectsOptions deleteOptions, CancellationToken token)
 {
     return(DeleteS3BucketWithObjectsAsync(s3Client, bucketName, deleteOptions, null, token));
 }
Exemplo n.º 13
0
 /// <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. This method 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>
 public static void DeleteS3BucketWithObjects(IAmazonS3 s3Client, string bucketName, S3DeleteBucketWithObjectsOptions deleteOptions)
 {
     DeleteS3BucketWithObjectsAsync(s3Client, bucketName, deleteOptions, CancellationToken.None).Wait();
 }
Exemplo n.º 14
0
 /// <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. This method 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>
 public static  Task DeleteS3BucketWithObjectsAsync(IAmazonS3 s3Client, string bucketName, S3DeleteBucketWithObjectsOptions deleteOptions)
 {
     var cancelSource = new CancellationTokenSource();
     return  DeleteS3BucketWithObjectsAsync(s3Client, bucketName, deleteOptions, cancelSource.Token);
 }
Exemplo n.º 15
0
        /// <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="token">token to check if the operation has been request to cancel.</param>
        private static async Task DeleteS3BucketWithObjectsInternalAsync(IAmazonS3 s3Client, string bucketName,
            S3DeleteBucketWithObjectsOptions deleteOptions, Action<S3DeleteBucketWithObjectsUpdate> updateCallback,
            CancellationToken token)
        {
            // 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 (token.IsCancellationRequested)
                {
                    // Signal that the operation is canceled.
                    return;
                }

                // List all the versions of all the objects in the bucket.
                listVersionsResponse = await s3Client.ListVersionsAsync(listVersionsRequest,token).ConfigureAwait(false);

                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
                    {
                        Key = listVersionsResponse.Versions[index].Key,
                        VersionId = listVersionsResponse.Versions[index].VersionId
                    });
                }

                try
                {
                    // Delete the current set of objects.
                    var deleteObjectsResponse = await s3Client.DeleteObjectsAsync(new DeleteObjectsRequest
                    {
                        BucketName = bucketName,
                        Objects = keyVersionList,
                        Quiet = deleteOptions.QuietMode
                    },token).ConfigureAwait(false);

                    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.Response.DeletedObjects,
                                    DeleteErrors = deleteObjectsException.Response.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);

            const int maxRetries = 10;
            for (int retries = 1; retries <= maxRetries; retries++)
            {
                try
                {
                    // Bucket is empty, delete the bucket.
                    await s3Client.DeleteBucketAsync(new DeleteBucketRequest
                    {
                        BucketName = bucketName
                    },token).ConfigureAwait(false);
                    break;
                }
                catch (AmazonS3Exception e)
                {
                    if (e.StatusCode != HttpStatusCode.Conflict || retries == maxRetries)
                        throw;
                    else
                        DefaultRetryPolicy.WaitBeforeRetry(retries, 5000);
                }
            }
        }
Exemplo n.º 16
0
 /// <summary>
 /// Initiates the asynchronous execution of the DeleteS3BucketWithObjects operation. 
 /// DeleteS3BucketWithObjects deletes an S3 bucket which contains objects.
 /// An S3 bucket which contains objects cannot be deleted until all the objects 
 /// in it are deleted. This method 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="callback">An AsyncCallback delegate that is invoked when the operation completes.</param>
 /// <param name="state">A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback procedure using the AsyncState property.</param>
 /// <returns>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.</returns>
 public static IAsyncCancelableResult BeginDeleteS3BucketWithObjects(string bucketName, AmazonS3 s3Client,
     S3DeleteBucketWithObjectsOptions deleteOptions, AsyncCallback callback, object state)
 {
     return BeginDeleteS3BucketWithObjects(bucketName, s3Client, deleteOptions, null, callback, state);
 }
Exemplo n.º 17
0
        /// <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(IAmazonS3 s3Client, string bucketName,
                                                              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
                    {
                        Key       = listVersionsResponse.Versions[index].Key,
                        VersionId = listVersionsResponse.Versions[index].VersionId
                    });
                }

                try
                {
                    // Delete the current set of objects.
                    var deleteObjectsResponse = s3Client.DeleteObjects(new DeleteObjectsRequest
                    {
                        BucketName = bucketName,
                        Objects    = keyVersionList,
                        Quiet      = deleteOptions.QuietMode
                    });

                    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.Response.DeletedObjects,
                            DeleteErrors   = deleteObjectsException.Response.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);

            // Bucket is empty, delete the bucket.
            s3Client.DeleteBucket(new DeleteBucketRequest
            {
                BucketName = bucketName
            });

            // Signal that the operation is completed.
            asyncCancelableResult.SignalWaitHandleOnCompleted();
        }
Exemplo n.º 18
0
 /// <summary>
 /// Initiates the asynchronous execution of the DeleteS3BucketWithObjects operation. 
 /// DeleteS3BucketWithObjects deletes an S3 bucket which contains objects.
 /// An S3 bucket which contains objects cannot be deleted until all the objects 
 /// in it are deleted. This method 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="token">token to check if the operation has been request to cancel.</param>
 /// <returns>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.</returns>
 public static Task DeleteS3BucketWithObjectsAsync(IAmazonS3 s3Client, string bucketName,
      S3DeleteBucketWithObjectsOptions deleteOptions, CancellationToken token)
 {
     return DeleteS3BucketWithObjectsAsync(s3Client, bucketName, deleteOptions, null, token);
 }