Using Amazon s3 and Cloudfront what is the best way to set expires headers and cache expiration
We have a huge bucket of photos on s3. We add and remove from this daily but it currently has millions of photos that we don't want to rewrite or update if possible.
We have not set any expires headers on these existing photos.
I just Spun up cloudfront and pointed it to our bucket.
I have two goals now:
- Use versioning to let older photos expire from Cloudfront.
- Set Far Future Expire headers somewhere (S3, CloudFront) to inform browsers they should keep these files forever.
This raises a few questions. Can I accomplish both goals above with one setting?
Can I make this setting(s) at a bucket level, an individual photo level, or at the cloudfront level.
It would be preferable to be able to make any cache and far future expires header settings at the cloudfront or S3 bucket level and not worry about these settings on an individual photo level, especially in light of the fact that we have millions of photos without these settings already in s3.
thnks for any help, much appreciated!
Solution 1:
You add a cache-control unfortunately, at the individual item level within S3. I don't know if you can do this at the bucket level.
You can however do this for multiple items at once using a 3rd party tool, like CloudBerry Explorer
I have done this for some items, here's a screenshot from the AWS web console:
the value is in seconds.
S3 also supports versioning but I've not personally had a play with it.
In terms of Cloudfront, you'll only need to mess around with it to invalidate old versions of documents. i.e. you've changed the index.html file and you need Cloudfront to immediately delete it's (now) out of date copy. Invalidate the file, and users browsing the site will get the updated copy from S3.