Example usage for com.amazonaws.services.s3 AmazonS3 listNextBatchOfVersions

List of usage examples for com.amazonaws.services.s3 AmazonS3 listNextBatchOfVersions

Introduction

In this page you can find the example usage for com.amazonaws.services.s3 AmazonS3 listNextBatchOfVersions.

Prototype

public VersionListing listNextBatchOfVersions(ListNextBatchOfVersionsRequest listNextBatchOfVersionsRequest)
        throws SdkClientException, AmazonServiceException;

Source Link

Document

Provides an easy way to continue a truncated VersionListing and retrieve the next page of results.

Usage

From source file:aws.example.s3.DeleteBucket.java

License:Open Source License

public static void main(String[] args) {
    final String USAGE = "\n" + "To run this example, supply the name of an S3 bucket\n" + "\n"
            + "Ex: DeleteBucket <bucketname>\n";

    if (args.length < 1) {
        System.out.println(USAGE);
        System.exit(1);/*from   w  w w.j  av a  2  s . c  o  m*/
    }

    String bucket_name = args[0];

    System.out.println("Deleting S3 bucket: " + bucket_name);
    final AmazonS3 s3 = new AmazonS3Client();

    try {
        System.out.println(" - removing objects from bucket");
        ObjectListing object_listing = s3.listObjects(bucket_name);
        while (true) {
            for (Iterator<?> iterator = object_listing.getObjectSummaries().iterator(); iterator.hasNext();) {
                S3ObjectSummary summary = (S3ObjectSummary) iterator.next();
                s3.deleteObject(bucket_name, summary.getKey());
            }

            // more object_listing to retrieve?
            if (object_listing.isTruncated()) {
                object_listing = s3.listNextBatchOfObjects(object_listing);
            } else {
                break;
            }
        }
        ;

        System.out.println(" - removing versions from bucket");
        VersionListing version_listing = s3.listVersions(new ListVersionsRequest().withBucketName(bucket_name));
        while (true) {
            for (Iterator<?> iterator = version_listing.getVersionSummaries().iterator(); iterator.hasNext();) {
                S3VersionSummary vs = (S3VersionSummary) iterator.next();
                s3.deleteVersion(bucket_name, vs.getKey(), vs.getVersionId());
            }

            if (version_listing.isTruncated()) {
                version_listing = s3.listNextBatchOfVersions(version_listing);
            } else {
                break;
            }
        }

        System.out.println(" OK, bucket ready to delete!");
        s3.deleteBucket(bucket_name);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}

From source file:com.emc.ecs.sync.util.AwsS3Util.java

License:Open Source License

public static ListIterator<S3ObjectVersion> listVersions(SyncPlugin parentPlugin, AmazonS3 s3, String bucket,
        String key, String relativePath) {
    List<S3ObjectVersion> versions = new ArrayList<>();

    VersionListing listing = null;/*from w  w  w. ja v  a2  s. c o m*/
    do {
        if (listing == null)
            listing = s3.listVersions(bucket, key, null, null, "/", null);
        else
            listing = s3.listNextBatchOfVersions(listing);

        for (S3VersionSummary summary : listing.getVersionSummaries()) {

            if (summary.getKey().equals(key)) {
                versions.add(new S3ObjectVersion(parentPlugin, s3, bucket, key, summary.getVersionId(),
                        summary.isLatest(), summary.isDeleteMarker(), summary.getLastModified(),
                        summary.getETag(), relativePath, summary.getSize()));
            }
        }
    } while (listing.isTruncated());

    // sort chronologically
    Collections.sort(versions, new VersionComparator());
    return versions.listIterator();
}