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

后端 未结 3 740
Happy的楠姐
Happy的楠姐 2020-12-31 20:45

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 fo

相关标签:
3条回答
  • 2020-12-31 21:27

    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.

    0 讨论(0)
  • 2020-12-31 21:42

    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/
    0 讨论(0)
  • 2020-12-31 21:45

    "Amazon S3 Object Lifecycle Management" flushs some objects from your bucket based on a rule you can define. It's only about storage.

    What you want to do is set the Expires header of the HTTP request as you set the Cache-Control header. It works the same: you juste have to add this header to your PUT query. Expires doesn't work as Cache-Control: Expires gives a date. For instance: Sat, 31 Jan 2013 23:59:59 GMT

    You may read this: https://web.archive.org/web/20130531222309/http://www.newvem.com/how-to-add-caching-headers-to-your-objects-using-amazon-s3/

    0 讨论(0)
提交回复
热议问题