Cloud storage has no folders. Whether it's AWS S3 or Azure Blob storage, a bucket is a flat list of files. Folders are emulated by treating a character in the name as a separator. A bucket isn't a disk either, so it has no allocated vs used space statistics. Objects are stored in blob storage arrays that use compression and deduplication.
If you want to find out what storage you're actually billed for, it's the total object size, not the actual physical storage. If you upload the same 1MB file 100 times you'll be billed for 100MB even if just 1MB are actually stored.
Calculating the total size of a bucket is too expensive, which is why it's not updated automatically. If calculating the size of a local folder is slow, imagine how much slower it is to calculate the size of a bucket with 1M objects that are actively being updated.
The only way to get the used space is to list the objects and sum their sizes. This operation is billed, no matter what tool is used to perform it.
The article Find out the size of your Amazon S3 buckets shows which tools and services can be used to monitor the actual size.
- Amazon S3 console
- S3 Storage Lens
- Amazon CloudWatch
- Amazon S3 Inventory
These services calculate and cache the size periodically, to avoid delays and extra billing.
The other option is to list and sum all objects either through the AWS CLI or code. You can use the code in Listing object keys programmatically to list all objects in a bucket one page at a time and sum the sizes:
public static async Task<long> GetBucketSizeAsync(IAmazonS3 client, string bucketName)
{
long total=0;
var request = new ListObjectsV2Request
{
BucketName = bucketName,
};
var response = new ListObjectsV2Response();
do
{
response = await client.ListObjectsV2Async(request);
total += response.S3Objects.Sum(obj =>obj.Size);
request.ContinuationToken = response.NextContinuationToken;
}
while (response.IsTruncated);
return total;
}
Remember, ListObjectsV2Async
is billed. You should cache the results instead of calling this all the time.