简体   繁体   中英

Amazon S3 - 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 eg, 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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM