A CloudFlare worker script that removes the /file/{bucket-name}/
portion of
the URL when access a B2 Bucket.
- Works with the free tier of CloudFlare and CloudFlare Workers
- It's still free egress from your B2 bucket
- No performance penalty, just prettier URLs for your B2 assets
- Doesn't expose the name of your B2 bucket in the URL
If you followed this guide to set up a CloudFlare domain in front of Backblaze B2, you may have been disappointed by the length of the URL.
For instance, let's say you set up i.example.com
as a proxied CNAME on
CloudFlare in front of f001.backblazeb2.com
. Then for you to download a file
called /test.txt
in the root of the bucket example-bucket
, the URL would be
https://i.example.com/file/example-bucket/test.txt
. By putting this Worker
script in front of it, your URL becomes a simple
https://i.example.com/test.txt
.
- Use wrangler to generate a new app
wrangler generate myapp https://github.com/phistrom/b2-remove-prefix
- Overwrite the wrangler-generated
wrangler.toml
with a copy ofwrangler.toml.example
. - Fill in
zone_id
,account_id
, androutes
fields as usual inwrangler.toml
- Also set the
B2_BUCKET_NAME
under[vars]
inwrangler.toml
to your B2 bucket name - Upload to the routes you specified with
wrangler publish
Alternatively, you could also just copy and paste the code from index.js
into the Quick Edit editor in your CloudFlare dashboard. Just be sure to also
set the B2_BUCKET_NAME
and CF_CACHE_TTL
environment variables for this
Worker by going into its settings.