Skip to content

S3Path does not set object mime-type when uploading a file on S3 #222

@davidatsamp

Description

@davidatsamp

In order to serve correctly S3 object from cloudfront origin, we need to set correctly S3 object mime-type.
Unfortunately the S3 client does not provide any mime-type when calling upload_file.

It could be possible to use at least the mimetypes module for the most common mime-types based on file extension in s3client.py:

def _upload_file(self, local_path: Union[str, os.PathLike], cloud_path: S3Path) -> S3Path:
    obj = self.s3.Object(cloud_path.bucket, cloud_path.key)

    extra_args = {}
    content_type, content_encoding = mimetypes.guess_type(str(local_path))
    if content_type is not None:
        extra_args["ContentType"] = content_type
    if content_encoding is not None:
        extra_args["ContentEncoding"] = content_encoding

    obj.upload_file(str(local_path), Config=self.boto3_transfer_config, ExtraArgs=extra_args)
    return cloud_path

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions