Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change metadata on an object in Amazon S3

If you have already uploaded an object to an Amazon S3 bucket, how do you change the metadata using the API? It is possible to do this in the AWS Management Console, but it is not clear how it could be done programmatically. Specifically, I'm using the boto API in Python and from reading the source it is clear that using key.set_metadata only works before the object is created as it just effects a local dictionary.

like image 597
natevw Avatar asked Jan 21 '11 00:01

natevw


People also ask

How do I update metadata for an existing Amazon S3 file?

You can set object metadata at the time you upload it. After you upload the object, you cannot modify object metadata. The only way to modify object metadata is to make a copy of the object and set the metadata.

What is held as metadata in an S3 object?

For each object stored in a bucket, Amazon S3 maintains a set of system metadata. Amazon S3 processes this system metadata as needed. For example, Amazon S3 maintains object creation date and size metadata and uses this information as part of object management.

Can you edit data in S3?

s3 doesn't support editing files... it stores objects that must be rewritten any time you want to update them.

Are S3 tags metadata?

If you want to add your own custom metadata to an object in S3, you can add metadata instead of tags. Tags are not the same thing as object metadata in S3. Metadata applies only to that object in S3 and cannot be searched on, as you can search with tags.


1 Answers

It appears you need to overwrite the object with itself, using a "PUT Object (Copy)" with an x-amz-metadata-directive: REPLACE header in addition to the metadata. In boto, this can be done like this:

k = k.copy(k.bucket.name, k.name, {'myKey':'myValue'}, preserve_acl=True) 

Note that any metadata you do not include in the old dictionary will be dropped. So to preserve old attributes you'll need to do something like:

k.metadata.update({'myKey':'myValue'}) k2 = k.copy(k.bucket.name, k.name, k.metadata, preserve_acl=True) k2.metadata = k.metadata    # boto gives back an object without *any* metadata k = k2; 

I almost missed this solution, which is hinted at in the intro to an incorrectly-titled question that's actually about a different problem than this question: Change Content-Disposition of existing S3 object

like image 104
natevw Avatar answered Oct 06 '22 01:10

natevw