Skip to content

Instantly share code, notes, and snippets.

@jawsthegame
Created June 28, 2018 18:36
Show Gist options
  • Save jawsthegame/cb5ec5385405ec5c9be9dee9b2b74c31 to your computer and use it in GitHub Desktop.
Save jawsthegame/cb5ec5385405ec5c9be9dee9b2b74c31 to your computer and use it in GitHub Desktop.
Add Bucket Tags to all S3 Buckets
import boto3
s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
tags = s3.BucketTagging(bucket.name)
try:
bucket_tags = [t for t in tags.tag_set if t['Key'] == 'bucket']
if len(bucket_tags) == 0:
tags.put(Tagging={'TagSet':tags.tag_set.append({'Key': 'bucket', 'Value': bucket.name})})
except:
try:
tags.put(Tagging={'TagSet':[{'Key': 'bucket', 'Value': bucket.name}]})
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment