python - How do I set the Content-Type of an existing S3 key with boto3? -
i want update content-type of existing object in s3 bucket, using boto3, how do that, without having re-upload file?
    file_object = s3.object(bucket_name, key)     print file_object.content_type     # binary/octet-stream     file_object.content_type = 'application/pdf'     # attributeerror: can't set attribute is there method have missed in boto3?
related questions:
there doesn't seem exist method in boto3, can copy file overwrite itself.
to using aws low level api through boto3, this:
s3 = resource('s3') api_client = s3.meta.client response = api_client.copy_object(bucket=bucket_name,                                   key=key,                                   contenttype="application/pdf",                                   metadatadirective="replace",                                   copysource=bucket_name + "/" + key) the metadatadirective="replace" turns out required s3 overwrite file, otherwise error message saying this copy request illegal because trying copy object without changing object's metadata, storage class, website redirect location or encryption attributes. .
or can use copy_from, pointed out jordon phillips in comments:
s3 = boto3.resource("s3") object = s3.object(bucket_name, key) object.copy_from(copysource={'bucket': bucket_name,                              'key': key},                  metadatadirective="replace",                  contenttype="application/pdf") 
Comments
Post a Comment