AWS
S3
Boto3
Content Type
Python

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.

Practice system design

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:

python
1import boto3
2
3s3 = boto3.client("s3")
4
5s3.put_object(
6    Bucket="my-bucket",
7    Key="pages/index.html",
8    Body=b"<html><body>Hello</body></html>",
9    ContentType="text/html",
10)

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:

python
1import boto3
2
3s3 = boto3.client("s3")
4
5s3.upload_file(
6    Filename="report.pdf",
7    Bucket="my-bucket",
8    Key="docs/report.pdf",
9    ExtraArgs={"ContentType": "application/pdf"},
10)

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.

python
1import mimetypes
2import boto3
3
4s3 = boto3.client("s3")
5file_path = "image.png"
6content_type, _ = mimetypes.guess_type(file_path)
7
8s3.upload_file(
9    Filename=file_path,
10    Bucket="my-bucket",
11    Key="images/image.png",
12    ExtraArgs={"ContentType": content_type or "application/octet-stream"},
13)

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".

python
1import boto3
2
3s3 = boto3.client("s3")
4
5s3.copy_object(
6    Bucket="my-bucket",
7    Key="pages/index.html",
8    CopySource={"Bucket": "my-bucket", "Key": "pages/index.html"},
9    ContentType="text/html",
10    MetadataDirective="REPLACE",
11)

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:

python
1s3.copy_object(
2    Bucket="my-bucket",
3    Key="pages/index.html",
4    CopySource={"Bucket": "my-bucket", "Key": "pages/index.html"},
5    ContentType="text/html",
6    CacheControl="max-age=3600",
7    MetadataDirective="REPLACE",
8)

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:

python
response = s3.head_object(Bucket="my-bucket", Key="pages/index.html")
print(response["ContentType"])

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/html for HTML pages'
  • 'text/css for CSS files'
  • 'application/javascript for JavaScript'
  • 'image/png for PNG images'
  • 'image/jpeg for JPEG images'
  • 'application/pdf for 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 ContentType and assuming S3 will always infer it correctly.
  • Updating ContentType later 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 ContentType during upload whenever possible.
  • Use ExtraArgs for upload_file and ContentType directly for put_object.
  • 'mimetypes.guess_type() is a practical way to infer content types from filenames.'
  • To change ContentType later, copy the object onto itself with replacement metadata.
  • Always verify the stored content type with head_object when the behavior looks wrong.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design