AWS static site file upload via boto 3 set the right content-types


What are the right content types for the different types of files of a static site hosted at AWS and how to set these in a smart way via boto3?

I use the upload_file method:

This works well for the html files. I initially left out the ExtraArgs which results in a file download (probably because the content type is binary?). I found this page that states several content types but I am not sure how to apply it.

E.g. probably the CSS files should be uploaded with ‘ContentType’: ‘text/css’.
But what about the js files, index.xml, etc? And how to do this in a smart way? FYI this is my current script to upload from Windows to AWS, this requires string.replace(“\”,”/”) which probably is not the smartest either?

I uploaded my complete Hugo site from the same source using the AWS CLI to the same S3 bucket and this works perfect without specifying content types, is this also possible via boto 3?

Many thanks in advance for your help!


There is a python built-in library to guess mimetypes.

So you could just lookup each filename first. It works like this:


In your code. I also slightly improved the portability of your code with respect to the windows path. Now it will do the same thing, but be portable to a Unix platform by looking up the platform specific separator (os.path.sep) that will be being used in any paths.

Leave a Reply