The aws cli list s3 buckets command retrieves a list of all Amazon S3 buckets in your AWS account, returning their names and associated regions. It's a foundational tool for managing cloud storage resources via the command line.
This command runs against the AWS API using credentials configured in your environment. It returns a JSON-formatted response by default, including bucket names, creation dates, and region locations. You can filter and format output using options like --query or --output.
For example, running aws s3api list-buckets returns:
{
"Buckets": [
{
"Name": "my-data-backup",
"CreationDate": "2023-07-15T10:23:45.000Z"
},
{
"Name": "customer-photos-2024",
"CreationDate": "2024-01-03T08:12:10.000Z"
}
],
"Owner": {
"DisplayName": "user123",
"ID": "a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6"
}
}
When you run aws cli list s3 buckets, the CLI uses the AWS SDK to authenticate with your configured profile. Authentication relies on credentials stored in ~/.aws/credentials or available through IAM roles. Without valid credentials, the command fails with an error like "Unable to locate credentials."
By default, the command returns all buckets in the current AWS region. To list buckets across regions, use the --region flag or set the AWS_DEFAULT_REGION environment variable. For example:
aws s3api list-buckets --region us-east-1
Common issues include missing permissions. The AWS Identity and Access Management (IAM) policy must include s3:ListAllMyBuckets. A user without this permission receives an access denied error. According to AWS documentation, this action requires explicit permission even for the owner of the bucket.
Use the --query option to extract specific fields. For example:
aws s3api list-buckets --query 'Buckets[*].Name' --output text
This returns only bucket names, one per line:
my-data-backup
customer-photos-2024
Use --output json, --output table, or --output text to change formatting. Table output improves readability in terminal environments. Text output simplifies scripting.
| Option | Purpose | Example |
|---|---|---|
| --query | Filter and extract fields from JSON response | --query 'Buckets[?Name==`prod-backup`].CreationDate' |
| --output | Change output format | --output table |
| --region | Specify AWS region | --region eu-west-1 |
Some users confuse aws cli list s3 buckets with aws s3 ls. The latter lists objects within a bucket, not the buckets themselves. Running aws s3 ls without a bucket name shows nothing. Use aws s3api list-buckets to list buckets.
A common mistake is assuming the command lists all buckets globally. It only returns buckets in the specified region. To see all buckets across regions, you must query each region individually or use AWS Resource Groups Tag Editor.
Another confusion arises from the difference between s3api and s3 commands. The s3api subcommand accesses low-level S3 operations. The s3 command group handles higher-level actions like copying files. Use s3api list-buckets for consistent results.
I once ran aws cli list s3 buckets in a CI pipeline and got empty output. Debugging revealed the AWS role had s3:ListBucket but not s3:ListAllMyBuckets. Adding the latter permission resolved it. This highlights that permission granularity matters-just because you own a bucket doesn't mean you can list all of them.
Use aws list s3 buckets with scripts to audit storage usage. For example, combine it with jq to count buckets:
aws s3api list-buckets --query 'length(Buckets)' --output text
This returns a number like 12. Use it in monitoring tools to detect unexpected changes in bucket count.
To filter by name pattern, use --query with string matching. For example:
aws s3api list-buckets --query 'Buckets[?starts_with(Name, `logs-`)].[Name]' --output table
This lists only buckets whose names start with logs-. The result includes:
| Name |
|---|
| logs-production-2024 |
| logs-staging-2024 |
Use the --profile flag to switch between AWS profiles. If you have multiple accounts, this avoids credential conflicts. For example:
aws s3api list-buckets --profile dev-account
For automation, store the output in a file. Use --output json and pipe to a file:
aws s3api list-buckets --output json > s3-buckets.json
Later, parse the file with tools like jq or Python to analyze ownership, age, or size.
When troubleshooting, check the AWS CLI version. Older versions may not support newer query syntax. Run aws --version to verify. Update via package managers like apt or brew.
Use aws list s3 buckets to validate configurations during deployment. If a script creates a bucket but the list command doesn't show it, the creation failed silently. Always verify with the list command.