Created
October 31, 2023 14:12
-
-
Save kennycyb/1a13a402fcb1b1c9ca3e00811e7da46b to your computer and use it in GitHub Desktop.
AWS | S3 | Bucket List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
#!/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