import boto3 from botocore.exceptions import ClientError s3_client = boto3.client('s3') s3_res = boto3.resource('s3') response = s3_client.list_buckets() buckets = response['Buckets'] for bucket in buckets: print('---') print(bucket) bucket_obj = s3_res.Bucket(bucket['Name']) bucket_tagging = bucket_obj.Tagging() try: tags = bucket_tagging.tag_set except ClientError as e: tags = [] # Add Name tag name_idx = [i for i in range(len(tags)) if tags[i]['Key'] == 'Name'] if len(name_idx) > 0: name_idx = name_idx[0] tags[name_idx]['Value'] = bucket['Name'] else: tags.append({ 'Key': 'Name', 'Value': bucket['Name'] }) res = bucket_tagging.put(Tagging={'TagSet': tags}) print(res)