'To upload metadata for s3 image upload using python

I am uploading image using python boto3 to s3. Not able to set metadata field for s3 object to "image/png". code:

s3.put_object(Bucket=settings.AWS_STORAGE_PRIVATE_BUCKET_NAME,
                      Key=s3_storage_path,
                      Body=file_content,
                      Metadata={"Content-Type":"image/png"},
                      )

Metadata gets set by system to "binary/octet-stream"



Solution 1:[1]

When using put_object, the content type is set via the ContentType parameter, not via Metadata.

ContentType (string) -- A standard MIME type describing the format of the contents. For more information, see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17 .

Try:

s3.put_object(
    Bucket=settings.AWS_STORAGE_PRIVATE_BUCKET_NAME,
    Key=s3_storage_path,
    Body=file_content,
    ContentType='image/png'
)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ermiya Eskandary