24

I have an amazon S3 bucket with approximately 300K items in it that are used by a large website. I would like to set the expiration of all the objects that are served out of CloudFront from the S3 bucket so that they can be cached in the browser by the user's machine. Is there an easy way to set the cache control on all the s3 objects currently in the bucket AND most importantly set a default for the bucket so that any new items added also gain the expires and cache-control headers OR can this be done using CloudFront?

So far i have read a number of AWS documents but have found nothing:

dpegasusm
  • 620
  • 2
  • 7
  • 20

1 Answers1

16

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/

For CloudFront you can specify Minimum TTL, Maximum TTL, and Default TTL for a cache behavior.they are basically the time for which an object can be cached on CloudFront and has nothing to do with adding an expiry header for the object i.e it doesn't mody any header

So now if you haven't added any header, then cloudfront will caches it for DEFAULT TTL. FOR MORE INFO READFOLLOWING TABLE http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Expiration.html#ExpirationDownloadDist

ashishyadaveee11
  • 1,021
  • 15
  • 17
  • 1
    The CloudFront settings "Minimum TTL", "Maximum TTL" and "Default TTL" specify the behaviour of the CloudFront cache, not what headings are set in the HTTP response, correct? – Flimm Nov 23 '16 at 14:23
  • 2
    This does not seem to address the last part of the question. How do you set the bucket so that new things added in that folder or even the whole bucket get that tag added to them – boatcoder Dec 13 '16 at 18:26
  • 1
    CloudFront TTLs are saying for how long should objects be in edge nodes cache and do nothing with client caching – mac Jan 29 '17 at 12:20
  • @boatcoder I wasn't able to find the solution to that part. Do share with me if you are able to find one. – ashishyadaveee11 Jan 30 '17 at 07:03
  • 1
    I'm glad I found this tool. It's such a great thing. – Edward Jun 18 '17 at 04:35
  • 4
    With AWS CLI the command looks like this: `aws s3 cp s3://YOUR_BUCKET/ s3://YOUR_BUCKET/ --metadata-directive REPLACE --recursive --cache-control max-age=31536000` – Thomas Dec 24 '18 at 14:01
  • If you are using aws sync command-line option, you can specify --cache-control max-age=31536000 at end of the command to have that by default whenever a new object is added to s3. – rahimv Apr 28 '21 at 08:48