2013-02-08

List all objects from S3 bucket by Java code, but result limited 1000 items only.

Please refer to https://forums.aws.amazon.com/thread.jspa?messageID=347655
Solution 1.
AmazonS3 s3 = new AmazonS3Client();
ObjectListing current = s3.listObjects(new ListObjectsRequest().withBucketName("BucketName"));
List<S3ObjectSummary> keyList = current.getObjectSummaries();
ObjectListing next = s3.listNextBatchOfObjects(current);
keyList.addAll(next.getObjectSummaries());
while (next.isTruncated()) {
    current=s3.listNextBatchOfObjects(next);
    keyList.addAll(current.getObjectSummaries());
    next = s3.listNextBatchOfObjects(current);
}
keyList.addAll(next.getObjectSummaries());

Solution 2.
List<S3ObjectSummary> keyList = new ArrayList<S3ObjectSummary>();
AmazonS3 s3 = new AmazonS3Client();
for ( S3ObjectSummary summary : S3Objects.inBucket(s3, "BucketName") ) {
    keyList.add(summary);
}

No comments: