private Amazon.S3.Model.DeleteBucketResponse CallAWSServiceOperation(IAmazonS3 client, Amazon.S3.Model.DeleteBucketRequest request) { Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "Amazon S3", "DeleteBucket"); try { #if DESKTOP return(client.DeleteBucket(request)); #elif CORECLR return(client.DeleteBucketAsync(request).GetAwaiter().GetResult()); #else #error "Unknown build edition" #endif } catch (AmazonServiceException exc) { var webException = exc.InnerException as System.Net.WebException; if (webException != null) { throw new Exception(Utils.Common.FormatNameResolutionFailureMessage(client.Config, webException.Message), webException); } throw; } }
public static void DeleteBucket(IAmazonS3 client, string bucketName) { client.DeleteBucket(new DeleteBucketRequest { BucketName = bucketName }); }
void TestAccelerateUnsupportedOperations(IAmazonS3 client) { // List, Put and Delete bucket should hit regional endpoint var buckets = client.ListBuckets().Buckets; Assert.IsNotNull(buckets); var newBucket = UtilityMethods.GenerateName(); client.PutBucket(newBucket); S3TestUtils.WaitForConsistency(() => { var result = client.ListBuckets().Buckets.Any(b => b.BucketName.Equals(newBucket, StringComparison.Ordinal)); return(result ? (bool?)true : null); }); try { client.DeleteBucket(newBucket); } catch { Console.WriteLine($"Failed to clean up new bucket {newBucket}. Ignore leftover bucket."); } }
/// <summary> /// Delete a bucket by name /// </summary> /// <param name="bucketName">Bucket name</param> public static void DeleteBucket(string bucketName) { using (IAmazonS3 s3Client = GetAmazonS3ClientInstance()) { DeleteBucketRequest request = new DeleteBucketRequest(); request.BucketName = bucketName; request.BucketRegion = new S3Region(DefaultRegion); s3Client.DeleteBucket(bucketName); } }
void TestAccelerateUnsupportedOperations(IAmazonS3 client) { // List, Put and Delete bucket should hit regional endpoint var buckets = client.ListBuckets().Buckets; Assert.IsNotNull(buckets); var newBucket = UtilityMethods.GenerateName(); client.PutBucket(newBucket); UtilityMethods.WaitUntil(() => { return(client.ListBuckets().Buckets.Any(b => b.BucketName.Equals(newBucket, StringComparison.Ordinal))); }); client.DeleteBucket(newBucket); }
/// <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(); }
public void TestPipelineOperations() { var inputBucket = S3TestUtils.CreateBucket(s3Client); var outputBucket = S3TestUtils.CreateBucket(s3Client); var pipelineName = "sdktest-pipeline" + DateTime.Now.Ticks; var roleName = "sdktest-ets-role" + DateTime.Now.Ticks; var policyName = "Access_Policy"; var pipelineId = string.Empty; var pipelineExists = false; try { // Create a role with trust policy var role = iamClient.CreateRole(new CreateRoleRequest { RoleName = roleName, AssumeRolePolicyDocument = TrustPolicy }).Role; // Set access policy iamClient.PutRolePolicy(new PutRolePolicyRequest { RoleName = roleName, PolicyDocument = AccessPolicy, PolicyName = policyName }); Client.ListPipelines(); // Create Pipeline var pipeline = Client.CreatePipeline( new CreatePipelineRequest { Name = pipelineName, InputBucket = inputBucket, OutputBucket = outputBucket, Notifications = new Notifications { }, Role = role.Arn, AwsKmsKeyArn = kmsKeyArn }).Pipeline; pipelineExists = true; Assert.IsNotNull(pipeline); Assert.AreEqual(pipeline.Name, pipelineName); Thread.Sleep(1000 * 5); pipelineId = pipeline.Id; // List Pipelines var pipelines = Client.ListPipelines().Pipelines; Assert.IsTrue(pipelines.Count > 0); pipelines.Contains(pipeline); // Get Pipeline var readPipelineResult = Client.ReadPipeline( new ReadPipelineRequest() { Id = pipelineId }); Assert.AreEqual(readPipelineResult.Pipeline.Id, pipelineId); // Update pipeline Client.UpdatePipelineStatus( new UpdatePipelineStatusRequest { Id = pipelineId, Status = "Paused" }); // Get pipeline readPipelineResult = Client.ReadPipeline( new ReadPipelineRequest { Id = pipelineId }); Assert.AreEqual("Paused".ToLower(), readPipelineResult.Pipeline.Status.ToLower()); // List jobs var jobs = Client.ListJobsByPipeline( new ListJobsByPipelineRequest { PipelineId = pipelineId, Ascending = "true" }).Jobs; // Remove pipeline Client.DeletePipeline( new DeletePipelineRequest { Id = pipelineId }); pipelineExists = false; AssertExtensions.ExpectException(() => { readPipelineResult = Client.ReadPipeline( new ReadPipelineRequest() { Id = pipelineId }); }, typeof(ResourceNotFoundException)); } finally { s3Client.DeleteBucket(new DeleteBucketRequest { BucketName = inputBucket }); s3Client.DeleteBucket(new DeleteBucketRequest { BucketName = outputBucket }); iamClient.DeleteRolePolicy(new DeleteRolePolicyRequest { RoleName = roleName, PolicyName = policyName }); iamClient.DeleteRole(new DeleteRoleRequest { RoleName = roleName }); if (pipelineExists) { // Remove pipeline Client.DeletePipeline(new DeletePipelineRequest { Id = pipelineId }); } } }
//Só deleta se o Bucket estiver vazio static void DeletingBucket(String bucketName) { client.DeleteBucket(bucketName); Console.WriteLine("Deletando bucket: {0} ...", bucketName); }
/// <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); const int maxRetries = 10; for (int retries = 1; retries <= maxRetries; retries++) { try { // Bucket is empty, delete the bucket. s3Client.DeleteBucket(new DeleteBucketRequest { BucketName = bucketName }); break; } catch (AmazonS3Exception e) { if (e.StatusCode != HttpStatusCode.Conflict || retries == maxRetries) throw; else DefaultRetryPolicy.WaitBeforeRetry(retries, 5000); } } // Signal that the operation is completed. asyncCancelableResult.SignalWaitHandleOnCompleted(); }