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?
AWS Elemental MediaStore is a caching and content distribution system specifically built for video workflows and media delivery from Amazon S3. MediaStore provides end-to-end storage APIs specifically for video, and is recommended for performance- sensitive video workloads.
After you create a bucket, you can enable static website hosting for your bucket. You can create a new bucket or use an existing bucket. Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ .
For hosting a dynamic website on AWS, you need to use EC2 product. S3 is only used for storage and static website hosting. Other than EC2, you can also use Lightsail, which is basically a VPS. For hosting on EC2, you will need to launch an empty and install LAMP or any PHP based stack you have on the server.
Lambda@Edge is a feature of Amazon CloudFront that lets you run code closer to your users, which reduces latency, improve performance, and allows you to programmatically modify the requests or responses that CloudFront handles.
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
max-age=number of seconds that you want objects to stay in a CloudFront edge cache
If you want to add a header field to additional objects, click the name of the next object, and repeat steps 5 through 9.
The simplest way to achieve this is to use AWS CLI (S3). This can also be automated entirely for free using a tool like GitHub actions.
A static site should not have the cache-control
set to a long-lived value on HTML
files because the users will not see the updated version until the browser cache expires or manually busts their cache.
Due to AWS CLI
restrictions, you have to do the following to set the cache for the whole bucket.
Generic Example
Upload the content and --delete
old S3 content, and set cache-control
on all content.
aws s3 sync [YOUR_LOCAL_SOURCE_CODE_PATH] s3://[BUCKET_NAME] --delete --cache-control max-age=31536000
Recursively remove cache-control
headers from all HTML
files and set the file back to type HTML.
aws s3 cp s3://[BUCKET_NAME] s3://[TO_BUCKET_NAME] --recursive --exclude "*" --include "*.html" --metadata-directive REPLACE --cache-control max-age:no-cache --content-type text/html
Notes
HTML
file's metadata in AWS S3, you must also set the content-type
, or it will automatically be set to a generic type causing the browser to download the file instead of rendering it in the browser.Example
// delete old files and upload files from the local directory to the s3 bucket, and set the cache-control header on every file. aws s3 sync ./out s3://www.test.com --delete --cache-control max-age=31536000 // copy all files and remove cache control header from only HTML files and set back to html content type aws s3 cp s3://www.test.com s3://www.test.com --recursive --exclude "*" --include "*.html" --metadata-directive REPLACE --cache-control max-age:no-cache --content-type text/html // bonus - if using CloudFront - small site can invalidate all cache (/*) aws cloudfront create-invalidation --distribution-id=123ABCDEFG --paths "/*"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With