Skip to content

Instantly share code, notes, and snippets.

@guozi
Created November 26, 2021 09:51
Show Gist options
  • Select an option

  • Save guozi/58f503487f06f2da756937e96e515152 to your computer and use it in GitHub Desktop.

Select an option

Save guozi/58f503487f06f2da756937e96e515152 to your computer and use it in GitHub Desktop.

Revisions

  1. guozi created this gist Nov 26, 2021.
    23 changes: 23 additions & 0 deletions ListKeysV1.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    public class ListKeysV1 {
    private static String bucketName = "***bucket name***";

    public static void main(String[] args) throws IOException {
    AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());

    ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
    .withBucketName(bucketName)
    .withPrefix("m");
    ObjectListing objectListing;

    do {
    objectListing = s3client.listObjects(listObjectsRequest);
    for (S3ObjectSummary objectSummary :
    objectListing.getObjectSummaries()) {
    System.out.println( " - " + objectSummary.getKey() + " " +
    "(size = " + objectSummary.getSize() +
    ")");
    }
    listObjectsRequest.setMarker(objectListing.getNextMarker());
    } while (objectListing.isTruncated());
    }
    }
    42 changes: 42 additions & 0 deletions ListKeysV2.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    public class ListKeys {
    private static String bucketName = "***bucket name***";

    public static void main(String[] args) throws IOException {
    AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());
    try {
    System.out.println("Listing objects");
    final ListObjectsV2Request req = new ListObjectsV2Request().withBucketName(bucketName);
    ListObjectsV2Result result;
    do {
    result = s3client.listObjectsV2(req);

    for (S3ObjectSummary objectSummary :
    result.getObjectSummaries()) {
    System.out.println(" - " + objectSummary.getKey() + " " +
    "(size = " + objectSummary.getSize() +
    ")");
    }
    System.out.println("Next Continuation Token : " + result.getNextContinuationToken());
    req.setContinuationToken(result.getNextContinuationToken());
    } while(result.isTruncated() == true );

    } catch (AmazonServiceException ase) {
    System.out.println("Caught an AmazonServiceException, " +
    "which means your request made it " +
    "to Amazon S3, but was rejected with an error response " +
    "for some reason.");
    System.out.println("Error Message: " + ase.getMessage());
    System.out.println("HTTP Status Code: " + ase.getStatusCode());
    System.out.println("AWS Error Code: " + ase.getErrorCode());
    System.out.println("Error Type: " + ase.getErrorType());
    System.out.println("Request ID: " + ase.getRequestId());
    } catch (AmazonClientException ace) {
    System.out.println("Caught an AmazonClientException, " +
    "which means the client encountered " +
    "an internal error while trying to communicate" +
    " with S3, " +
    "such as not being able to access the network.");
    System.out.println("Error Message: " + ace.getMessage());
    }
    }
    }