Get Folder Size in AWS S3 Bucket
In this article
To get the folder size in an AWS S3 bucket, you must sum the sizes of all objects under the folder's prefix, because S3 has no native folder concept—folders are just shared key prefixes. You can do this with the AWS CLI, the AWS Management Console, or SDKs, and the method you choose depends on whether you need a quick estimate or an exact total.
Background: How S3 Stores Data
Amazon S3 is an object storage service that uses a flat namespace. What looks like a folder in the console is actually a prefix—a string that precedes the object key. For example, s3://my-bucket/logs/2024/ is a prefix, and all objects with keys starting with that string are displayed as if they were inside a folder. There is no metadata that stores the size of a "folder" itself; the only way to know the size is to list the objects and add up their Size attributes.
This design has an important consequence: the size of a prefix can change as objects are added or deleted, and the total can be large (terabytes or more). For very large buckets, a simple list operation may take a long time or require pagination.
How to Get Folder Size with the AWS CLI
The most common method is to use the AWS CLI command aws s3 ls with the --summarize and --recursive flags. The command lists all objects under the prefix and prints a summary with the total number of objects and total size.
- Open a terminal where the AWS CLI is installed and configured with credentials that have
s3:ListBucketpermission. - Run the following command, replacing
my-bucketandpath/to/folderwith your bucket name and prefix:
aws s3 ls s3://my-bucket/path/to/folder --recursive --summarizeThe output ends with lines like:
Total Objects: 1234
Total Size: 567890123The total size is in bytes. To get a human-readable size in MB or GB, you can use the --human-readable flag:
aws s3 ls s3://my-bucket/path/to/folder --recursive --human-readable --summarizeThis prints the total size in a format like 541.2 MiB.
Note that the CLI uses the ListObjectsV2 API under the hood, which paginates through the bucket. For very large folders, the command may take a while, but it will eventually produce the correct total.
Using the AWS Management Console
The S3 console does not display a folder size directly. However, you can get an approximate size by selecting all objects in a folder and looking at the "Total size" in the top bar. This method works only if the folder contains a manageable number of objects (up to a few thousand); for larger folders, the console may not load all objects, and the total will be incomplete.
To do this:
- Navigate to your bucket in the S3 console.
- Click into the folder (prefix) you want to measure.
- Click the checkbox in the header row to select all visible objects, or press
Ctrl+A(Windows) orCmd+A(Mac). - Look at the top of the page—the console shows the number of selected objects and the total size.
This method is quick for small folders but is not reliable for production use. For exact totals, use the CLI or SDK.
Using AWS SDKs (Python, Java, etc.)
If you need to automate the process or integrate it into an application, use an AWS SDK. The following Python example uses the boto3 library to sum the sizes of all objects under a prefix:
import boto3
s3 = boto3.client('s3')
bucket = 'my-bucket'
prefix = 'path/to/folder/'
total_size = 0
object_count = 0
paginator = s3.get_paginator('list_objects_v2')
for page in paginator.paginate(Bucket=bucket, Prefix=prefix):
for obj in page.get('Contents', []):
total_size += obj['Size']
object_count += 1
print(f'Total size: {total_size} bytes')
print(f'Object count: {object_count}')This code handles pagination automatically, so it works for large folders. You must have the s3:ListBucket permission for the bucket.
Common Problems and Misconceptions
One common mistake is to use the aws s3 du command, which is not a real AWS CLI command. The correct command is aws s3 ls --summarize. Another issue is that the console's folder size display is often inaccurate for large folders because it only loads a limited number of objects. Also, remember that the size includes all versions of objects if versioning is enabled—the CLI's --summarize includes only the current versions unless you use --include-versions (which is not supported with ls). For versioned buckets, you need to use the ListObjectVersions API to get the total size of all versions.
Another misconception is that the folder size is stored somewhere. It is not; it is computed on the fly. This means that every time you run the command, it will list all objects, which can be slow for huge buckets. For very large buckets (millions of objects), consider using Amazon S3 Inventory to get a daily or weekly CSV file with object sizes, then sum the sizes for the prefix.
Costs and Performance Considerations
Listing objects in S3 is free of charge, but it does count against your request rate. If you run the command frequently on a large bucket, you may hit throttling limits. For production monitoring, use S3 Inventory, which is a managed service that generates a daily or weekly report of all objects and their metadata. The inventory files are stored in a separate bucket, and you can query them with Athena to get folder sizes without scanning the entire bucket each time.
For one-off checks, the CLI is sufficient. For ongoing monitoring, set up S3 Inventory and use Athena or a script to aggregate the data.
Alternatives: S3 Storage Lens and Cost Explorer
AWS also offers S3 Storage Lens, which provides organization-wide visibility into storage usage, including metrics like total storage and object count, but it does not break down by prefix by default. You can create a Storage Lens dashboard with prefix-level aggregation, but that requires additional configuration. For cost purposes, AWS Cost Explorer can show you the cost of a bucket, but not the size of a specific folder.
If you need to measure a folder size for billing or capacity planning, the CLI or SDK approach is the most direct and accurate.
Frequently Asked Questions
Why doesn't the S3 console show folder size?
The console is designed to show object counts and sizes for the currently displayed list. Because folders are not real entities, the console cannot show a folder size without listing all objects under the prefix, which it avoids for performance reasons. The CLI's --summarize flag does this listing and is the recommended way.
Does the folder size include subfolders?
Yes, because a "folder" is just a prefix. If you run the command on s3://bucket/logs/, it includes all objects under logs/, including those in logs/2024/ and logs/2025/. To get the size of just one subfolder, use its full prefix.
How do I get the size of a folder in a bucket with versioning enabled?
The aws s3 ls --summarize command only counts the current versions. To include all versions, you need to use the ListObjectVersions API and sum the sizes of all version objects. You can do this with a script using an SDK, or use S3 Inventory with the Versions option.
Is there a way to get folder size without using the CLI?
Yes, you can use the AWS SDK in any language, or use the S3 Console's select-all method for small folders. For large folders, you can also use S3 Inventory and query the results with Athena. The CLI is the simplest for one-off checks.
Why does the CLI take so long for a large folder?
The CLI must list every object under the prefix, which requires multiple API calls (each returns up to 1,000 objects). For millions of objects, this can take minutes or longer. If you need faster results, use S3 Inventory, which precomputes the data.