Time Limited URL (With Expiry)

Dear All,

I would like to check if it’s possible to do the following with workers in CF.

Basically, I have a CNAME called

download.example.com/$1
Forwarded to
original.download.root.url.example.com/*

Is there any way I could create a URL(File Download) that is unique and expires after a few hours.

  • Maybe I have some kind of unique hash that could calculate the “Expiry” date of the incoming URL hash, if it fails the check , redirect.

Any advise?

Hey @jonathantjh - should be possible! This is actually a great use-case for Workers KV - you can set up an expiring key, and match it to a URL. For instance, if the $1 in your URL example is something like an ID, you can look up that ID in KV and redirect to a known URL for that data. If you want the key to expire, you can give it a 3h expiration or whatever value you’d like:

const id = '45a468d4-21d4-466d-ace0-726274d05886'
const data = { redirectUrl: 'https://google.com' }
const threeHoursInSeconds = 60 * 60 * 3

NAMESPACE.put( // NAMESPACE is a KV namespace
  id, 
  JSON.stringify(data), 
  { expirationTtl: threeHoursInSeconds }
)
3 Likes