Search in sources :

Example 1 with ObjectVersion

use of software.amazon.awssdk.services.s3.model.ObjectVersion in project aws-sdk-java-v2 by aws.

the class S3IntegrationTestBase method deleteBucketAndAllContents.

protected static void deleteBucketAndAllContents(String bucketName) {
    System.out.println("Deleting S3 bucket: " + bucketName);
    ListObjectsResponse response = s3.listObjects(ListObjectsRequest.builder().bucket(bucketName).build());
    while (true) {
        if (response.contents() == null) {
            break;
        }
        for (S3Object objectSummary : response.contents()) {
            s3.deleteObject(DeleteObjectRequest.builder().bucket(bucketName).key(objectSummary.key()).build());
        }
        if (response.isTruncated()) {
            response = s3.listObjects(ListObjectsRequest.builder().marker(response.nextMarker()).build());
        } else {
            break;
        }
    }
    ListObjectVersionsResponse versionsResponse = s3.listObjectVersions(ListObjectVersionsRequest.builder().bucket(bucketName).build());
    if (versionsResponse.versions() != null) {
        for (ObjectVersion s : versionsResponse.versions()) {
            s3.deleteObject(DeleteObjectRequest.builder().bucket(bucketName).key(s.key()).versionId(s.versionId()).build());
        }
    }
    s3.deleteBucket(DeleteBucketRequest.builder().bucket(bucketName).build());
}
Also used : ObjectVersion(software.amazon.awssdk.services.s3.model.ObjectVersion) ListObjectsResponse(software.amazon.awssdk.services.s3.model.ListObjectsResponse) S3Object(software.amazon.awssdk.services.s3.model.S3Object) ListObjectVersionsResponse(software.amazon.awssdk.services.s3.model.ListObjectVersionsResponse)

Example 2 with ObjectVersion

use of software.amazon.awssdk.services.s3.model.ObjectVersion in project aws-sdk-java-v2 by aws.

the class CloudTrailIntegrationTest method deleteBucketAndAllContents.

public static void deleteBucketAndAllContents(String bucketName) {
    System.out.println("Deleting S3 bucket: " + bucketName);
    ListObjectsResponse response = s3.listObjects(ListObjectsRequest.builder().bucket(bucketName).build());
    while (true) {
        if (response.contents() == null) {
            break;
        }
        for (Iterator<?> iterator = response.contents().iterator(); iterator.hasNext(); ) {
            S3Object objectSummary = (S3Object) iterator.next();
            s3.deleteObject(DeleteObjectRequest.builder().bucket(bucketName).key(objectSummary.key()).build());
        }
        if (response.isTruncated()) {
            response = s3.listObjects(ListObjectsRequest.builder().marker(response.nextMarker()).build());
        } else {
            break;
        }
    }
    ListObjectVersionsResponse versionsResponse = s3.listObjectVersions(ListObjectVersionsRequest.builder().bucket(bucketName).build());
    if (versionsResponse.versions() != null) {
        for (ObjectVersion s : versionsResponse.versions()) {
            s3.deleteObject(DeleteObjectRequest.builder().bucket(bucketName).key(s.key()).versionId(s.versionId()).build());
        }
    }
    s3.deleteBucket(DeleteBucketRequest.builder().bucket(bucketName).build());
}
Also used : ObjectVersion(software.amazon.awssdk.services.s3.model.ObjectVersion) ListObjectsResponse(software.amazon.awssdk.services.s3.model.ListObjectsResponse) S3Object(software.amazon.awssdk.services.s3.model.S3Object) ListObjectVersionsResponse(software.amazon.awssdk.services.s3.model.ListObjectVersionsResponse)

Aggregations

ListObjectVersionsResponse (software.amazon.awssdk.services.s3.model.ListObjectVersionsResponse)2 ListObjectsResponse (software.amazon.awssdk.services.s3.model.ListObjectsResponse)2 ObjectVersion (software.amazon.awssdk.services.s3.model.ObjectVersion)2 S3Object (software.amazon.awssdk.services.s3.model.S3Object)2