Skip to content

Instantly share code, notes, and snippets.

@WarFox
Last active November 8, 2023 14:33
Show Gist options
  • Save WarFox/0b551c794b30b5e50496103410abf98c to your computer and use it in GitHub Desktop.
Save WarFox/0b551c794b30b5e50496103410abf98c to your computer and use it in GitHub Desktop.
List empty S3 buckets
counter=count
count=0
for b in $(aws s3 ls | cut -d" " -f3)
do
(($counter++))
echo -n $count,"$b",
tags="$(aws s3api get-bucket-tagging --bucket $b --query 'TagSet[*].Value' | jq -r '@csv')"
echo $tags
done
echo "Index,Bucket Name,Status,Repository,Created By"
counter=count
count=0
for b in $(aws s3 ls | cut -d" " -f3)
do
if [[ "$(aws s3api list-objects-v2 --bucket $b --max-items 1)" == "" ]]
then
(($counter++))
echo -n $count,$b",BUCKET EMPTY,"
tags="$(aws s3api get-bucket-tagging --bucket $b --query 'TagSet[?Key==`Repository` || Key == `Created_by`].Value' | jq -r '@csv')"
echo $tags
fi
done
for b in $(aws s3 ls | cut -d" " -f3)
do
if [[ "$(aws s3api list-objects-v2 --bucket $b --max-items 1)" == "" ]]
then
echo -n "|" $b "| BUCKET EMPTY |"
tags="$(aws s3api get-bucket-tagging --bucket $b --query 'TagSet[?Key==`Repository` || Key == `CreatedBy`].Value' | jq -r '@csv' | sed 's/\",\"/\" | \"/g')"
echo $tags " |"
fi
done
# Here the Repository and CreatedBy tags will be shown along with the bucket name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment