B2 CDN
A Cloudflare Worker for serving and browsing content in a private Backblaze B2 bucket.
- Serve the content in your B2 bucket for free
- Browse the content in your B2 bucket
Why?
- Backblaze B2 is cheap file storage
- Downloading from Backblaze B2 is normally $0.01 per GB
- Downloading from Backblaze B2 through Cloudflare's CDN is free
- Private buckets prevent people from downloading from your bucket directly
Setup
Install Wrangler
See Cloudflare's documentation for more information.
wrangler.toml
Copy wrangler.toml.example
to wrangler.toml
and fill in your own information
into the variables. Refer to
Cloudflare's documentation
for more information.
routes
The domains and subdomains this Worker will listen on. This needs to contain at least the domains specified in theMAIN_DOMAIN
andDIR_DOMAIN
vars below.vars
The followingvars
are required for the Worker to function correctly.B2ACCESS
Application key ID from Backblaze B2.B2BUCKET
The name of the B2 bucket to use.MAIN_DOMAIN
The domain from which files will be served. If a request comes in for any other domain (exceptDIR_DOMAIN
), it will be redirected to this domain.DIR_DOMAIN
The domain to use for directory listings.
kv_namespaces
Create a Workers KV namespace and bind it to this app with the nameB2CDN
triggers
B2 authorization tokens only last 24 hours. When triggered by a cron job, this Worker gets a new authorization token from the B2 API and stores it in Workers KV.
Secrets
Secrets
are like vars
but... secret. This Worker only needs one secret named
B2SECRET
which should contain the applicationKey (the secret part) of your
Backblaze B2 application key.
Protected File Listings
You can use Cloudflare Access to
require a login for certain subdomains. Let's say you want to publicly share the
files in your B2 Bucket, but not allow others to list the files in it, you can
set up a separate DIR_DOMAIN
and
place Cloudflare Access in front of it.
There's a lot of supported identity providers.
You can make it so only a certain IP can access the directory listing, or only
particular Google Accounts.
To Do
- Implement pagination for enormous folders
- Implement more efficient calls to
b2_list_file_names
License
MIT