Count Number of Objects in S3 Bucket

avatar

Borislav Hadzhiev

Last updated: Sep 20, 2021

banner

Photo from Unsplash

Table of Contents #

  1. Count Number of Objects in an S3 Bucket with AWS Console
  2. Count Number of Objects in an S3 Bucket with AWS CLI
  3. Count Objects in a Folder of an S3 Bucket with AWS CLI

Count Number of Objects in an S3 Bucket with AWS Console #

To count the number of objects in an S3 bucket:

  1. Open the AWS S3 console and click on your bucket's name
  2. In the Objects tab, click the top row checkbox to select all files and folders or select the folders you want to count the files for
  3. Click on the Actions button and select Calculate total size

count bucket objects console 4. The Summary section of the page will display the Total number of objects

total number of objects

The page also displays the total number of objects in each of the selected folders.

total number of objects in folder

If you have thousands of files and folders in your bucket, it's easier to run the AWS CLI s3 ls command to count the bucket's objects.

Count Number of Objects in an S3 Bucket with AWS CLI #

To count the number of objects in an S3 bucket with the AWS CLI, use the s3 ls command, passing in the recursive, human-readable and summarize parameters.

shell
aws s3 ls s3://YOUR_BUCKET --recursive --human-readable --summarize

count bucket objects

Replace the YOUR_BUCKET placeholder with the name of your S3 bucket, otherwise you'd get an AccessDenied exception.

The bucket in the example above contains of a total of 5 objects.

We specified the following parameters in the call to the s3 ls command:

NameDescription
recursiveapplies the s3 ls command to all files under the specified directory
human-readabledisplays the file sizes in human-readable format
summarizedisplays the number of objects and total size of the files
List requests are associated with a cost. At the time of writing the cost of 1,000 list requests is $0.005 in the us-east-1 region.

Count Objects in a Folder of an S3 Bucket with AWS CLI #

To count the number of objects in a specific folder in your S3 bucket, use the s3 ls command and specify the path of the directory, e.g. s3://my-bucket/images/.

shell
aws s3 ls s3://YOUR_BUCKET/folder/ --recursive --human-readable --summarize

count bucket objects in folder

The output shows that the personal-website-bac/ directory consists of 4 objects.

Further Reading #

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.