1 May 2026
This month I launched a character AI app that lets you chat with characters. Each character has many thumbnails stored in Cloudflare R2. I put R2 behind a custom domain, pointed Cloudflare at it, and called it a day.
Before launch, I was thinking about how to handle media URLs while I started out with simple public URLs. I knew I'd have to solve this eventually, and I'd rather get it right early than rearchitect later. So I spent a weekend going down the rabbit hole.
The first version was embarrassingly simple. Store the object key in Postgres, prepend the CDN domain, done:
https://thumbnails.myapp.ai/alexis-noir-SkfxyKLbW3/0.jpg
It worked. Images loaded fast. Cloudflare cached them since I had added cache rules. I moved on to other things.
The problem is obvious. That URL works for anyone. No token, no expiry, nothing!!! Anyone can copy it or share it. I wouldn’t be able to revoke it because it's just a path to a file. The CDN in front of it is doing nothing for security- it's just making the public file load faster.
I knew about S3 pre-signed URLs. The idea is clean: your server generates a temporary URL with a cryptographic signature. The signature expires, so even if someone copies the URL, it stops working after an hour.
I set it up. Each API response now returned URLs like
https://thumbnails.myapp.ai/thumbnails/alexis-noir-SkfxyKLbW3/0.jpg
?X-Amz-Algorithm=AWS4-HMAC-SHA256
&X-Amz-Credential=AKIA.../thumbnails/aws4_request
&X-Amz-Date=20260321T143052Z
&X-Amz-Expires=3600
&X-Amz-Signature=9f2e8a...
Secure, expiring, and……. completely un-cacheable.
Every signature is different because the timestamp is different. Every different URL is a different cache key. Cloudflare sees X-Amz-Signature=9f2e8a and ?X-Amz-Signature=b41c3d as two completely unrelated resources.
Twenty thumbnails on a page. A thousand users a day. That's 20,000 R2 reads for the same 20 images, over and over. This number could scale really fast if the number of users grow. The CDN had a 0% hit rate.
Signing and caching seem fundamentally opposed. Signing needs unique URLs (so they can expire). Caching needs identical URLs (so the cache can match them). It looks like you can't have both.
Except you can, if you think about what "unique" actually needs to mean.
A signed URL doesn't need to be unique per-request. It needs to be unique per-time-period. If two users ask for the same image within the same hour, or the same day, there's no reason those URLs should be different.
I stopped using S3's signing library and wrote my own. Three lines:
const exp = Math.ceil(Date.now() / 86_400_000) * 86400;
const path = `/${s3Key}`;
const token = createHmac('sha256', secret).update(`${path}${exp}`).digest('hex');
exp is the next midnight UTC, in seconds. Math.ceil(Date.now() / 86_400_000) gives the number of days since epoch, rounded up. Multiply by 86400 to get back to seconds.
The key insight here is that every call to this function within the same UTC day returns the exact same exp. Same exp + same path = same HMAC = same token. Same URL, all day.
10:00 AM → /thumbs/abc/0.jpg?token=xyz123&exp=1774828800
2:30 PM → /thumbs/abc/0.jpg?token=xyz123&exp=1774828800
11:59 PM → /thumbs/abc/0.jpg?token=xyz123&exp=1774828800
Now caching works again. User A and User B get the same URL. The browser caches it. The CDN caches it. Everyone's happy.
At midnight, exp rolls to the next day. New exp, new token, new URL. The old URL expires. Anyone who saved it gets a 403 after midnight.
Regular CDNs can't validate tokens. Cloudflare's CDN doesn't know my HMAC secret. It'll serve cached images to anyone who asks. The CDN is a dumb cache. It doesn't run my code.
That's where Cloudflare Workers come in. A Worker is a small script that runs in Cloudflare's edge data centers. When a request comes in for my domain, the Worker handles it instead of just proxying to origin.
My Worker does this:
token and exp from the query stringexp is in the past → 403token → 403 if mismatchToken validation on every request. It results in a R2 read only on cache miss. The worker here acts like an origin.
The browser caches by the full URL including query params. Since we normalize exp to the nearest day, all requests to fetch the same resource within the same day end up hitting the browser cache. When exp changes at midnight, the browser thinks it's a new resource and re-fetches.
The edge cache TTL I configured is 30 days. But I don't want exp changing at midnight to cause an edge-cache miss too.
The Worker caches responses in Cloudflare's edge cache using caches.default. By default, the cache key is the full URL. Which means Monday's URL (?exp=111) and Tuesday's URL (?exp=222) would create separate cache entries. But we don’t want duplicate caching for the same resource.
const cacheKey = new URL(url.pathname, url.origin).href;
By stripping the query params, the cache key is just the pathname: /thumbnails/alexis-noir-SkfxyKLbW3/0.jpg. Monday and Tuesday both map to the same cache entry in the edge cache. Since the configured edge cache TTL is 30 days, R2 gets hit once per edge location per month.