I am trying to find a faster way to count all of the objects within an s3 bucket using Amazon's AWS SDK.
private static int getBucketFileCount(AmazonS3 s3, ListObjectsV2Request req) {
ListObjectsV2Result result;
int fileCount = 0;
log.info("Counting s3 files");
do {
result = s3.listObjectsV2(req);
for (S3ObjectSummary objectSummary : result.getObjectSummaries()) {
fileCount++;
}
req.setContinuationToken(result.getNextContinuationToken());
} while (result.isTruncated() == true);
return fileCount;
}
However, this method is very slow and I have not been able to figure out a way to do it properly. I have found another answer that sort of helps, but cannot figure out the implementation exactly. Will getObjectSummaries get the count of objects stored in a S3 Bucket?
How do I use the getNextMarker() function with my current implementation? What do I need to change?
fileCount += result.getKeyCount()
as well asfileCount++
inside a for loop. Are you double counting? – Branch