简体   繁体   English

Amazon S3-使用Java API递归列出S3存储桶中的所有zip文件

[英]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. 我在亚马逊上有一个S3存储桶,试图递归获取位于存储桶下文件夹中的所有zip文件的列表。

For eg, my zip files are located as shown below : 例如,我的zip文件位于如下所示的位置:

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 ? 有什么方法可以从存储桶中递归获取zip文件列表吗?

Only thing I can see is getting connection to your S3 bucket.Try the following and it may help 我唯一能看到的就是与您的S3存储桶建立了连接。请尝试以下操作,这可能会有所帮助

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

ObjectListing objects = s3Client.listObjects(listobjectrequest);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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