Saurabh Gokhale
Saurabh Gokhale

Reputation: 46425

List all the zip files recursively within S3 bucket using Java API

I've a S3 bucket on the Amazon and trying to get the list of all the zip files located within folders under the bucket recursively.

For e.g, my zip files are located as shown below :

bucket1/${user1}/${date1}/abc.zip
bucket1/${user2}/${date2}/xyz.zip
bucket1/${user3}/${date3}/mno.zip

bucketName=bucket1
prefix=bucket1/

Below is my code :

final AmazonS3 amazonS3 = AmazonS3Utils.getAmazonS3Client();
final ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName("bucket1")
                                                                            .withPrefix("bucket1/");
        
ObjectListing current = amazonS3.listObjects(listObjectsRequest);
final List<S3ObjectSummary> keyList = current.getObjectSummaries();

while (current.isTruncated()) 
{
   keyList.addAll(current.getObjectSummaries());
   current = amazonS3.listNextBatchOfObjects(current);
}
    
keyList.addAll(current.getObjectSummaries());
for(S3ObjectSummary summary : keyList)
{
    System.out.println(summary.getKey());
}

But I get back an empty list.

Am I doing anything wrong? Is there any way to recursively get list of zip files from the bucket?

Upvotes: 4

Views: 1298

Answers (1)

scalauser
scalauser

Reputation: 1327

Only thing I can see is getting connection to your S3 bucket.Try the following and it may help

AWSCredentials credentials = new BasicAWSCredentials(accessKeyId,secretAccessKey);
        AmazonS3 s3Client = new AmazonS3Client(credentials);

ObjectListing objects = s3Client.listObjects(listobjectrequest);

Upvotes: 1

Related Questions