AWS Content Type Settings in S3 Using Boto3
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In S3, ContentType is object metadata that affects how browsers and clients interpret the file. If you upload an HTML file as binary/octet-stream instead of text/html, clients may download it instead of rendering it. With Boto3, the right place to set content type is usually during upload.
Set ContentType at Upload Time
For direct uploads with put_object, pass ContentType explicitly:
This is the simplest and cleanest approach because the object is written with the correct metadata from the start.
Upload Files with ExtraArgs
If you are using upload_file, the metadata goes into ExtraArgs:
This is a common pattern for scripts that upload local files rather than in-memory data.
Guess the Content Type from the Filename
Hardcoding the content type every time gets repetitive. Python's mimetypes module can infer a reasonable default from the file extension.
This is usually good enough for standard web file types such as .html, .css, .js, .png, and .pdf.
Changing ContentType After Upload
S3 object metadata is not patched in place the way a database row might be updated. To change ContentType, you usually copy the object onto itself with MetadataDirective="REPLACE".
This is the standard fix when an object already exists with the wrong content type.
Preserve Other Metadata When Replacing
A common mistake is to replace ContentType and accidentally wipe out other metadata or headers. When using MetadataDirective="REPLACE", you should supply the metadata and headers you want to keep.
For example, if the object also needs cache control:
If you omit values while replacing, they may not survive the operation the way you expect.
Verify What S3 Stored
You can inspect the stored metadata with head_object:
This is the easiest way to confirm whether the upload or metadata update actually did what you intended.
Common Useful Content Types
Some common values are:
- '
text/htmlfor HTML pages' - '
text/cssfor CSS files' - '
application/javascriptfor JavaScript' - '
image/pngfor PNG images' - '
image/jpegfor JPEG images' - '
application/pdffor PDF files'
If you are serving content through CloudFront or directly from S3, getting these values right improves browser behavior immediately.
Why It Matters in Real Deployments
Incorrect content types are especially painful for:
- static website hosting
- front-end asset deployment
- downloadable reports
- API clients expecting specific response types
A file may upload successfully but still behave "wrong" because the metadata misdescribes it.
Common Pitfalls
- Uploading objects without
ContentTypeand assuming S3 will always infer it correctly. - Updating
ContentTypelater without realizing S3 metadata replacement is copy-based. - Forgetting to preserve other metadata and headers when using
MetadataDirective="REPLACE". - Hardcoding one content type for every file regardless of extension.
- Debugging browser behavior without checking the object's stored metadata with
head_object.
Summary
- Set
ContentTypeduring upload whenever possible. - Use
ExtraArgsforupload_fileandContentTypedirectly forput_object. - '
mimetypes.guess_type()is a practical way to infer content types from filenames.' - To change
ContentTypelater, copy the object onto itself with replacement metadata. - Always verify the stored content type with
head_objectwhen the behavior looks wrong.
Related reading
- AWS create role - Has prohibited field
- AWS, Credential must have exactly 5 slash-delimited elements,
- AWS Custom SSL certificate option is disabled in CloudFront, but I created a SSL certificate using AWS Certificate Manager
- AWS Data Pipeline vs Step Functions
- AWS Elastic Beanstalk error - ImportError No module named flask.ext.sqlalchemy
- AWS Elastic Beanstalk logging with python django
- AWS Dedicated Host VS Dedicated Instance, why the first is more expensive than the later?
- AWS Difference between a snapshot and AMI
System design interview problems on this topic
- Design Dropbox: solution and walkthrough
- Design a Global Content Distribution Network: solution and walkthrough
- Design a Serverless Architecture Framework: solution and walkthrough
- Design Youtube or Netflix: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.