statguy
statguy

Reputation: 1707

How can I add a tag to a key in boto (Amazon S3)?

I am trying to tag a key that I've uploaded to S3. In the same below I just create a file from a string. Once I have they key, I'm not sure how to tag the file. I've tried Tag as well as TagSet.

from boto.s3.bucket import Bucket
from boto.s3.key import Key
from boto.s3.tagging import Tag, TagSet

k = Key(bucket)
k.key = 'foobar/somefilename'
k.set_contents_from_string('some data in file')

Tag(k, 'the_tag')

Upvotes: 19

Views: 24593

Answers (5)

Piotr Kula
Piotr Kula

Reputation: 9811

I am new to Python and Boto3 and it took me several attempts to try and decipher what is expected to be added into ExtraArgs for example like in the Copy functionality

import json
import boto3
from urllib import parse


s3 = boto3.resource('s3')

# Get Tags for an object

responseTagging = s3.meta.client.get_object_tagging(
     Bucket='bucket',
     Key='file_key'
)
print(responseTagging)

# Copy origin to destination with new tags
# The TagSet must be a <class 'str> .. so you must stringify the object+dictionary
# Tagging in Extra params must be an object with the stringified TagSet

taggingEncoded = parse.urlencode({'TagSet': 
        [
            {'Key': 'Secure', 'Value': 'False'}, 
            {'Key': 'MimeType', 'Value': 'image/png'}
        ]})

extraArgs = {"Tagging": taggingEncoded}

copy_source_config ={
  'Bucket' : 'origin_bucket',
  'Key' : 'originKey'
}

s3.meta.client.copy(copy_source_config,'desination_bucket', 'destination_key', ExtraArgs=extraArgs)

The boto3 documentation is difficult for newbies to understand but what makes it worse over the years answers on SO seem to be all wrong as the API matured.

Upvotes: 0

valex
valex

Reputation: 5769

import boto3

s3_client = boto3.client(
    's3',
    region_name='region-name',
    aws_access_key_id='aws-access-key-id',
    aws_secret_access_key='aws-secret-access-key',
)

get_tags_response = s3_client.get_object_tagging(
    Bucket='your-bucket-name',
    Key='folder-if-any/file-name.extension',
)

put_tags_response = s3_client.put_object_tagging(
    Bucket='your-bucket-name',
    Key='folder-if-any/file-name.extension',    
    Tagging={
        'TagSet': [
            {
                'Key': 'tag-key',
                'Value': 'tag-value'
            },
        ]
    }
)

Upvotes: 26

Hank
Hank

Reputation: 3661

S3 has since added object level tags. You can get and set them with boto3.

These are considerably more versatile than metadata:

  • They can be added and modified without copying the object.
  • They can be used as filters in lifecycle management rules.
  • They can be used to control access to objects.

Upvotes: 25

Jimmy Schementi
Jimmy Schementi

Reputation: 1239

While S3 "tags" are only at the bucket-level, each key in a bucket can have arbitrary "metadata" associated with it, which are key-value pairs themselves. See the boto documentation:

k.set_metadata('key', 'value')
value = k.get_metadata('key') # prints 'value'

Upvotes: -1

j0nes
j0nes

Reputation: 8099

As far as I can see in the docs, a setTags-method is only available on a bucket level and not on individual keys. Therefore you cannot set different tags to your uploaded file, but you would have to do this on the containing bucket.

Upvotes: -3

Related Questions