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/...
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.");
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With