Skip to content

Instantly share code, notes, and snippets.

@kennycyb
Created October 31, 2023 14:12
Show Gist options
  • Save kennycyb/1a13a402fcb1b1c9ca3e00811e7da46b to your computer and use it in GitHub Desktop.
Save kennycyb/1a13a402fcb1b1c9ca3e00811e7da46b to your computer and use it in GitHub Desktop.
AWS | S3 | Bucket List
```
#!/bin/bash
# List all S3 buckets
buckets=$(aws s3 ls | awk '{print $3}')
# Print the list of buckets and their region
echo "List of S3 buckets and their regions:"
for bucket in $buckets
do
region=$(aws s3api get-bucket-location --bucket $bucket | jq '.LocationConstraint' | sed 's/"//g')
if [ "$region" == "null" ]; then
region="us-east-1"
fi
echo "$bucket,$region" >> bucket_list.csv
done
# Print the list of buckets and their region to the console
echo "List of S3 buckets and their regions:"
cat bucket_list.csv
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment