content-type to be used for uploading svg images to AWS S3

21,571

Solution 1

As you mentioned, the correct Content-Type for SVG files is "image/svg+xml".

Even if the AWS console does not provide that value in the Content-Type selection field, you can enter it anyway and S3 will accept it.

AWS specifies the following in their API docs for the Content-Type header:

A standard MIME type describing the format of the contents. For more information, go to http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17.

Type: String

Default: binary/octet-stream

Valid Values: MIME types

Constraints: None

For additional details see http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html

Solution 2

For those using an SDK, here's an example code snippet that I used to solve this problem. Im using Javascript (NodeJs). This is to complement the accepted answer above, which is to explicitly define the ContentType as 'image/svg+xml' from the params before uploading.

const params = {
    Bucket: 'bucket', 
    Key: 'key', 
    Body: stream,
    ACL: 'public-read',
    ContentType: 'image/svg+xml',
 };
s3.upload(params, function(err, data) {
  console.log(err, data);
});
Share:
21,571
UW20989
Author by

UW20989

Updated on September 07, 2021

Comments

  • UW20989
    UW20989 over 2 years

    I was trying to upload *.svg images to S3 without specifying any Content-type. This upload works successfully and AWS sets Content-Type as binary/octet-stream by default. Now, when I try to use S3 url of image in my browser, the browser does not render the image and throws incorrect mime-type warning.

    To set the correct mime-type I checked list of Content-type which AWS offers but it does not have "image/svg+xml".

    So I wanted to know if anyone has tried to upload svg images to S3? What is the content-type set in that case? Or is there any other compatible Content-type that can be used for uploading svg images to S3?

    Thanks in Advance.

  • UW20989
    UW20989 almost 11 years
    Thanks dcro.. It worked for me, I was trying to upload using old version of s3cmd. And thus, even if I tried to upload with Content-type header, it was giving me error.
  • Ryan Parman
    Ryan Parman almost 11 years
    This isn't handled at the S3 layer; it's handled at the application layer. You didn't mention how you were uploading files in your original post.
  • UW20989
    UW20989 over 10 years
    Hi Ryan, I was trying to upload using s3cmd commands. (s3tools.org/s3cmd). I wanted to set mime-type for specific files. Now, I am doing this by using --mime-type param of s3cmd. Note: Sorry, for a very delayed response. I did'nt get any notifications for any comments on this question. :(
  • UW20989
    UW20989 over 10 years
    @RyanParman, can you please help me with this question-- stackoverflow.com/questions/18356880/…
  • dchapman
    dchapman almost 10 years
    @UW20989 Did using the 'image/svg+xml' type work for you? Any time I upload an png/jpeg/gif it works, but if I upload an svg I get a 403 and it says my signature isn't correct. I'm not changing code, so it must be the content-type that it doesn't like.
  • UW20989
    UW20989 almost 10 years
    Yes, chapmand it does work for me.. What version of s3cmd you are using? As far as I remember even I had got this error & upgrading to latest version resolved it.. Please try version-1.5.0-alpha1 of s3cmd. e.g. s3cmd -c s3cmd.cfg --verbose --acl-public --mime-type='image/svg+xml' put --add-header='Cache-Control:max-age=2600000, public' <source> <destination>