Mr.Stranger Mr.Stranger - 15 days ago 5
PHP Question

Size of folder in s3 bucket

i am working on amazon s3 bucket. And i need to find a size of the folder inside a bucket through the code. I'm not finding any method to find the size of the folder directly. So is there any other way to achieve this function?

EDIT : I'm aware that there is nothing called folders in s3 bucket. But i need to find the size of all files looking like a folder folder structure. That is, if the structure is like this,

https://s3.amazonaws.com/****/uploads/storeeoll48jipuvjbqufcap3p6on6er2bwsufv5ojzqnbe01xvw0fy58x65.png
then i need to find the size of all files with the structure,
https://s3.amazonaws.com/****/uploads/...

Answer

From AwsConsoleApp.java AWS SDK sample:

List<Bucket> buckets = s3.listBuckets();
long totalSize  = 0;
int  totalItems = 0;
for (Bucket bucket : buckets)
{
    ObjectListing objects = s3.listObjects(bucket.getName());
    do {
        for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
            totalSize += objectSummary.getSize();
            totalItems++;
        }
        objects = s3.listNextBatchOfObjects(objects);
    } while (objects.isTruncated());
    System.out.println("You have " + buckets.size() + " Amazon S3 bucket(s), " +
                    "containing " + totalItems + " objects with a total size of " + totalSize + " bytes.");
}
Comments