Search code examples
pythonamazon-web-servicesamazon-s3boto3s3-bucket

How to add tags to an S3 Bucket without deleting the existing tags using boto3?


I am using this function:

s3 = boto3.resource('s3')
bucket_tagging = s3.BucketTagging(bucket)
Set_Tag = bucket_tagging.put(Tagging={'TagSet':[{'Key':'Owner', 'Value': owner}]})

It is deleting the existing tags and I can see only one tag.


Solution

  • I would use the following

    s3 = boto3.resource('s3')
    bucket_tagging = s3.BucketTagging('bucket_name')
    tags = bucket_tagging.tag_set
    tags.append({'Key':'Owner', 'Value': owner})
    Set_Tag = bucket_tagging.put(Tagging={'TagSet':tags})
    

    This gets the existing tags, adds a new one, and then puts them all back in.