I want to update the Content-Type of an existing object in a S3 bucket, using boto3, but how do I do that, without having to re-upload the file?
file_object = s3.Object(bucket_name, key)
file_object.content_type = 'application/pdf'
# AttributeError: can't set attribute
Thee doesn't seem to exist any method for this in boto3. However, it turnes out you can access the AWS low level API through boto3, and do like 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)
MetadataDirective="REPLACE" turns out to be required for S3 to overwrite the file, otherwise you will get an error message saying
This copy request is illegal because it is trying to copy an object to itself without changing the object's metadata, storage class, website redirect location or encryption attributes.