Search in sources :

Example 1 with DeleteBucketRequest

use of com.amazonaws.services.s3.model.DeleteBucketRequest in project aws-doc-sdk-examples by awsdocs.

the class S3BucketDeletion method listAllObjects.

// snippet-start:[s3.java2.s3_bucket_ops.delete_bucket]
public static void listAllObjects(S3Client s3, String bucket) {
    try {
        // To delete a bucket, all the objects in the bucket must be deleted first
        ListObjectsV2Request listObjectsV2Request = ListObjectsV2Request.builder().bucket(bucket).build();
        ListObjectsV2Response listObjectsV2Response;
        do {
            listObjectsV2Response = s3.listObjectsV2(listObjectsV2Request);
            for (S3Object s3Object : listObjectsV2Response.contents()) {
                s3.deleteObject(DeleteObjectRequest.builder().bucket(bucket).key(s3Object.key()).build());
            }
            listObjectsV2Request = ListObjectsV2Request.builder().bucket(bucket).continuationToken(listObjectsV2Response.nextContinuationToken()).build();
        } while (listObjectsV2Response.isTruncated());
        // snippet-end:[s3.java2.s3_bucket_ops.delete_bucket]
        DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest.builder().bucket(bucket).build();
        s3.deleteBucket(deleteBucketRequest);
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : ListObjectsV2Request(software.amazon.awssdk.services.s3.model.ListObjectsV2Request) DeleteBucketRequest(software.amazon.awssdk.services.s3.model.DeleteBucketRequest) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) S3Object(software.amazon.awssdk.services.s3.model.S3Object) ListObjectsV2Response(software.amazon.awssdk.services.s3.model.ListObjectsV2Response)

Example 2 with DeleteBucketRequest

use of com.amazonaws.services.s3.model.DeleteBucketRequest in project aws-doc-sdk-examples by awsdocs.

the class S3ObjectOperations method deleteBucket.

public static void deleteBucket(S3Client client, String bucket) {
    DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest.builder().bucket(bucket).build();
    client.deleteBucket(deleteBucketRequest);
}
Also used : DeleteBucketRequest(software.amazon.awssdk.services.s3.model.DeleteBucketRequest)

Example 3 with DeleteBucketRequest

use of com.amazonaws.services.s3.model.DeleteBucketRequest in project camel by apache.

the class S3Producer method deleteBucket.

private void deleteBucket(AmazonS3 s3Client, Exchange exchange) {
    String bucketName;
    bucketName = exchange.getIn().getHeader(S3Constants.BUCKET_NAME, String.class);
    if (ObjectHelper.isEmpty(bucketName)) {
        bucketName = getConfiguration().getBucketName();
    }
    DeleteBucketRequest deleteBucketRequest = new DeleteBucketRequest(bucketName);
    s3Client.deleteBucket(deleteBucketRequest);
}
Also used : DeleteBucketRequest(com.amazonaws.services.s3.model.DeleteBucketRequest)

Example 4 with DeleteBucketRequest

use of com.amazonaws.services.s3.model.DeleteBucketRequest in project nifi by apache.

the class AbstractS3IT method oneTimeTearDown.

@AfterClass
public static void oneTimeTearDown() {
    // Empty the bucket before deleting it.
    try {
        ObjectListing objectListing = client.listObjects(BUCKET_NAME);
        while (true) {
            for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                client.deleteObject(BUCKET_NAME, objectSummary.getKey());
            }
            if (objectListing.isTruncated()) {
                objectListing = client.listNextBatchOfObjects(objectListing);
            } else {
                break;
            }
        }
        DeleteBucketRequest dbr = new DeleteBucketRequest(BUCKET_NAME);
        client.deleteBucket(dbr);
    } catch (final AmazonS3Exception e) {
        System.err.println("Unable to delete bucket " + BUCKET_NAME + e.toString());
    }
    if (client.doesBucketExist(BUCKET_NAME)) {
        Assert.fail("Incomplete teardown, subsequent tests might fail");
    }
}
Also used : DeleteBucketRequest(com.amazonaws.services.s3.model.DeleteBucketRequest) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) AfterClass(org.junit.AfterClass)

Aggregations

DeleteBucketRequest (com.amazonaws.services.s3.model.DeleteBucketRequest)2 DeleteBucketRequest (software.amazon.awssdk.services.s3.model.DeleteBucketRequest)2 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)1 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)1 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)1 AfterClass (org.junit.AfterClass)1 ListObjectsV2Request (software.amazon.awssdk.services.s3.model.ListObjectsV2Request)1 ListObjectsV2Response (software.amazon.awssdk.services.s3.model.ListObjectsV2Response)1 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)1 S3Object (software.amazon.awssdk.services.s3.model.S3Object)1