Force CloudFront distribution/file update

后端 未结 13 1195
失恋的感觉
失恋的感觉 2020-12-07 07:01

I\'m using Amazon\'s CloudFront to serve static files of my web apps.

Is there no way to tell a cloudfront distribution that it needs to refresh it\'s file or point

相关标签:
13条回答
  • 2020-12-07 07:34

    one very easy way to do it is FOLDER versioning.

    So if your static files are hundreds for example, simply put all of them into a folder called by year+versioning.

    for example i use a folder called 2014_v1 where inside i have all my static files...

    So inside my HTML i always put the reference to the folder. ( of course i have a PHP include where i have set the name of the folder. ) So by changing in 1 file it actually change in all my PHP files..

    If i want a complete refresh, i simply rename the folder to 2014_v2 into my source and change inside the php include to 2014_v2

    all HTML automatically change and ask the new path, cloudfront MISS cache and request it to the source.

    Example: SOURCE.mydomain.com is my source, cloudfront.mydomain.com is CNAME to cloudfront distribution.

    So the PHP called this file cloudfront.mydomain.com/2014_v1/javascript.js and when i want a full refresh, simply i rename folder into the source to "2014_v2" and i change the PHP include by setting the folder to "2014_v2".

    Like this there is no delay for invalidation and NO COST !

    This is my first post in stackoverflow, hope i did it well !

    0 讨论(0)
  • 2020-12-07 07:34

    If you are using AWS, you probably also use its official CLI tool (sooner or later). AWS CLI version 1.9.12 or above supports invalidating a list of file names.

    Full disclosure: I made this. Have fun!

    0 讨论(0)
  • 2020-12-07 07:37

    In ruby, using the fog gem

    AWS_ACCESS_KEY = ENV['AWS_ACCESS_KEY_ID']
    AWS_SECRET_KEY = ENV['AWS_SECRET_ACCESS_KEY']
    AWS_DISTRIBUTION_ID = ENV['AWS_DISTRIBUTION_ID']
    
    conn = Fog::CDN.new(
        :provider => 'AWS',
        :aws_access_key_id => AWS_ACCESS_KEY,
        :aws_secret_access_key => AWS_SECRET_KEY
    )
    
    images = ['/path/to/image1.jpg', '/path/to/another/image2.jpg']
    
    conn.post_invalidation AWS_DISTRIBUTION_ID, images
    

    even on invalidation, it still takes 5-10 minutes for the invalidation to process and refresh on all amazon edge servers

    0 讨论(0)
  • 2020-12-07 07:43

    As of March 19, Amazon now allows Cloudfront's cache TTL to be 0 seconds, thus you (theoretically) should never see stale objects. So if you have your assets in S3, you could simply go to AWS Web Panel => S3 => Edit Properties => Metadata, then set your "Cache-Control" value to "max-age=0".

    This is straight from the API documentation:

    To control whether CloudFront caches an object and for how long, we recommend that you use the Cache-Control header with the max-age= directive. CloudFront caches the object for the specified number of seconds. (The minimum value is 0 seconds.)

    0 讨论(0)
  • 2020-12-07 07:43

    Go to CloudFront.

    Click on your ID/Distributions.

    Click on Invalidations.

    Click create Invalidation.

    In the giant example box type * and click invalidate

    Done

    0 讨论(0)
  • 2020-12-07 07:44

    Just posting to inform anyone visiting this page (first result on 'Cloudfront File Refresh') that there is an easy-to-use+access online invalidator available at swook.net

    This new invalidator is:

    • Fully online (no installation)
    • Available 24x7 (hosted by Google) and does not require any memberships.
    • There is history support, and path checking to let you invalidate your files with ease. (Often with just a few clicks after invalidating for the first time!)
    • It's also very secure, as you'll find out when reading its release post.

    Full disclosure: I made this. Have fun!

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