A simple CDN made using Cloudflare Workers, Cloudflare R2, and Hono.
- A Cloudflare account
- R2 Plan (starts for free)
- KV namespace
Note
The "Deploy to Cloudflare" is back and can now create R2 buckets and KV namespaces. As such, you can use this button to deploy. However, should you wish to deploy manually, you can instead follow the instructions below.
To follow the instructions below, you must have Node.js installed. Additionally, I will be showing any terminal commands using Yarn (my preferred package manager), but any Node.js package manager will work with slight modifications.
- Download the project onto your machine by downloading the ZIP file and extracting it, or forking and then cloning the project using Git.
- Open a terminal in the location of the project and run
yarn install(oryarnfor short) to install the required dependencies. - Log in to Cloudflare by running
yarn wrangler login, which will open the browser and request permissions once logged in. - Create the R2 bucket by running
yarn wrangler r2 bucket create cdnif it doesn't already exist. If you get an error saying something along the lines of purchasing or upgrading, then you will need to go to your Cloudflare dashboard and click "R2 Object Storage" on the left (towards the bottom). Don't worry, R2 starts off being free for the first 10GB of storage. You can see the pricing here: https://developers.cloudflare.com/r2/pricing/#r2-pricing - Create the KV namespace by running
yarn wrangler kv namespace create cdn_users(for this one, the name can be anything you like) if one doesn't already exist. Alternatively, you can do this on the Cloudflare dashboard via "Storage & Databases" followed by "KV". Upon completion, you will receive an ID that will need to be set inside wrangler.toml. The existing preview ID can be removed as you will not need it, for example:id = "your id goes here", but do not change the binding! It must remainCDN_USERS. - Go to the "Account Home" page on your Cloudflare dashboard. At the top, next to your username, click the 3 dots, followed by "Copy Account ID". Next, you will want to go to wrangler.toml and replace the existing account ID with yours that you copied. For example:
account_id = "your account id goes here" - Just one more step left to go! Make sure you have saved the changes you've made in wrangler.toml. Next, run
yarn deploy, which will begin to deploy the Cloudflare worker on your account. Once completed, your CDN is now ready!
To add users, you must add them to the KV namespace. If you don't already know, KV stands for key/value pair. The key is the user's access token, and the value is something to identify them with (eg, username).
Users can be added via your Cloudflare dashboard by going to "Storage & Databases" followed by "KV" and then clicking on the namespace that was just created. As for doing it via the terminal, you would run the following: yarn wrangler kv key put --remote --binding=CDN_USERS "access token" "user identifier".
Important
It's important that you create strong access tokens to prevent random people from gaining access! I recommend at least 64+ characters! You can use a password manager or password generator to create them.
To use a custom domain, you will have to go to your Cloudflare dashboard, click on the "Workers & Pages" page and then click on the CDN worker. Once you've done this, you can click "Settings" at the top, where you should see "Domains & Routes". Next, you will click the "+ Add" button on the right, followed by "Custom Domain". Here you will enter one of the domains that you have set up to use Cloudflare. For example, you can enter something like cdn.mydomain.com.
If you need some help, I can try my best to help, but I cannot guarantee anything. First, have a look through existing issues first to make sure someone else hasn't already had the same issue. If it hasn't, then please create a new issue by clicking on the highlighted text. Please explain thoroughly and include images or videos where possible. That way, you'll have a better chance of getting a faster solution.
| Route | Description |
|---|---|
GET / |
Redirects to my website by default. However, you can change it inside wrangler.toml. |
GET /:key |
Displays the specified media that has been uploaded publicly. |
POST /upload |
Uploads media to the CDN. The Access-Token header is required! |
DELETE /:key |
Deletes media with the given key from the CDN. The Access-Token header is required! |
{
"Version": "14.0.1",
"Name": "cdn",
"DestinationType": "ImageUploader, TextUploader, FileUploader",
"RequestMethod": "POST",
"RequestURL": "https://your.domain/upload",
"Headers": {
"Access-Token": "Recommended to be 64+ characters"
},
"Body": "MultipartFormData",
"FileFormName": "image",
"URL": "{json:url}",
"DeletionURL": "{json:url}",
"ErrorMessage": "{json:error}"
}If you wish to use the example above:
- Set
RequestURLto be the domain you are using, andAccess-Tokento the one you created in your KV namespace. - Go to ShareX, Destinations, Custom uploader settings and then click Import. You can import from your clipboard, file, or URL.
- Follow the prompts that it gives you, and you should be done.