Java Code Examples for software.amazon.awssdk.services.s3.S3Client#deleteBucket()

The following examples show how to use software.amazon.awssdk.services.s3.S3Client#deleteBucket() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: S3BucketOps.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {

        // snippet-start:[s3.java2.s3_bucket_ops.create_bucket]
        // snippet-start:[s3.java2.s3_bucket_ops.region]
        Region region = Region.US_WEST_2;
        S3Client s3 = S3Client.builder().region(region).build();
        // snippet-end:[s3.java2.s3_bucket_ops.region]
        String bucket = "bucket" + System.currentTimeMillis();
        System.out.println(bucket);

        // Create bucket
        CreateBucketRequest createBucketRequest = CreateBucketRequest
                .builder()
                .bucket(bucket)
                .createBucketConfiguration(CreateBucketConfiguration.builder()
                        .locationConstraint(region.id())
                        .build())
                .build();
        s3.createBucket(createBucketRequest);
        // snippet-end:[s3.java2.s3_bucket_ops.create_bucket]

        // snippet-start:[s3.java2.s3_bucket_ops.list_bucket]
        // List buckets
        ListBucketsRequest listBucketsRequest = ListBucketsRequest.builder().build();
        ListBucketsResponse listBucketsResponse = s3.listBuckets(listBucketsRequest);
        listBucketsResponse.buckets().stream().forEach(x -> System.out.println(x.name()));
        // snippet-end:[s3.java2.s3_bucket_ops.list_bucket]

        // Delete empty bucket
        // snippet-start:[s3.java2.s3_bucket_ops.delete_bucket]      
        DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest.builder().bucket(bucket).build();
        s3.deleteBucket(deleteBucketRequest);
        // snippet-end:[s3.java2.s3_bucket_ops.delete_bucket] 
    }
 
Example 2
Source File: Synch.java    From pegasus with Apache License 2.0 5 votes vote down vote up
/**
 * Delete a S3 bucket with the given name
 *
 * @param name
 * @return
 */
public boolean deleteS3Bucket(String name) {
    boolean deleted = true;
    ListObjectsV2Request listObjectsV2Request =
            ListObjectsV2Request.builder().bucket(name).build();
    ListObjectsV2Response listObjectsV2Response;
    S3Client s3Client = S3Client.builder().region(mAWSRegion).build();
    do {
        listObjectsV2Response = s3Client.listObjectsV2(listObjectsV2Request);
        if (listObjectsV2Response.contents() != null) {
            // detelete the files in the bucket
            for (S3Object s3Object : listObjectsV2Response.contents()) {
                mLogger.debug("Deleteing file " + s3Object.key() + " from bucket " + name);
                s3Client.deleteObject(
                        DeleteObjectRequest.builder().bucket(name).key(s3Object.key()).build());
            }
        }

        listObjectsV2Request =
                ListObjectsV2Request.builder()
                        .bucket(name)
                        .continuationToken(listObjectsV2Response.nextContinuationToken())
                        .build();

    } while (listObjectsV2Response.isTruncated());

    // Delete empty bucket
    DeleteBucketRequest deleteBucketRequest =
            DeleteBucketRequest.builder().bucket(name).build();
    s3Client.deleteBucket(deleteBucketRequest);
    return deleted;
}
 
Example 3
Source File: S3TestUtils.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
public static void deleteBucketAndAllContents(S3Client s3, String bucketName) {
    try {
        System.out.println("Deleting S3 bucket: " + bucketName);
        ListObjectsResponse response = Waiter.run(() -> s3.listObjects(r -> r.bucket(bucketName)))
                                             .ignoringException(NoSuchBucketException.class)
                                             .orFail();
        List<S3Object> objectListing = response.contents();

        if (objectListing != null) {
            while (true) {
                for (Iterator<?> iterator = objectListing.iterator(); iterator.hasNext(); ) {
                    S3Object objectSummary = (S3Object) iterator.next();
                    s3.deleteObject(DeleteObjectRequest.builder().bucket(bucketName).key(objectSummary.key()).build());
                }

                if (response.isTruncated()) {
                    objectListing = s3.listObjects(ListObjectsRequest.builder()
                                                                     .bucket(bucketName)
                                                                     .marker(response.marker())
                                                                     .build())
                                      .contents();
                } else {
                    break;
                }
            }
        }


        ListObjectVersionsResponse versions = s3
                .listObjectVersions(ListObjectVersionsRequest.builder().bucket(bucketName).build());

        if (versions.deleteMarkers() != null) {
            versions.deleteMarkers().forEach(v -> s3.deleteObject(DeleteObjectRequest.builder()
                                                                                     .versionId(v.versionId())
                                                                                     .bucket(bucketName)
                                                                                     .key(v.key())
                                                                                     .build()));
        }

        if (versions.versions() != null) {
            versions.versions().forEach(v -> s3.deleteObject(DeleteObjectRequest.builder()
                                                                                .versionId(v.versionId())
                                                                                .bucket(bucketName)
                                                                                .key(v.key())
                                                                                .build()));
        }

        s3.deleteBucket(DeleteBucketRequest.builder().bucket(bucketName).build());
    } catch (Exception e) {
        System.err.println("Failed to delete bucket: " + bucketName);
        e.printStackTrace();
    }
}
 
Example 4
Source File: S3ObjectOperations.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
public static void deleteBucket(S3Client client, String bucket) {
    DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest.builder().bucket(bucket).build();
    client.deleteBucket(deleteBucketRequest);
}