
Cloudflare and Backblaze B2 are two powerful tools that can help you protect and store your online data. Cloudflare is a content delivery network (CDN) that can speed up your website and protect it from cyber threats. By integrating Cloudflare with Backblaze B2, you can automatically store your website's backups in a secure and cost-effective way.
Backblaze B2 is a cloud storage service that offers affordable and reliable storage for your files. With the Cloudflare Backblaze B2 integration, you can configure Cloudflare to automatically upload your website's backups to Backblaze B2 whenever you make changes. This way, you can ensure that your data is always up-to-date and easily recoverable in case of an emergency.
To get started with the integration, you'll need to create a Backblaze B2 account and generate an application key. This key will be used to connect Cloudflare to your Backblaze B2 account, allowing you to configure automated backups.
Prerequisites
To get started with Backblaze and Cloudflare, you'll need to sign up for a Backblaze account and follow the bucket setup instructions. This will give you access to 10GB of storage, free of charge, no credit card required.
You'll also need to sign up for a Cloudflare account, which will allow you to publish Workers to the default *.workers.dev subdomain free of charge, or to your own paid domain.
To access your uploaded files from your own domain, say yes when prompted and enter the domain or subdomain you want to use, such as files.example.com or assets.example.com.
You'll need to remember what you set, as you'll be creating a CNAME for it in the next step.
Here are the accounts you'll need to sign up for:
- Backblaze
- Cloudflare
You'll also need your own DNS domain, which you can register at Cloudflare for a few dollars a year, or your local equivalent.
Configuring the Client
The only change required in your client application is the S3 endpoint configuration.
Set it to your Cloudflare Worker’s endpoint rather than your Backblaze account’s S3 endpoint.
This is a crucial step as it allows your client to communicate with Cloudflare Workers instead of directly accessing the Backblaze S3 Compatible API.
You'll need to use the same Application Key and ID as you did when directly accessing the Backblaze S3 Compatible API.
This means you won't need to update your application's credentials, just the S3 endpoint configuration.
Image Bucket
Creating an image bucket on Backblaze is a straightforward process. You'll need to log in to Backblaze and click on "Buckets" under B2 Cloud Storage, then click "Create a Bucket." Give your bucket a unique name and make it public.
Note that the bucket name must be globally unique within Backblaze B2, so you can't just call it something like "myfiles." You can call it literally anything as long as there isn't already a Backblaze B2 bucket with that name.
To upload a test file to your new bucket, click on "Upload/Download" and select the file you want to upload. Make a note of the hostname in the friendly URL of the uploaded file, which will be of the form https://f004.backblazeb2.com/file/metadaddy-public/smiley.png.
Here are the steps to create a new bucket:
- Log in to Backblaze and click on "Buckets" under B2 Cloud Storage.
- Click "Create a Bucket" and give your bucket a unique name.
- Make the bucket public and leave the other settings with their default values.
Once you've created your bucket, you can upload test files to it and make a note of the hostname in the friendly URL. This will be useful later when you need to rewrite requests to use your custom subdomain.
Optimizing Cache Efficiency
Cloudflare caching is enabled by default, so you don't need to do anything further to check its status.
However, it's worth noting that Cloudflare caching can affect your Backblaze 'class B transactions' allowance/charge.
Checking Cloudflare caching status is as simple as ensuring it's enabled by default, but it's good to know how it can impact your usage.
Cloudflare caching can be a game-changer for optimizing cache efficiency, but it's essential to understand its effects on your Backblaze costs.
You can check the Cloudflare caching status to see if it's enabled and working correctly, which can help you save on Backblaze costs.
By understanding how Cloudflare caching works, you can make informed decisions about your Backblaze usage and optimize your cache efficiency.
Proxying B2 Operations
Proxying B2 Operations is a powerful feature that allows you to integrate Backblaze B2 with other systems and services. This is done by using a Cloudflare Worker, which acts as a proxy between the client and Backblaze B2.
The Worker validates the client's signature and creates its own signed request, which is then sent to Backblaze B2. This ensures that the request is authenticated and verified before being processed.
The validation and signing process imposes minimal overhead, even for requests with large payloads. The Worker uses a SHA-256 digest of the request payload, which is included in the x-amz-content-sha256 HTTP header, rather than the payload itself.
Here's a high-level overview of the message flow:
- A user performs an action in a Backblaze B2 client application.
- The client app creates a signed request and sends it to the Cloudflare Worker.
- The Worker validates the client's signature and creates its own signed request.
- The Worker sends the signed request to Backblaze B2.
- Backblaze B2 validates the signature and processes the request.
- Backblaze B2 returns the response to the Worker.
- The Worker forwards the response to the client app.
- The Worker sends a notification to the webhook recipient.
- The recipient takes some action based on the notification.
The Worker returns Backblaze B2's response to the client unchanged and creates a JSON-formatted webhook notification containing the following parameters:
- contentLength: Size of the request body, if there was one, in bytes.
- contentType: Describes the request body, if there was one.
- method: HTTP method, for example, PUT.
- signatureTimestamp: Request timestamp included in the signature.
- status: HTTP status code returned from B2 Cloud Storage.
- url: The URL requested from B2 Cloud Storage.
B2 Setup
Backblaze and Cloudflare can be set up together in a matter of minutes. This integration allows for seamless data transfer and storage.
To get started, you'll need to create a Backblaze B2 account and enable the Cloudflare integration. This can be done through the Backblaze control panel.
The integration requires a Cloudflare account with a paid plan, as the free plan does not support API access.
B2
Backblaze B2 is a cloud storage solution that's similar to Amazon AWS S3, but at a fraction of the cost. The first 10GB of storage are entirely free, which is what we'll be using for this tutorial.
The Bandwidth Alliance makes egress between Backblaze and Cloudflare entirely free, which is a game-changer for bandwidth fees.
You can sign up for a Backblaze B2 account and get started with the free tier.
Here's a brief overview of the message flow when proxying Backblaze B2 operations with a Cloudflare Worker:
- A user performs an action in a Backblaze B2 client application, for example, uploading an image.
- The client app creates a signed request, exactly as it would for Backblaze B2, but sends it to the Cloudflare Worker rather than directly to Backblaze B2.
- The Worker validates the client’s signature, and creates its own signed request.
- The Worker sends the signed request to Backblaze B2.
- Backblaze B2 validates the signature, and processes the request.
- Backblaze B2 returns the response to the Worker.
- The Worker forwards the response to the client app.
- The Worker sends a notification to the webhook recipient.
- The recipient takes some action based on the notification.
The validation and signing process imposes minimal overhead, even for requests with large payloads, since the signed data includes a SHA-256 digest of the request payload.
Bucket Creation
To create a bucket in Backblaze B2, you'll want to register an account and create a new bucket in the "Buckets" UI.
Give your bucket a unique name, such as "jross-files" or any other name that isn't already taken by another Backblaze B2 bucket.
Make sure to set the bucket to public, so people can view your uploaded media.
From the "App keys" section of the UI, click "Add a New Application Key" and give it a name like "sharex-uploader".
Restrict the application key to your bucket for security, and leave the access type as "Read and Write".
You won't be able to retrieve the application key after generating it, so be sure to save it securely.
Upload a temporary test file to your bucket to gather information about its hosting location.
Note down the domain where your file is hosted, such as https://f002.backblazeb2.com/, as you'll need it for the next steps.
If you already have a B2 Cloud Storage bucket, you can skip this section, but make sure it's public and has a unique name.
The bucket name must be globally unique within Backblaze B2, so choose a name that isn't already taken.
Upload a test file to your new bucket to see its details, including its various URLs.
Make a note of the hostname in the friendly URL, such as f004.backblazeb2.com, as you'll need it for rewriting requests.
Set Entity Tag

