How to update metadata of an existing object in AWS S3 using python boto3?

后端 未结 3 1019
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-01 14:19

boto3 documentation does not clearly specify how to update the user metadata of an already existing S3 Object.

3条回答
  •  无人及你
    2020-12-01 15:21

    You can do this using copy_from() on the resource (like this answer) mentions, but you can also use the client's copy_object() and specify the same source and destination. The methods are equivalent and invoke the same code underneath.

    import boto3
    s3 = boto3.client("s3")
    src_key = "my-key"
    src_bucket = "my-bucket"
    s3.copy_object(Key=src_key, Bucket=src_bucket,
                   CopySource={"Bucket": src_bucket, "Key": src_key},
                   Metadata={"my_new_key": "my_new_val"},
                   MetadataDirective="REPLACE")
    

    The 'REPLACE' value specifies that the metadata passed in the request should overwrite the source metadata entirely. If you mean to only add new key-values, or delete only some keys, you'd have to first read the original data, edit it and call the update.

    To replacing only a subset of the metadata correctly:

    1. Retrieve the original metadata with head_object(Key=src_key, Bucket=src_bucket). Also take note of the Etag in the response
    2. Make desired changes to the metadata locally.
    3. Call copy_object as above to upload the new metadata, but pass CopySourceIfMatch=original_etag in the request to ensure the remote object has the metadata you expect before overwriting it. original_etag is the one you got in step 1. In case the metadata (or the data itself) has changed since head_object was called (e.g. by another program running simultaneously), copy_object will fail with an HTTP 412 error.

    Reference: boto3 issue 389

提交回复
热议问题