Amazon s3 static web hosting caching

Mattan Bitner picture Mattan Bitner · Mar 1, 2017 · Viewed 24.1k times · Source

I'm using Amazon S3 webhosting for my static html,js,css (etc..) files. After replacing my index.html file, I still get the old version when consuming via the browser. I would like to set a default ttl to the bucket (and not to specific objects in it). I found this link: http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/distribution-web-values-specify.html#DownloadDistValuesDefaultTTL

but can't find the "Object Caching" setting in the dashboard. can someone point out where it is?

Answer

D. Woods picture D. Woods · May 30, 2017

Here's an Amazon S3 link that answers your question. According to Amazon, you can't set the cache control header for the entire bucket unless you use a third party tool (that page links to a few). One tool I ran across describes how to set the cache directives for objects submitted using a PUT request (or in bulk using their tool). See BucketExplorer for more info.

Here is a cut-paste of Amazon's instructions (since S.O. doesn't like to rely on external links that might change or disappear):

To add a Cache-Control or Expires header field to Amazon S3 objects using the Amazon S3 console

  1. Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3.
  2. In the Amazon S3 console, in the buckets pane, click the name of the bucket that contains the files.
  3. In the list of objects, select the first object to which you want to add a header field.
  4. Click Actions and click Properties.
  5. In the right pane, expand Metadata.
  6. Click Add More Metadata.
  7. In the Key list, click Cache-Control or Expires, as applicable.
  8. In the Value field, enter the applicable value:
  9. For a Cache-Control field, enter: max-age=number of seconds that you want objects to stay in a CloudFront edge cache
  10. For an Expires field, enter a date and time in HTML format.
  11. Click Save.

If you want to add a header field to additional objects, click the name of the next object, and repeat steps 5 through 9.