SFDC Apex – Get list of all the file names from Amazon S3 bucket

|
| By Webner

I have two buckets in AWS S3. For one of the buckets, code given below returns all the file names (more than 1000) but the same code returns only 1000 file names for 2nd bucket:

ObjectListing objects = s3.listObjects("bucket.new.test");
do {
for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
String key = objectSummary.getKey();
System.out.println(key);
}
objects = s3.listNextBatchOfObjects(objects);
} while (objects.isTruncated());

My 2nd bucket has hierarchical structure (like folder1/folder2/filename.jpg). I fixed the issue by changing my code to use addAll instead of for loop to add object one by one. This is modified code:

List<S3ObjectSummary> keyList = new ArrayList<S3ObjectSummary>();

ObjectListing object = s3.listObjects("bucket.new.test");

keyList = object.getObjectSummaries();

object = s3.listNextBatchOfObjects(object);

 while (object.isTruncated()){

keyList.addAll(current.getObjectSummaries());

object = s3.listNextBatchOfObjects(current);

}

keyList.addAll(object.getObjectSummaries());

3 comments

Leave a Reply

Your email address will not be published. Required fields are marked *