Amazon S3 - List all zip files recursively into S3 bucket using Java API
I have an S3 bucket on Amazon and am trying to get a list of all zip files located in folders under the bucket recursively.
For example, 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 am returning an empty list.
Am I doing something wrong? Is there a way to recursively get a list of zip files from the bucket?
+3
source to share
1 answer
The only thing I can see is the connection to your S3 bucket. Try the following and it might help
AWSCredentials credentials = new BasicAWSCredentials(accessKeyId,secretAccessKey);
AmazonS3 s3Client = new AmazonS3Client(credentials);
ObjectListing objects = s3Client.listObjects(listobjectrequest);
+1
source to share