Setting an Entity Tag is a crucial step in optimizing cache performance. An Entity Tag, or ETag, acts like a version number for a file, allowing expired cache items to be served by comparing the ETag of the file on the server with the ETag from the expired cache file.
To set an ETag, go back into your Header Response Modification rule and add a new line after the 'x-bz' removal lines. Choose 'Set Dynamic' from the drop-down, and ETag for the header name.
For the value box, you'll concatenate some of the Backblaze headers into a long identifier. This can be done by copying and pasting the following string without changing anything.
You'll then hit save, and make another cURL request, where you'll see that the etag has appeared. This indicates that the ETag has been successfully set.
Comments
The B2 setup is a crucial part of any home or office installation. A well-set-up B2 can save you a significant amount of time and energy in the long run.
The key to a successful B2 setup is to ensure all components are properly connected and secured. This includes the B2 unit itself, the control panel, and any additional features such as sensors or relays.
A typical B2 setup takes around 2-3 hours to complete, depending on the complexity of the installation. This time frame can vary depending on the individual's level of experience and the specific requirements of the setup.
The B2 unit should be placed in a well-ventilated area, away from any heat sources or flammable materials. This is a critical safety precaution to prevent any potential hazards.
Regular maintenance is essential to keep your B2 setup running smoothly. This includes checking the unit's oil levels, cleaning the air filters, and replacing any worn-out parts.
It's also a good idea to keep a log of your B2 setup's performance, including any issues that arise and the steps taken to resolve them. This will help you identify any potential problems before they become major issues.
Cloudflare Setup
In your Cloudflare dashboard, choose the domain you'll be using and navigate to the DNS settings.
To add a CNAME record, click 'Add Record' and select CNAME from the type drop-down. For a root domain like example.com, enter '@' in the name field, while a subdomain like 'massets' requires the subdomain name.
Make sure to enter the friendly URL for the target, excluding the https:// or /file/bucket-name part. For example, if the friendly URL is 'f003.backblazeb2.com', enter just 'f003.backblazeb2.com' in the target field.
Ensure 'Proxy Status' is set to 'Proxied' to make the setup work.
Create DNS Subdomain for Image Host
To create a DNS subdomain for your image host, you'll need to add a DNS CNAME record pointing to the bucket hostname. Create a subdomain in the Cloudflare dashboard by adding a DNS CNAME record.
In the Cloudflare dashboard, you can create a subdomain by adding a DNS CNAME record pointing to the bucket hostname. For example, I created files.superpat.com, which points to my bucket's hostname, f004.backblazeb2.com.
You'll need to test this by going to your test file's URL in your custom subdomain, but you may initially see a 522 "connection timed out" error from Cloudflare due to the default HTTP connection. To fix this, change the encryption mode from "Flexible" to "Full (strict)" in the SSL/TLS section of the Cloudflare dashboard.
If you're using a root domain, enter '@' in the name field when adding a CNAME record in the Cloudflare dashboard. Otherwise, enter the part of the subdomain you want to create. For example, for the subdomain 'massets', you'd enter 'massets' in the name field.
The target for the CNAME record should be the friendly URL, but only the domain part, not the https:// or the /file/bucket-name bit. For example, that's 'f003.backblazeb2.com'. Make sure 'Proxy Status' is showing 'Proxied' to ensure everything works correctly.
Final Steps
Now that you've set up your Backblaze bucket behind Cloudflare, it's time to reap the benefits. This setup can significantly lower your costs and improve the experience for your users.
You can expect to save money by leveraging Cloudflare's free account features, which are surprisingly robust. Cloudflare's free account has a lot to offer, and it's a great place to start.
By following these steps, you'll be able to create a seamless experience for your users. Your users will appreciate the improved performance and reliability of your application.
Cloudflare's free account is a great option for small to medium-sized projects, and it's a great way to test the waters before upgrading to a paid plan.
Frequently Asked Questions
What is the best alternative for Cloudflare?
If you're looking for a reliable alternative to Cloudflare, consider using Akamai CDN or Fastly Deliver, both of which offer robust content delivery networks and high-performance solutions for website acceleration and security.
Sources
- https://jross.me/free-personal-image-hosting-with-backblaze-b2-and-cloudflare-workers/
- https://techbits.io/free-s3-egress-backblaze-b2-cloudflare/
- https://www.backblaze.com/blog/use-a-cloudflare-worker-to-send-notifications-on-backblaze-b2-events/
- https://lowendspirit.com/discussion/3412/is-anyone-using-the-backblaze-b2-cloudflare-combo
- https://www.backblaze.com/blog/free-image-hosting-with-cloudflare-transform-rules-and-backblaze-b2/
Featured Images: pexels.com