Search in sources :

Example 11 with S3VersionSummary

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

the class ListKeysVersioningEnabledBucket method main.

public static void main(String[] args) {
    Regions clientRegion = Regions.DEFAULT_REGION;
    String bucketName = "*** Bucket name ***";
    try {
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new ProfileCredentialsProvider()).withRegion(clientRegion).build();
        // Retrieve the list of versions. If the bucket contains more versions
        // than the specified maximum number of results, Amazon S3 returns
        // one page of results per request.
        ListVersionsRequest request = new ListVersionsRequest().withBucketName(bucketName).withMaxResults(2);
        VersionListing versionListing = s3Client.listVersions(request);
        int numVersions = 0, numPages = 0;
        while (true) {
            numPages++;
            for (S3VersionSummary objectSummary : versionListing.getVersionSummaries()) {
                System.out.printf("Retrieved object %s, version %s\n", objectSummary.getKey(), objectSummary.getVersionId());
                numVersions++;
            }
            // there are, retrieve them. Otherwise, exit the loop.
            if (versionListing.isTruncated()) {
                versionListing = s3Client.listNextBatchOfVersions(versionListing);
            } else {
                break;
            }
        }
        System.out.println(numVersions + " object versions retrieved in " + numPages + " pages");
    } catch (AmazonServiceException e) {
        // The call was transmitted successfully, but Amazon S3 couldn't process
        // it, so it returned an error response.
        e.printStackTrace();
    } catch (SdkClientException e) {
        // Amazon S3 couldn't be contacted for a response, or the client
        // couldn't parse the response from Amazon S3.
        e.printStackTrace();
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) SdkClientException(com.amazonaws.SdkClientException) VersionListing(com.amazonaws.services.s3.model.VersionListing) S3VersionSummary(com.amazonaws.services.s3.model.S3VersionSummary) AmazonServiceException(com.amazonaws.AmazonServiceException) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) Regions(com.amazonaws.regions.Regions) ListVersionsRequest(com.amazonaws.services.s3.model.ListVersionsRequest)

Aggregations

S3VersionSummary (com.amazonaws.services.s3.model.S3VersionSummary)9 VersionListing (com.amazonaws.services.s3.model.VersionListing)9 AmazonServiceException (com.amazonaws.AmazonServiceException)6 ListVersionsRequest (com.amazonaws.services.s3.model.ListVersionsRequest)5 Test (org.junit.Test)5 AmazonS3 (com.amazonaws.services.s3.AmazonS3)4 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)4 S3FileTransferRequestParamsDto (org.finra.herd.model.dto.S3FileTransferRequestParamsDto)4 AmazonClientException (com.amazonaws.AmazonClientException)3 MultiObjectDeleteException (com.amazonaws.services.s3.model.MultiObjectDeleteException)3 IOException (java.io.IOException)3 SdkClientException (com.amazonaws.SdkClientException)2 ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)2 Regions (com.amazonaws.regions.Regions)2 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)2 DeleteObjectsRequest (com.amazonaws.services.s3.model.DeleteObjectsRequest)2 ArrayList (java.util.ArrayList)2 ObjectNotFoundException (org.finra.herd.model.ObjectNotFoundException)2 RetryPolicy (com.amazonaws.retry.RetryPolicy)1 KeyVersion (com.amazonaws.services.s3.model.DeleteObjectsRequest.KeyVersion)1