Created
April 22, 2015 18:46
-
-
Save filipenf/0528d26f0dba78b72b39 to your computer and use it in GitHub Desktop.
Print a list of aws buckets along with their tags
anyway to filter output to show only the buckets without any tags?
You could try this:
for BUCKET in $(aws s3api list-buckets | jq .Buckets[].Name -r); do
RESULT=$(aws s3api get-bucket-tagging --bucket $BUCKET 2>&1)
if [[ $RESULT =~ "(NoSuchTagSet)" ]]; then
echo $BUCKET
fi
done
Note you have to capture the error text and test for it rather than being able to check the response code, as unfortunately aws s3api
returns a success code even if the command errors:
$ aws s3api get-bucket-tagging --bucket $BUCKET
An error occurred (NoSuchTagSet) when calling the GetBucketTagging operation: The TagSet does not exist
$ echo $?
0
update the above line and it will give you clear output
echo $bucket '|' $tags >> buckets-tags.txt
I know this is old.. But you don't need jq
for the bucket list step. Also added some additional stuff to answer other questions.
for bucket in `aws s3api list-buckets --query "Buckets[].Name" --output text` ; do
echo $bucket
tags=$(aws s3api get-bucket-tagging --bucket $bucket 2>/dev/null| jq -cr '.[][] | {(.Key): .Value}')
output=${tags:-"no Tags assigned"}
echo $output
done
~
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try this one: