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

Author - Webner
16.09.2015
|
3 Comments
||

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());

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

3 responses on “SFDC Apex – Get list of all the file names from Amazon S3 bucket

  1. sakshi says:

    How can I implement this in salesforce ?

    Please do help

  2. Teja says:

    Hai there

    How can this code be implemented in salesforce. Please help.

    Thanx

Leave a Reply

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