amazon-web-servicesamazon-s3boto3

Updating existing metadata of my S3 object


I am trying to update the existing metadata of my S3 object but in spite of updating it is creating the new one. As per the documentation, it is showing the same way but don't know why it is not able to update it.

 k = s3.head_object(Bucket='test-bucket', Key='test.json')
            s3.copy_object(Bucket='test-bucket', Key='test.json', CopySource='test-bucket' + '/' + 'test.json', Metadata={'Content-Type': 'text/plain'}, MetadataDirective='REPLACE')

Solution

  • I was able to update using the copy_from method

    s3 = boto3.resource('s3')
    object = s3.Object(bucketName, uploadedKey)
    object.copy_from(
        CopySource={'Bucket': bucketName,'Key': uploadedKey},
        MetadataDirective="REPLACE",
        ContentType=value
    )