How to change metadata on an object in Amazon S3

前端 未结 7 1117
迷失自我
迷失自我 2020-12-24 06:01

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

7条回答
  •  感情败类
    2020-12-24 06:23

    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

提交回复
热议问题