我们可以迭代Amazon S3中的完整对象集

ZZz*_*Zzz 7 java amazon-s3

我试图在S3存储桶中打印所有对象的元数据.但是,它不会返回超过1000个对象的结果.我试过实施objectListing.isTruncated()它并没有帮助.这是我列出超过1000个对象的示例代码.

 ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
            .withBucketName(bucketName);
    ObjectListing objectListing;
    do {
        objectListing = s3client.listObjects(listObjectsRequest);
        for (S3ObjectSummary objectSummary :
                objectListing.getObjectSummaries()) {
            System.out.println( " - " + objectSummary.getKey() + "  " +
                    "(size = " + objectSummary.getSize() +
                    ")");

            listObjectsRequest.setMarker(objectListing.getNextMarker());
        }
        listObjectsRequest.setMarker(objectListing.getNextMarker());
    } while (objectListing.isTruncated());
Run Code Online (Sandbox Code Playgroud)

mad*_*ead 11

对于所有在2018年阅读此内容的人.Java SDK中有一个新的API,它允许您非常轻松地遍历S3存储桶中的对象,而无需使用分页:

AmazonS3 s3 = AmazonS3ClientBuilder.standard().build();

S3Objects.inBucket(s3, "bucket").forEach((S3ObjectSummary objectSummary) -> {
    // TODO: Consume `objectSummary` the way you need
    // System.out.println(objectSummary.key);
});
Run Code Online (Sandbox Code Playgroud)


mad*_*ead 6

亚马逊最近发布了适用于 Java 2.x 的 AWS 开发工具包。API 发生了变化,因此这里是 SDK 2.x 版本:

S3Client client = S3Client.builder().region(Region.US_EAST_1).build();
ListObjectsV2Request request = ListObjectsV2Request.builder().bucket("the-bucket").prefix("the-prefix").build();
ListObjectsV2Iterable response = client.listObjectsV2Paginator(request);

for (ListObjectsV2Response page : response) {
    page.contents().forEach(x -> System.out.println(x.key()));
}
Run Code Online (Sandbox Code Playgroud)

ListObjectsV2Iterable也很懒:

当调用该操作时,将返回该类的一个实例。此时,尚未进行任何服务调用,因此不能保证请求有效。当您迭代可迭代对象时,SDK 将通过进行服务调用来开始延迟加载响应页面,直到没有页面剩余或迭代停止。如果您的请求中有错误,只有在开始迭代可迭代对象后您才会看到失败。


ZZz*_*Zzz 4

这解决了我的问题。我设置了一个标记并截断了我的列表,并且能够打印所有对象(超过 1000 个)。

 ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
     .withBucketName(bucketName);
 ObjectListing objectListing;
 do {
     objectListing = s3.listObjects(listObjectsRequest);
     System.out.println("Enter the path where to save yout file");
     Scanner scan = new Scanner(System.in);
     String path = scan.nextLine();
     fileOne = new File(path);
     fw = new FileWriter(fileOne.getAbsoluteFile(), true);
     bw = new BufferedWriter(fw);
     bw.write("Writing data to file");
     bw.write("\n");
     for (S3ObjectSummary objectSummary: objectListing.getObjectSummaries()) {
         String key = objectSummary.getKey();
         String dummyKey = key.substring(2);
         if (dummyKey.equalsIgnoreCase("somestring")) {
             S3Object s3object = s3.getObject(new GetObjectRequest(bucketName, key));
             BufferedReader reader = new BufferedReader(new InputStreamReader(s3object.getObjectContent()));
             String line;
             int i = 0;
             while ((line = reader.readLine()) != null) {
                 if (i > 0) {
                     bw.append(line + "," + s3object.getKey().substring(0, 2));
                     bw.append(objectSummary.getLastModified().toString());
                     bw.newLine();
                 }
                 i++;
                 System.out.println(line);
             }
         }
         //                    bw.close();
     }
     listObjectsRequest.setMarker(objectListing.getNextMarker());
 } while (objectListing.isTruncated());
Run Code Online (Sandbox Code Playgroud)