/b2-remove-prefix

A CloudFlare Worker for removing the /file/bucket-name/ part of a B2 bucket domain

Primary LanguageJavaScriptThe UnlicenseUnlicense

B2 Remove Prefix Worker

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

Why

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.

License

The Unlicense

Installation

  1. Use wrangler to generate a new app
wrangler generate myapp https://github.com/phistrom/b2-remove-prefix
  1. Overwrite the wrangler-generated wrangler.toml with a copy of wrangler.toml.example.
  2. Fill in zone_id, account_id, and routes fields as usual in wrangler.toml
  3. Also set the B2_BUCKET_NAME under [vars] in wrangler.toml to your B2 bucket name
  4. 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.