amazon web services - S3/CloudFront, serve all files with header "Cache-Control: max-age=365000000, immutable" -


i have s3 bucket on top of there cloudfront cdn.

this s3 bucket "immutable", means once upload file there, never delete or update it. safe clients cache files coming s3/cloudfront aggressively.

currently, etags working great, , clients hit 304 responses of time. getting 304 response still involve roundtrip avoided more aggressive caching.

so i'd behavior:

  • cloudfront cdn cache should never invalidated, because s3 cache never changes. cloudfront not need ask again s3 file more once. think i've configured using cloudfront distribution settings.

  • cloudfront should serve files header cache-control: max-age=365000000, immutable (immutable new, partially supported value of 2016)

i don't understand how can achieve desired result. should handle @ cloudfront or s3 level? i've read stuff configuring appropriate header each s3 file. isn't there global setting serve files custom http header use?

should handle @ cloudfront or s3 level?

there no global setting adding custom http headers either in cloudfront or in s3. add http headers objects, must set in s3, individually on each object in bucket. stored in object' metadata - , can found in metadata section each object in aws s3 console.

typically, it's easiest set headers when adding object bucket - exact mechanism doing depends on client app you're using, or sdk.

e.g. aws cli command use --cache-control option:

aws s3 cp test.txt s3://mybucket/test2.txt \     --cache-control max-age=365000000,immutable 

to modify existing objects, s3cmd utility has modify option described in answer: https://stackoverflow.com/a/22522942/6720449

or can use aws s3 command copy objects onto modifying metadata, explained in answer: https://stackoverflow.com/a/29280730/6720449. e.g. replace metadata on objects in bucket:

aws s3 cp s3://mybucket/ s3://mybucket/ --recursive --metadata-directive replace \     --cache-control max-age=365000000,immutable 

cloudfront cdn cache should never invalidated

this quite stringent requirement - can't prevent cloudfront cache ever getting invalidated. is, there no setting prevent cloudfront invalidation being created, if user creating has sufficient permissions. so, in roundabout way, can prevent invalidations ensuring no users, roles, or groups have permissions create invalidation in distribution using cloudfront:createinvalidation iam permission - possibly not practical.

however, there few reasons cloudfront might choose invalidate cache in contravention of backend's cache-control - e.g. if maximum ttl setting set , less max-age.


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -