def modify_bucket_tags(session_factory, buckets, add_tags=(), remove_tags=()):
for bucket in buckets:
client = bucket_client(local_session(session_factory), bucket)
# all the tag marshalling back and forth is a bit gross :-(
new_tags = {t['Key']: t['Value'] for t in add_tags}
for t in bucket.get('Tags', ()):
if (t['Key'] not in new_tags and
not t['Key'].startswith('aws') and
t['Key'] not in remove_tags):
new_tags[t['Key']] = t['Value']
tag_set = [{'Key': k, 'Value': v} for k, v in new_tags.items()]
try:
client.put_bucket_tagging(
Bucket=bucket['Name'], Tagging={'TagSet': tag_set})
except ClientError as e:
log.exception(
'Exception tagging bucket %s: %s', bucket['Name'], e)
continue
评论列表
文章目录