serverless-image-handler icon indicating copy to clipboard operation
serverless-image-handler copied to clipboard

Cloudfront batch invalidations.

Open jasonobrown opened this issue 7 years ago • 7 comments

I’m currently using an older version that still uses Thumbor. I’m getting ready to switch to the newest version, but I wanted to know if the newer version will fix one of my major problems. I have some javascript on my website that calculates the exact image dimensions needed for each image on page load and then builds the url for the image (for example: distrobution.cloudfront.net/100x100/path/image.jpg). It works great and is super SEO friendly. However, the one drawback is that when image.jpg is changed you cannot invalidate all the versions of it in cloudfront without invalidating everything in the bucket (not SEO friendly at all). Invalidating /path/image.jpg doesn’t do any good because the images being loaded are all /widthxheight/path/image.jpg and there’s no way to know what dimensions for a particular image exist. Cloudfront doesn’t allow wildcards at the beginning of a path, so...

All that to say, my question is: Does the newest version build the path differently, or can the way the path is built be changed easily in the newest version? I think something like distrobution.cloudfront.net/path/image_name/100x100/image.jpg would work best, but even if I could narrow it down to only having to invalidate the folder that the image is in (distrobution.cloudfront.net/path/100x100/image.jpg) would be drastically better. I’m open to other suggestions on possible solutions.

jasonobrown avatar Jun 20 '19 00:06 jasonobrown

You can override the parseRequestType callback to handle the same format as Thumbor. Otherwise you have to pass your parameters using the new JSON format:

{
    "key": "image.jpg"
    "edits": {
        "resize": {
            "width": 100,
            "height": 100,
        }
    }
}

then encode to base64. The base64 encoded solution has the advantage of hiding the parameters to the end user.

fabiovasini avatar Jun 20 '19 07:06 fabiovasini

Ok, but do either of those options impact my issue? If it's the exact same format as Thumbor, the issue still exist. If I can change it to be different than Thumbors format, then that may help. I'm assuming the base64 encoded option doesn't offer any help at all unless invalidating those images would work differently in cloudfront?

jasonobrown avatar Jun 20 '19 20:06 jasonobrown

Hey @jasonobrown: CloudFront invalidation has been something we've been looking into for some time to build into the solution as you mentioned above. Since invalidation doesn't apply to all use cases and can get expensive for customers who don't necessarily need it, we decided to keep it on the backlog for a future release.

One potential solution that we've identified for this is creating a small Lambda function that can be optionally deployed via the template that's triggered when the version of the object is changed. This would then trigger invalidations via the CloudFront API so that the newest version is delivered and cached. We'd love to hear any thoughts/feedback on this though to help deliver a better experience!

hayesry avatar Jun 26 '19 14:06 hayesry

@hayesry the potential solution you described sounds great, but I have no idea how it would work. Are the images cached in cloudfront somehow connected to the base image in s3? Or, would the lambda function try to run an invalidation for all possible matches of that image? For example:

bucket/folder/image.jpg exist in s3 bucket/200x200/folder/image.jpg is what exist in cloudfront

Does versioning somehow connect those 2 images?

jasonobrown avatar Jul 02 '19 19:07 jasonobrown

@jasonobrown , sorry for the delayed response. For your last question, I would say you can see "Popular objects" on Amazon CloudFront console.

beomseoklee avatar Feb 17 '20 21:02 beomseoklee

@hayesry - We are having an issue with S3 consistency when updating an image, cloudfront sometimes still uses the old version when retrieving from the bucket.

{"bucket":"xxxxxxxx","key":"images/catimg/172.jpg","edits":{"resize":{"width":320,"height":320,"fit":"inside","withoutEnlargement":true}},"version":"ff22345c3a39e0b46833b427210f67ad4fcaf14ca"}

We currently pass the "Version" as a parameter which is a hash (sha1) of the original file contents. Is it possible to change the image request to only cache it for a short time if the hash of the s3 file doesn't match the hash in the parameters? I could potentially write it but wondering if it's something you'd want in the main code or we should just update for us?

bretto36 avatar Mar 17 '20 04:03 bretto36

@jasonobrown we have updated our solution, and I believe your issue has been fixed. If you still see the issue with the latest version (v6.0.0), please feel free to reopen the issue.

You can refer to the recent changes here.

bitcldr avatar Dec 21 '21 19:12 bitcldr