Optimize performance of serving many images
What's your recommendation on serving large amount of images with CF ? I currently have them stored in R2 with file sizes range from 20 ~ 150kb each, with pre-signed URL on the server side (EC2) returned to client to fetch images.
Problems I noticed are presigning takes too long and probably im also not leveraging CDN from CF. One of my primary concern was not to put image storage bucket public by default, thus better gated and tried pre-signing url.
With CF, maybe the best option going forward is to have a CF worker binding with R2 bucket, intercept all image access requests, do simple verification, cache CDN and fetch the image directly to client? Thanks!
4 Replies
Presigning is an offline operation, only thing you'd be bound by is the response time of your server (and the ms it takes to compute the signature). You'd not be leveraging cache though yea. You could use a worker to serve images from the r2 bucket, you'd be paying for worker requests/cpu time but fairly inexpensive. You'd have to use the Cache API manually: https://developers.cloudflare.com/workers/runtime-apis/cache/ but very much as an option and very flexible in whatever verification you want to do (JWTs, external calls, etc). https://github.com/kotx/render is a r2 serving library which takes care of caching/custom 404/etc which could be either used directly or at least as a reference
Thanks @Chaika ! I did a worker with caching api + binded r2 bucket. I wonder if would be simpler just for the caching part if i enable custom domain name on the bucket directly tho as it might save some cost on the worker?
Worker would always have to run to check auth, and workers also always run in front of cache
Pro has hmac firewall option which you could use to make essentially presigned URLs with custom domain cache but you'd be back at the presigned issue
Worker seems cheap anyways so i might stick with it for auth at least and flexibility. Thanks!