Deliver Public Backblaze B2 Content Through Cloudflare CDN
    • Dark
      Light

    Deliver Public Backblaze B2 Content Through Cloudflare CDN

    • Dark
      Light

    Article summary

    The partnership between Backblaze and Cloudflare means that you can store content with Backblaze B2 Cloud Storage and serve it to users using Cloudflare's CDN (Content Delivery Network) with no download fees from Backblaze. This means that content can be stored in Backblaze B2 while being delivered with the high speed and low latency of one of the world's premier CDNs. Users access your site with the same URLs as before, but now they enjoy the speed and reliability of Cloudflare. As a webmaster, you can also use Cloudflare features like Edge cache expire TTL.

    Setting up your website with Cloudflare allows your site to serve data from one or more public Backblaze B2 buckets.

    For example, you might own a web site like photos.mysite.com. You can use Cloudflare to set up a file URL like https://photos.mysite.com/file/mybucket/myimage.jpg, where the file myimage.jpg is actually stored in your Backblaze B2 bucket at https://f000.backblazeb2.com/file/mybucket/myimage.jpg.

    Note
    To use Cloudflare with a private Backblaze B2 bucket, see Deliver Private Backblaze B2 Content Through Cloudflare CDN.

    Enable Backblaze B2

    Before you begin: You must have a Backblaze B2 Cloud Storage account. You can sign up here. If you already have a Backblaze account and the left navigation menu contains a B2 Cloud Storage section, your account is already enabled for Backblaze B2.

    1. Sign in to your Backblaze account.
    2. In the left navigation menu under Account, click My Settings.
    3. Under Enabled Products, select the checkbox to enable B2 Cloud Storage.
    4. Review the Terms and Conditions, and click OK to accept them. 

    Create a Public Bucket

    You can use an existing public bucket, or you can create a new public bucket. You must select Public as your privacy setting.

    1. Sign in to your Backblaze account.
    2. In the left navigation menu under B2 Cloud Storage, click Buckets.
    3. Click Create a Bucket.
    4. Enter a name for your bucket.
      Bucket names must be at least six characters and globally unique. A message is displayed if your bucket name is already in use.
    5. Select a privacy setting: Private or Public.
      Files that are in a private bucket require authentication to perform an action, for example, downloading. Public buckets do not require authentication so you can easily share files. You can change a bucket's privacy settings at any time.
    6. If applicable, enable a Backblaze B2 server-side encryption key.
    7. Enable Object Lock to restrict a file from being modified or deleted for a specified period of time.
    8. Click Create a Bucket, and copy the value that is in the Endpoint field; you may need this value for other processes.
    9. Click Lifecycle Settings to control how long to keep the files in your new bucket.

    Upload Files

    You must upload one or more files to your public bucket.

    You can upload files to a Backblaze B2 bucket from your local drive. The maximum single file size that you can upload through the Backblaze web console is 500 MB. If you need to upload larger files, click here for more information. You can also use the B2 Native API or the Command-Line Interface to upload files.

    1. Sign in to your Backblaze account.
    2. In the left navigation menu under B2 Cloud Storage, click Buckets.
    3. In your bucket details, click Upload/Download and click Upload.
    4. Drop your images from your local drive into the dialog box, or manually select your files.
    5. In the left navigation menu, click Browse Files to see your uploaded file and click the image filename to see more details.

    Locate your Bucket Origin

    1. Sign in to your Backblaze account.
    2. In the left navigation menu under B2 Cloud Storage, click Browse Files and navigate to the public bucket that you created.
    3. Click(info) for any file in the bucket.
    4. Copy the origin of the Friendly URL, for example, f000.backblazeb2.com.

    Configure Cloudflare

    Before you begin: You must have a Cloudflare account. You can select the Cloudflare billing plan that is appropriate for you, but you can use the "Free Website" option to use with Backblaze B2. For more information about Cloudflare's capabilities, visit the service plan page.

    1. Sign in to your Cloudflare account, and click Get Started (if you have not yet added a website) or click Add a Site (if you already added a website).
    2. Add your domain, and click Continue. Your domain is the hostname suffix that is used to retrieve your Backblaze B2 bucket's content through Cloudflare, for example, mydomain.com.
    3. Select a plan and click Continue.
    4. If your domain is not yet active on Cloudflare, complete the nameserver setup process.
    5. In the left navigation menu, click DNS and click Records.
    6. Click Add record.
      1. Select a record type. For example, if your site has separate areas for photos and videos, select CNAME.
      2. Enter your hostname under Name. For example, if you want to map your bucket to photos.mydomain.com, enter photos.
      3. In the Target field, enter the origin of your bucket.
      4. Optionally, enter a comment or tags to help you manage the record.
      5. Ensure that Proxy status is enabled, and ensure that (Proxied) is orange.
      6. Click Save.

    Configure Page Rules

    You must configure page rules to allow Cloudflare to fetch only your Backblaze B2 bucket from your domain. The DNS configuration maps your domain to any content that is hosted on the same Backblaze B2 origin as your bucket. Otherwise, someone could use your domain to fetch content from another customer's public bucket. To ensure this does not happen, Cloudflare lets you use page rules to scope requests to your bucket.

    For each Backblaze B2 bucket that you want to use with Cloudflare, create two page rules to allow Cloudflare to cache your content. In the following steps, the domain is mysite.com, the hostname is photos, and the bucket is mybucket. The URL in the Forwarding URL rule is https://secure.backblaze.com/404notfound, but you may want to use the URL for a similar "file not found" page of your own.

    1. In the Cloudflare left navigation menu, click Rules.
    2. Click Page Rules, and click Create Page Rule.
      1. Set the URL to https://photos.mysite.com/file/mybucket/*.
      2. In the Pick a Setting field, select Cache Level.
      3. In the Select Cache Level field, select Standard.
      4. Click Save and Deploy Page Rule.
    3. Click Create Page Rule.
      1. Set the URL to https://photos.mysite.com/file/*/*.
      2. In the Pick a Setting field, select Forwarding URL.
      3. In the Select status code field, select 302 - Temporary Redirect.
      4. Set the destination URL to https://secure.backblaze.com/404notfound.
      5. Click Save and Deploy Page Rule.

    These rules configure Cloudflare to cache the contents of your bucket and ensure that no one can use your domain to fetch contents from another public Backblaze B2 bucket. Attempts to do so are instead redirected to the "file not found" page.

    Configure Your SSL/TLS Encryption Mode

    Backblaze B2 serves files securely through HTTPS and not HTTP. You must ensure that your SSL/TLS encryption mode is set accordingly. For more information, see Cloudflare’s documentation on encryption modes.

    1. In the Cloudflare left navigation menu, click SSL/TLS.
    2. Click Overview.
    3. Ensure that SSL/TLS is set to Full (strict).

    Access your Backblaze B2 Bucket Content Through Cloudflare CDN

    Use the format https://<hostname>.<domain>/file/<bucketname>/<filename> to display your file.

    For example, https://photos.mysite.com/file/xxx/img.jpg is mapped to https://f000.backblazeb2.com/file/xxx/img.jpg.

    Remember, you must use an https:// (TLS-secured) URL. Backblaze does not support http:// URLs.

    Configure a Home Page for your Domain

    Note
    This is an optional configuration.

    You can configure a Transform Rule, specifically a Rewrite URL rule, to have Cloudflare serve an HTML file from your Backblaze B2 bucket in response to requests for the root URL of your domain, for example, https://photos.mysite.com/.

    1. In Cloudflare, navigate to Rules and select Transform Rules.
    2. Click Create Rule.
    3. Enter a descriptive name for the rule, for example, Rewrite root to index.
    4. In the field When incoming requests match…, click Custom filter expression.
      1. Set Field to URI Full.
      2. Set Operator to equals.
      3. Set Value to the root of your domain, for example, https://photos.mysite.com/.
    5. In the field Set Rewrite parameters, click Path and click Rewrite to....
      1. Select Static.
      2. Enter the target path, without the leading '/', for example, file/mybucket/index.html.
    6. Click Deploy.

    Now, when the incoming URL matches https://photos.mysite.com/, Cloudflare rewrites the path to file/mybucket/index.html, requests https://f000.backblazeb2.com/file/mybucket/index.html from the origin, and returns the HTML content to the client.


    Was this article helpful?