-
-
Save filipenf/0528d26f0dba78b72b39 to your computer and use it in GitHub Desktop.
why do you mix back ticks and $() ?
for bucket in $(aws s3api list-buckets | jq .Buckets[].Name -r); do
tags=$(aws s3api get-bucket-tagging --bucket $bucket | jq -c '.[][] | {(.Key): .Value}' | tr '\n' '\t')
echo $bucket '|' $tags
done
I tried this....but seems like AWS CLI doestnt support the command - "$aws s3api list-buckets | jq .Buckets[].Name"
I tried this....but seems like AWS CLI doestnt support the command - "$aws s3api list-buckets | jq .Buckets[].Name"
What I think is that you need jq tool installed and available from the console where executing the command, as it is a simple command piped at the aws output result
anyway to filter output to show only the buckets without any tags?
I tried this....but seems like AWS CLI doesn't support the command - "$aws s3api list-buckets | jq .Buckets[].Name"
Try this one:
aws s3api list-buckets | jq -r '.Buckets[].Name'
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
~
Copy-Paste, optimized version for the lazy like me:
This one needs AWS CLI and JQ.