Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon S3: Cache-Control and Expiry Date difference and setting trough REST API

I want to enhance my sites loading speed, so I use http://gtmetrix.com/, to check what I could improve. One of the lowest rating I get for "Leverage browser caching". I found, that my files (mainly images), have problem "expiration not specified".

Okay, problem is clear, I thought. I start to googling and I found that amazon S3 prefer Cache-Control meta data over Expiry date (I lost this link, now I think maybe I misunderstood something). Anyway, I start looking for how to add cache-control meta to S3 object. I found this page: http://www.bucketexplorer.com/documentation/amazon-s3--how-to-set-cache-control-header-for-s3-object.html

I learned, that I must add string to my PUT query.

x-amz-meta-Cache-Control : max-age= <value in seconds> //(there is no need space between equal sign and digits(I made a mistake here)).

I use construction: Cache-control:max-age=1296000 and it work okay.

After that I read https://developers.google.com/speed/docs/best-practices/caching This article told me: 1) "Set Expires to a minimum of one month, and preferably up to one year, in the future."

2) "We prefer Expires over Cache-Control: max-age because it is is more widely supported."(in Recommendations topic).

So, I start to look way to set Expiry date to S3 object. I found this: http://www.bucketexplorer.com/documentation/amazon-s3--set-object-expiration-on-amazon-s3-objects-put-get-delete-bucket-lifecycle.html

And what I found: "Using Amazon S3 Object Lifecycle Management , you can define the Object Expiration on Amazon S3 Objects . Once the Lifecycle defined for the S3 Object expires, Amazon S3 will delete such Objects. So, when you want to keep your data on S3 for a limited time only and you want it to be deleted automatically by Amazon S3, you can set Object Expiration."

I don't want to delete my files from S3. I just want add cache meta for maximum cache time or/and file expiry time.

I completely confused with this. Can somebody explain what I must use: object expiration or cache-control?

like image 523
Dima Deplov Avatar asked Sep 05 '13 17:09

Dima Deplov


2 Answers

S3 lets you specify the max-age and Expires header for cache control , CloudFront lets you specify the Minimum TTL, Maximum TTL, and Default TTL for a cache behavior. and these header just tell when will the validity of an object expires in the cache(be it cloudfront or browser cache) to read how they are related read the following link http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Expiration.html#ExpirationDownloadDist

For letting you Leverage Browser caching just specify the Cache control header for all the object on s3 do Steps for adding cache control for existing objects in your bucket

  1. git clone https://github.com/s3tools/s3cmd
  2. Run s3cmd --configure (You will be asked for the two keys - copy and paste them from your confirmation email or from your Amazon account page. Be careful when copying them! They are case sensitive and must be entered accurately or you'll keep getting errors about invalid signatures or similar. Remember to add s3:ListAllMyBuckets permissions to the keys or you will get an AccessDenied error while testing access.)
  3. ./s3cmd --recursive modify --add-header="Cache-Control:public ,max-age= 31536000" s3://your_bucket_name/
like image 148
ashishyadaveee11 Avatar answered Sep 22 '22 16:09

ashishyadaveee11


Your files won't be deleted, just not cached after the expiration date.

The Amazon docs say:

After the expiration date and time in the Expires header passes, CloudFront gets the object again from the origin server every time an edge location receives a request for the object.

We recommend that you use the Cache-Control max-age directive instead of the Expires header field to control object caching. If you specify values both for Cache-Control max-age and for Expires, CloudFront uses only the value of max-age.

like image 31
Justin Avatar answered Sep 19 '22 16:09

Justin