/*
* Create a S3ServiceMulti object with an event listener that responds to
* ListObjectsEvent notifications and populates a complete object listing.
*/
final S3ServiceMulti s3Multi = new S3ServiceMulti(s3Service, new S3ServiceEventAdaptor() {
public void s3ServiceEventPerformed(ListObjectsEvent event) {
if (ListObjectsEvent.EVENT_IN_PROGRESS == event.getEventCode()) {
Iterator chunkIter = event.getChunkList().iterator();
while (chunkIter.hasNext()) {
S3ObjectsChunk chunk = (S3ObjectsChunk) chunkIter.next();
if (log.isDebugEnabled()) {
log.debug("Listed " + chunk.getObjects().length
+ " objects and " + chunk.getCommonPrefixes().length
+ " common prefixes in bucket '" + bucketName
+ "' using prefix=" + chunk.getPrefix()
+ ", delimiter=" + chunk.getDelimiter());
}
allObjects.addAll(Arrays.asList(chunk.getObjects()));
lastCommonPrefixes.addAll(Arrays.asList(chunk.getCommonPrefixes()));
}
} else if (ListObjectsEvent.EVENT_ERROR == event.getEventCode()) {
s3ServiceExceptions[0] = new S3ServiceException(
"Failed to list all objects in S3 bucket",
event.getErrorCause());
}
}
});
// The first listing partition we use as a starting point is the target path.
String[] prefixesToList = new String[] {targetPath};
int currentDepth = 0;
while (currentDepth <= toDepth && prefixesToList.length > 0) {
if (log.isDebugEnabled()) {
log.debug("Listing objects in '" + bucketName + "' using "
+ prefixesToList.length + " prefixes: "
+ Arrays.asList(prefixesToList));
}
// Initialize the variables that will be used, or populated, by the
// multi-threaded listing.
lastCommonPrefixes.clear();
final String[] finalPrefixes = prefixesToList;
final String finalDelimiter = (currentDepth < toDepth ? delimiter : null);
/*
* Perform a multi-threaded listing, where each prefix string
* will be used as a unique partition to be listed in a separate thread.
*/
(new Thread() {
public void run() {
s3Multi.listObjects(bucketName, finalPrefixes,
finalDelimiter, Constants.DEFAULT_OBJECT_LIST_CHUNK_SIZE);
};
}).run();
// Throw any exceptions that occur inside the threads.
if (s3ServiceExceptions[0] != null) {