问题
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')
回答1:
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.
回答2:
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.
回答3:
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'
来源:https://stackoverflow.com/questions/15827196/how-can-i-add-a-tag-to-a-key-in-boto-amazon-s3