How does versioning work on Amazon Cloudfront?

孤人 提交于 2021-01-28 05:33:03

问题


I've just set up a static website on Amazon S3. I'm also using the Cloudfront CDN service.

According to Amazon, there are 2 methods available for clearing the Cloudfront cache: invalidation and versioning. My question is regarding the latter.

Consider the following example:

I link to an image file (image.jpg) from my index.html file. I then decide to replace the image. I upload a second image with the filename: image_2.jpg and change the link in my index.html file.

Will the changes automatically take effect or is some further action required?

What triggers the necessary changes if the edited and newly uploaded files are located in the bucket and not the cache?


回答1:


Versioning in CloudFront is nothing more than adding (or prefixing) a version in the name of the object or 'folder' where objects are in stored.

  • all objects in a folder v1 and use a URL like https://xxx.cloudfront.net/v1/image.png
  • all objects contain a version in their name like image_v1.png and use a URL like https://xxx.cloudfront.net/image_v1.png

The second option is often a bit more work but then you don't need to upload new files which do not require to be updated (=cheaper in the context of storage). The first solution is often more clear and requires less work.

Using CloudFront Versioning requires more S3 storage but is often cheaper than creating many invalidations.

The other way to invalidate the cache is to create invalidations (can be expensive). If you don't really need invalidations but just need more quick cache refreshes (default 24h) then you can update the origin TTL settings (origin level) or set cache duration for an individual object (object level).




回答2:


Your cloudfront configuration has a cache TTL, which tells you when the file will be updated, regardless of when the source changes.

If you need it updated right away, use the invalidation function on your index.html file




回答3:


I'll chime in on this in case anyone else comes here looking for what I did. You can set up Cloudfront with S3 versioning enabled and reference specific S3 versions if you know which version you need. I put it behind a presigned Cloudfront URL and ended up with this in the Java SDK:

    S3Properties s3Properties... // Custom properties pulled from a config file
    String cloudfrontUrl = "https://" + s3Properties.getCloudfrontDomain() + "/" + 
            documentS3Key + "?versionId=" + documentS3VersionId;

    URL cloudfrontSignedUrl = new URL(CloudFrontUrlSigner.getSignedURLWithCannedPolicy(
            cloudfrontUrl,
            s3Properties.getCloudfrontKeypairId(),
            SignerUtils.loadPrivateKey(s3Properties.getCloudfrontKeyfilePath()),
            getPresignedUrlExpiration()));


来源:https://stackoverflow.com/questions/57643059/how-does-versioning-work-on-amazon-cloudfront

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!