The original post: /r/datahoarder by /u/rojo_salas on 2024-08-17 06:52:05.
Hey Guys!
If you’re using Backblaze B2 for your storage needs and want to make your URLs look more professional or customized, you can easily set up a custom subdomain to serve multiple B2 buckets. Here's a step-by-step guide on how to do this using Cloudflare.
Note: This is only based on my working knowledge and research. There may be another or better way to do this but this is my take for this. I may have said/done something wrong, feel free to correct me so I can correct this post.)
Materials Used:
- Custom Subdomain: b2.rojosalas.com
(Replace with your own) - CNAME for Backblaze: f002.backblazeb2.com
(Based on your B2 Friendly URL)
Step 1: Set Up Your CNAME Record
- Log in to Cloudflare:
- Go to Cloudflare Dashboard and select your domain.
- Navigate to DNS Settings:
- Click on the DNS tab.
- Add a CNAME Record: This CNAME setup directs requests from b2.rojosalas.com to your Backblaze B2 bucket endpoint.
- Type: CNAME
- Name:
b2 - Target: f002.backblazeb2.com
- Proxy Status: Proxied (Cloudflare will handle the requests and apply your rules).
Step 2: Create a Cloudflare Transform Rule
- Navigate to Transform Rules:
- Go to Rules > Transform Rules in the Cloudflare dashboard.
- Create a New Transform Rule:
- Click Create Transform Rule.
- Configure the Rule: This rule will ensure that requests to b2.rojosalas.com will be properly mapped to the Backblaze B2 URL with the
/fileprefix.- Name: Provide a name for your rule (e.g., "Rewrite Backblaze B2 Paths").
- When incoming requests match:
- Expression:
(http.host eq "b2.rojosalas.com") or (http.host eq "https://b2.rojosalas.com") - Path Matching: No separate field, include in the rule directly.
- Expression:
- Rewrite URL:
- Rewrite to:
Dynamic - Value:
concat("/file", http.request.uri.path)
- Rewrite to:
Example Usage
With the transform rule set up, you can now use your subdomain to access multiple buckets through the subdirectory. Here’s how it works:
- BucketA:
- BucketB:
With this setup, you can easily reference and serve files from multiple Backblaze B2 buckets under one custom subdomain.
Note: This is only based on my working knowledge and research. There may be another or better way to do this but this is my take for this. I may have said/done something wrong, feel free to correct me so I can correct this post.