AWS cloudfront not updating on update of files in S3

后端 未结 9 513
无人共我
无人共我 2021-02-01 14:12

I created a distribution in cloudfront using my files on S3. It worked fine and all my files were available. But today I updated my files on S3 and tried to access them via Clou

9条回答
  •  盖世英雄少女心
    2021-02-01 14:36

    The main advantage of using CloudFront is to get your files from a source (S3 in your case) and store it on edge servers to respond to GET requests faster. CloudFront will not go back to S3 source for each http request.

    To have CloudFront serve latest fiels/objects, you have multiple options:

    Use CloudFront to Invalidate modified Objects

    You can use CloudFront to invalidate one or more files or directories manually or using a trigger. This option have been described in other responses here. More information at Invalidate Multiple Objects in CloudFront. This approach comes handy if you are updating your files infrequently and do not want to impact the performance benefits of cached objects.

    Setting object expiration dates on S3 objects

    This is now the recommended solution. It is straight forward:

    • Log in to AWS Management Console
    • Go into S3 bucket
    • Select all files
    • Choose "Actions" drop down from the menu
    • Select "Change metadata"
    • In the "Key" field, select "Cache-Control" from the drop down menu.
    • In the "Value" field, enter "max-age=300" (number of seconds)
    • Press "Save" button The default cache value for CloudFront objects is 24 hours. By changing it to a lower value, CloudFront checks with the S3 source to see if a newer version of the object is available in S3.

    I use a combination of these two methods to make sure updates are propagated to an edge locations quickly and avoid serving outdated files managed by CloudFront.

    AWS however recommends changing the object names by using a version identifier in each file name. If you are using a build command and compiling your files, that option is usually available (as in react npm build command).

提交回复
热议问题