Brad Rhoads
Brad Rhoads

Reputation: 1846

How do you specify tagging in put_object?

I need to specify taggings using put_object as follows:

s3client = boto3.client('s3')

response = s3client.put_object(Bucket = dest_bucket_name, Body = body, Key = dest_key, Tagging = tagging, ACL = acl )

My question is what data structure do I use to for the tagging variable?

I've seen various examples, but I can't get them to work. For example:

tagging = {'TagSet' : [
            {
                'Key': 'voiceid',
                'Value': polly_voice
            },
            {
                'Key': 'author',
                'Value': book_author
            },
            . . .
          ]
     }

gives me

 "errorMessage": "Parameter validation failed:\nInvalid type for parameter Tagging, value: {'TagSet': [{'Key': 'voiceid', 'Value': 'Matthew'}, {'Key': 'author', 'Value': 'some guy'},...]}, type: <class 'dict'>, valid types: <class 'str'>"

I see that it's expecting a string, but then how do I specify the key:value pairs? And all the examples I've found use some specific data structure like in my example.

I doubt that this makes any difference, but I'm doing this in a lambda function.

Upvotes: 2

Views: 2838

Answers (2)

Jason H
Jason H

Reputation: 131

URL encode your dictionary and pass the resulting string with this answer

For Python3:

>>> import urllib.parse
>>> tags = {'key1':'value1','key2':'value2'}
>>> tagging = urllib.parse.urlencode(tags)
>>> print(tagging)
key1=value1&key2=value2

Upvotes: 1

krishna_mee2004
krishna_mee2004

Reputation: 7366

To get this working, try the following code:

tags = 'voiceid={}&author={}'.format(polly_voice, book_author)
s3client = boto3.client('s3')
response = s3client.put_object(
    Bucket=dest_bucket_name,
    Body=body,
    Key=dest_key,
    Tagging=tags
)

You can refer this stackoverflow answer too.

Upvotes: 4

Related Questions