Skip to content

How do I calculate the total size of an Amazon S3 bucket and the number of objects?

5 minute read
0

I want to know the total size of an Amazon Simple Storage Service (Amazon S3) bucket and number of objects.

Resolution

To get the bucket size and number of objects, use the Amazon S3 console, Amazon S3 Storage Lens, Amazon CloudWatch, or S3 Inventory. You can also use the AWS Command Line Interface (AWS CLI) to get the S3 bucket size and number of objects.

Important: If you turned on bucket versioning, then turn it off. You can't calculate the total size and number of objects in an Amazon S3 bucket when versioning is turned on.

Use the Amazon S3 console

To calculate your bucket size, use the Calculate total size action.

Note: Amazon S3 doesn't calculate multipart uploads and previous or noncurrent versions in the total bucket size. Amazon S3 calculates only the total number of objects for the current or newest version of each object that you store in the bucket.

Complete the following steps:

  1. Open the Amazon S3 console.
  2. Select the bucket name that you want to calculate.
  3. In Objects, choose all of the objects in the bucket.
  4. Choose Actions, and then choose Calculate total size.
  5. In Summary, note the numbers in the Total number of objects and Total size fields.

Use S3 Storage Lens

Complete the following steps:

  1. Open the Amazon S3 console.
  2. In the navigation pane, expand Storage lens, and then choose Dashboards.
  3. In Dashboards, choose default-account-dashboard.
  4. Expand Filters, and then choose the Buckets dropdown list.
  5. Select the bucket that you want to calculate, and then choose Apply.
  6. In Snapshot, view the Total storage and Object count metrics for the bucket.

You can use Storage Lens groups to combine metrics. You can also choose advanced metrics for additional metrics and features at an additional cost. For information about advanced metrics prices, see Amazon S3 pricing.

Use CloudWatch

Complete the following steps:

  1. Open the CloudWatch console in the same AWS Region as your S3 bucket.
  2. In the navigation pane, expand Metrics, and then choose All metrics.
  3. In Metrics, choose S3.
  4. Choose Storage Metrics.
  5. In BucketName, choose the bucket that you want to calculate.
  6. In the Storage metrics search bar, enter BucketSizeBytes to show the bucket size.
  7. In the Storage metrics search bar, enter StorageType to show the number of objects.

CloudWatch records Amazon S3 metrics once each day, so metrics might not display the most updated information. However, CloudWatch monitors your AWS resources and applications in real time.

Note: If there's a difference in the methods to get metrics, then either object versioning is turned on or uploads are incomplete. For more information, see Why is there a discrepancy in storage and size metrics between the Amazon S3 console, CloudWatch, and S3 Storage Lens?

Use S3 Inventory

Amazon S3 inventory reports list your objects and their metadata daily or weekly. After you configure Amazon S3 Inventory, you can use Amazon Athena to query S3 Inventory files for bucket size and number of objects. For more information, see How can I use Athena to query my Amazon S3 Inventory reports?

Note: If you configured an S3 Inventory report and received an Access Denied error, then see Why can't I generate an Amazon S3 Inventory report?

Use the AWS CLI

Note: If you receive errors when you run AWS CLI commands, then see Troubleshooting errors for the AWS CLI. Also, make sure that you're using the most recent AWS CLI version.

To get the size and number of objects of an Amazon S3 bucket, run the following Is command:

aws s3 ls --summarize --human-readable --recursive s3://bucket-name/

Note: Replace bucket-name with your S3 bucket name.

To get the size of a directory, run the following Is command:

aws s3 ls --summarize --human-readable --recursive s3://bucket-name/directory-name

Note: Replace directory-name with your directory name.

Create a lifecycle rule

If you receive timeout or XML errors, then create a lifecycle configuration rule to delete expired markers.

Complete the following steps:

  1. Open the Amazon S3 console.
  2. Choose the Management tab.
  3. Choose Create new policy.
  4. Add the name of the policy.
  5. Choose Select - Delete expired delete markers or incomplete multipart uploads.
  6. (Optional) If you didn't activate versioning on your bucket, then choose Delete incomplete multipart uploads.

For more information, see Removing expired object delete markers in a versioning-enabled bucket.

Related information

Find out the size of your Amazon S3 buckets

How do I use the S3 Storage Lens dashboard to view prefix-level metrics for my Amazon S3 bucket?

Amazon S3 daily storage metrics for buckets in CloudWatch

Monitoring metrics with Amazon CloudWatch

AWS OFFICIALUpdated a year ago