aws java驱动程序没有循环键

时间:2014-08-21 01:20:32

标签: java amazon-web-services

我有以下代码:

AmazonS3 s3 = new AmazonS3Client(awsCredentials);
    ObjectListing listing = s3.listObjects(new ListObjectsRequest()
            .withBucketName( "myBucket" )
            .withPrefix(path));
   System.out.println("path we need: " + path);
    List<S3ObjectSummary> summaries = listing.getObjectSummaries();
    System.out.println(summaries.size());
    do {
        listing = s3.listNextBatchOfObjects (listing);
        for (S3ObjectSummary objectSummary : listing.getObjectSummaries()) {
            System.out.println(objectSummary.getKey());
        }
        listing.setMarker(listing.getNextMarker());
    }
    while(listing.isTruncated());

这似乎部分奏效。行

   System.out.println("path we need: " + path);
   System.out.println(summaries.size());

打印出我期望的线条。然而,当它在S3ObjectSummary上方循环时,它什么都没打印出来。我在这做错了什么?

1 个答案:

答案 0 :(得分:0)

您似乎在打印信息之前尝试获取下一组截断对象。如果没有任何内容被截断,这可能就是你没有得到结果的原因。我建议您更改循环代码以匹配以下内容并再次尝试。

    AmazonS3 s3 = new AmazonS3Client(creds);
    ObjectListing listing = s3.listObjects(new ListObjectsRequest().withBucketName("myBucket").withPrefix(path));
    System.out.println("path we need: " + path);
    System.out.println(listing.getObjectSummaries().size() + " | " + listing.isTruncated());
    while(listing.getObjectSummaries().size() > 0) {
        for (S3ObjectSummary objectSummary : listing.getObjectSummaries()) {
            System.out.println(objectSummary.getKey());
        }
        if(!listing.isTruncated()) break;
        listing = s3.listNextBatchOfObjects(listing);
        System.out.println(listing.getObjectSummaries().size() + " | " + listing.isTruncated());
    };

我已经在我自己的一个S3存储桶上进行了测试,它似乎对我有用。