Yesterday I had to find the count of objects in a folder in an S3 bucket. Unfortunately, I only had access to AWS via the command line and was working on a Windows Server.
Using AWS CLI
After digging around, I found the solution using PowerShells’ Measure-Object cmdlet.
The solution to getting the object count was:
aws s3 ls s3://{bucket}/path/to/files | Measure-Object
You can use it in local folders as well. It also can be used to get the minimum/maximum/average/total size of the folder too so quite handy to get some quick stats about a folder/bucket
Using AWS Tools for PowerShell
If you have AWS Tools for PowerShell installed, you can achieve the same goal by running the Get-S3Object cmdlet like this:
Get-S3Object -BucketName {bucket} -Prefix path/to/files | Measure-Object
Alternatively, if you want to get the object count, you can run this as well:
(Get-S3Object -BucketName {bucket} -Prefix path/to/files).Count