Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License").
   * You may not use this file except in compliance with the License.
   * A copy of the License is located at
   *
   *  http://aws.amazon.com/apache2.0
   *
  * or in the "license" file accompanying this file. This file is distributed
  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  * express or implied. See the License for the specific language governing
  * permissions and limitations under the License.
  */
 package com.amazonaws.services.s3.iterable;
 
 
Provides an easy way to iterate Amazon S3 objects in a "foreach" statement. For example:
 for ( S3ObjectSummary summary : S3Objects.withPrefix(s3, "my-bucket", "photos/") ) {
     System.out.printf("Object with key '%s'\n", summary.getKey());
 }
 

The list of com.amazonaws.services.s3.model.S3ObjectSummarys will be fetched lazily, a page at a time, as they are needed. The size of the page can be controlled with the withBatchSize(int) method.

 
 public class S3Objects implements Iterable<S3ObjectSummary> {
 
     private AmazonS3 s3;
     private String prefix = null;
     private String bucketName;
     private Integer batchSize = null;
 
     private S3Objects(AmazonS3 s3String bucketName) {
         this. = s3;
         this. = bucketName;
     }

    
Constructs an iterable that covers all the objects in an Amazon S3 bucket.

Parameters:
s3 The Amazon S3 client.
bucketName The bucket name.
Returns:
An iterator for object summaries.
 
     public static S3Objects inBucket(AmazonS3 s3String bucketName) {
         return new S3Objects(s3bucketName);
     }

    
Constructs an iterable that covers the objects in an Amazon S3 bucket where the key begins with the given prefix.

Parameters:
s3 The Amazon S3 client.
bucketName The bucket name.
prefix The prefix.
Returns:
An iterator for object summaries.
 
     public static S3Objects withPrefix(AmazonS3 s3String bucketNameString prefix) {
         S3Objects objects = new S3Objects(s3bucketName);
         objects.prefix = prefix;
         return objects;
     }

    
Sets the batch size, i.e., how many com.amazonaws.services.s3.model.S3ObjectSummarys will be fetched at once.

Parameters:
batchSize How many object summaries to fetch at once.
 
     public S3Objects withBatchSize(int batchSize) {
         this. = batchSize;
         return this;
     }
 
     public Integer getBatchSize() {
         return ;
     }
 
     public String getPrefix() {
         return ;
    }
    public String getBucketName() {
        return ;
    }
    public AmazonS3 getS3() {
        return ;
    }
    private class S3ObjectIterator implements Iterator<S3ObjectSummary> {
        private ObjectListing currentListing = null;
        private Iterator<S3ObjectSummarycurrentIterator = null;
        @Override
        public boolean hasNext() {
            prepareCurrentListing();
            return .hasNext();
        }
        @Override
        public S3ObjectSummary next() {
            prepareCurrentListing();
            return .next();
        }
        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
        private void prepareCurrentListing() {
            while (  == null || (!.hasNext() && .isTruncated()) ) {
                if (  == null ) {
                    ListObjectsRequest req = new ListObjectsRequest();
                    req.setBucketName(getBucketName());
                    req.setPrefix(getPrefix());
                    req.setMaxKeys(getBatchSize());
                     = getS3().listObjects(req);
                } else {
                     = getS3().listNextBatchOfObjects();
                }
                 = .getObjectSummaries().iterator();
            }
        }
    }
    @Override
    public Iterator<S3ObjectSummaryiterator() {
        return new S3ObjectIterator();
    }
New to GrepCode? Check out our FAQ X