Madhurima Mishra
Madhurima Mishra

Reputation: 1083

"get_bucket_tagging" for s3 buckets give error, when there are no tags present

I am trying to get S3 bucket tags using "get_bucket_tagging".

Code:

response = client.get_bucket_tagging(Bucket='bucket_name') 
print(response['TagSet'])

I am getting output till there are any tags present. But getting following error when there are 0 tags.

An error occurred (NoSuchTagSet) when calling the GetBucketTagging operation: The TagSet does not exist

Is there any other method to check that?

Upvotes: 2

Views: 5199

Answers (1)

Venkatesh Wadawadagi
Venkatesh Wadawadagi

Reputation: 2943

From this document:

NoSuchTagSetError - There is no tag set associated with the bucket.

So when there is no tag set associated with the bucket, error/exception is expected. You need to handle this exception.

import boto3
client = boto3.client('s3')

try:
    response = client.get_bucket_tagging(Bucket='bucket_name')
    print(response['TagSet'])

except Exception, e:
    # Handle exception
    # Do something
    print e

Upvotes: 5

Related Questions