Setting Cache-Control header for all Amazon S3 objects

This week while optimizing a website for performance one of the issues that came up was that the cache headers were not set for assets served using CloudFront.

CloudFront does not allow Cache-Control headers to be set but allows the use of the Origin Cache Headers pulled from Amazon S3. Most assets uploaded to S3 over the last three years did not have the headers set 😱, and on Amazon S3 console you can only add headers to one object at a time, so had to use AWS CLI to add headers to all objects in one go.

With the default CacheControl settings defined all new objects uploaded to Amazon S3 will have the Cache-Control header set to max-age=2592000. These settings can be overridden for an individual object if needed.