Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Size of folder in s3 bucket

Tags:

php

amazon-s3

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/...

like image 599
Stranger Avatar asked Dec 28 '22 00:12

Stranger


2 Answers

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.");
}
like image 156
jimpic Avatar answered Jan 14 '23 06:01

jimpic


if you would want to use boto in python here is a small script that you may try:

import boto
conn=boto.connect_s3('api_key','api_secret')
bucket=conn.get_bucket('bucketname');
keys=bucket.list('path')
size=0
for key in keys:
        size+= key.size
print size
like image 35
sulabh Avatar answered Jan 14 '23 08:01

sulabh