I need to know the number of files that are stored under S3
bucket. Currently, ObjectListing
doesn't has a method such as count
or numberOfObject
.
However, it has a method that will return a List of S3ObjectSummary
.
public java.util.List<S3ObjectSummary> getObjectSummaries()
Since it is a List
, I can call size()
method but is it accurate and right thing to assume that the size of getObjectSummaries()
List is the same number of objects that are stored under a bucket?
No -- it is more accurate to say that the size of getObjectSummaries()
is the number of objects in the page of results from listObjects()
that you received. Otherwise, yes -- it is correct that each object summary in the list should correspond to an S3 object.
AWS pages the results of large result sets to 1,000 results per page. In a general case, you will need to expect to make this call more than once:
If isTruncated() is true, call getNextMarker() to get the next marker to use in your next ListObjectsRequest to listObjects().
If false, this last response you got will be the last one you need to handle. You can finish counting your objects at this point.
With that in mind, this isn't a great solution for very large buckets since you have to enumerate the whole bucket. Here's an alternative solution using s3cmd that you may be able to call from Java.