Set cache control on S3 objects using the AWS CLI

Cache-control can be set on S3 bucket objects.

Setting cache control using aws s3 cp

An object’s metadata may only be changed by overwriting it using the metadata-directive flag.

Single file:

1
2
3
aws s3 cp s3://bucket/example.txt s3://s3-bucket/file.txt \
  --metadata-directive REPLACE \
  --cache-control max-age=86400

All files:

1
2
3
4
aws s3 cp s3://bucket/ s3://bucket/ \
  --metadata-directive REPLACE \
  --cache-control max-age=86400 \
  --recursive

All image files / specific file extensions

1
2
3
4
5
6
7
8
aws s3 cp s3://bucket/ s3://bucket/ \
  --metadata-directive REPLACE \
  --cache-control max-age=86400 \
  --exclude "*" \
  --include "*.jpg" \
  --include "*.gif" \ 
  --include "*.png" \
  --recursive 

Setting cache control for websites served by AWS CloudFront

CloudFront is a CDN service provided by AWS. The max-age directive sets the browser caching age in seconds while another directive named s-maxage sets the CloudFront caching in seconds.

Choosing from the examples above, add s-maxage=86400 to the --cache-control flag for all objects in a bucket.

1
2
3
4
aws s3 cp s3://bucket/ s3://bucket/ \
  --metadata-directive REPLACE \
  --cache-control max-age=86400,s-maxage=86400 \
  --recursive

Setting cache control using aws s3 sync

Warning: Using this option only sets cache headers upon initial sync. The cp command will need to be used to replace headers on existing S3 objects.

1
2
aws s3 sync /path s3://bucket/ \
  --cache-control max-age=86400,s-maxage=86400