How to Calculate the Size of an S3 Folder Using AWS CLI
Calculating the size of an S3 folder is a common task when working with Amazon Web Services. In this tutorial, you will learn how to calculate the size of an S3 folder using AWS CLI, getting the total size in gigabytes directly from the terminal.
This method is ideal for developers, DevOps engineers, and technical teams who need to audit storage usage, optimize costs, or analyze S3 buckets.
Requirements to Calculate S3 Folder Size
- AWS CLI installed
- AWS credentials configured using
aws configure - Permission to list objects in the S3 bucket
- Access to a terminal (Linux, macOS, or WSL)
How to Calculate the Size of an S3 Folder
Amazon S3 does not have real folders; it uses prefixes instead. To calculate the size of an S3 folder, you must sum the size of all objects that share the same prefix.
Command to Calculate S3 Folder Size in GB
aws s3 ls s3://BUCKET-NAME/path/to/folder/ --recursive \
| awk '{sum+=$3} END {printf "%.2f GB\n", sum/1024/1024/1024}'
AWS CLI Command Explanation
aws s3 lslists objects inside the S3 bucket--recursiveincludes all subfolders$3represents the file size in bytesawksums the total size- The final output is shown in gigabytes (GB)
Calculate S3 Folder Size in GiB
If you need a more precise value, you can calculate the S3 folder size in GiB:
aws s3 ls s3://BUCKET-NAME/path/to/folder/ --recursive \
| awk '{sum+=$3} END {printf "%.2f GiB\n", sum/1024/1024/1024}'
Alternative Method to Calculate S3 Folder Size
This approach is recommended when the bucket contains a large number of objects:
aws s3api list-objects-v2 \
--bucket BUCKET-NAME \
--prefix path/to/folder/ \
--query "Contents[].Size" \
--output json \
| jq 'add / 1024 / 1024 / 1024'
Common Issues When Calculating S3 Folder Size
The command does not show the total size
The --summarize option in aws s3 ls does not always display the total size when many objects are present. This is why using awk or s3api is the most reliable way to calculate the real size of an S3 folder.
Frequently Asked Questions About S3 Folder Size
Can I calculate the size of an S3 folder from the AWS Console?
No. The AWS Console does not show the total size of an S3 folder. Using AWS CLI is the recommended way to calculate the size of an S3 folder.
Does this method modify my S3 bucket?
No. These commands only list objects. No files are modified or deleted.
Conclusion
You now know how to calculate the size of an S3 folder using AWS CLI accurately and efficiently. This process is essential for monitoring storage usage, performing audits, and optimizing costs in Amazon S3.
Español

