Search code examples
amazon-web-servicescdnamazon-cloudfront

Refresh image in CDN immediately after upload to S3 with same file name


I'm using amazon Cloudfront CDN with S3 behind as the origin server to serve images like profile photos of users on my application. I store a user's profile photo with file name as <userId>.jpg. When the user changes his profile photo, how do I refresh the image in the CDN immediately ?

Since the filename is <userId>.jpg I cannot have a new name for the new image.


Solution

  • You'd want to store a count of how many times it has been changed and use something like <userId>.jpg?v=2 on your site. You'd have to turn query strings on for the CloudFront distribution.

    Without a filename change or query string change, you'd have to use a CloudFront invalidation, which will take up to about 15 minutes to take effect everywhere.