Search code examples
amazon-s3http-headersamazon-web-servicesamazon-cloudfront

Best Practices on how to maintain images on S3 with the same name and invalidate the cache


I would like to understand if there are any best practices on how to maintain images on S3 and use cloudfront to access them. The reason why am asking this question is because of how S3 and cloudfront work together.

I upload an image, set an expiry using the meta tags on S3 and try to use the cloudfront URL I get the desired image. The problem is when I try to update the same image and it does not immediately reflect until the set expiry on the meta tags in met.

Is there a good way to get the latest image if there is a change on S3?


Solution

  • You need to tell CloudFront that the file it has cached has changed, so that it will get the updated version from S3. To do this you invalidate the file on CloudFront. Then the next time that file is requested, CloudFront will go back to S3 to get the latest version.