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/myimage.jpg, where the file myimage.jpg is actually stored in your Backblaze B2 bucket at https://mybucket.s3.us-west-001.backblazeb2.com/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. 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 the Backblaze web console.
    2. In the user menu in the upper-right corner of the page, select 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 the Backblaze web console.
    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 later.
    9. Click Lifecycle Settings to control how long to keep the files in your new bucket.
    Note
    If this is your first time creating a public bucket, complete the following tasks to ensure that you have the correct permissions to create a public bucket:

    1. Verify your email address.
    2. Have a payment history on file, or use the credit card form to pay a small fee that is credited to your account balance.

    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 the Backblaze web console.
    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 under B2 Cloud Storage, 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 the Backblaze web console.
    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 S3 URL, for example, my-bucket.s3.us-west-001.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.

    Create an Origin Rule

    By default, Cloudflare will simply pass the incoming host header, for example, photos.mysite.com, to the origin, rather than rewriting it to the origin hostname, for example, my-bucket.s3.us-west-001.backblazeb2.com. You must create an Origin rule to configure Cloudflare to rewrite the host header when it requests files from your Backblaze B2 bucket.

    In the following steps, the domain is mysite.com, the hostname is photos, and the bucket origin is my-bucket.s3.us-west-001.backblazeb2.com.

    Note
    You must have Cloudflare's Enterprise plan enabled in order to rewrite a Hostname.

    1. In the Cloudflare left navigation menu, click Rules.
    2. Click Origin Rules > Create Rule.
    3. In Rule name, enter a descriptive name.
    4. Leave Custom filter expression selected.
    5. Under When incoming requests match…, set the Field to Hostname.
    6. Set the Operator to equals
    7. Set the Value to photos.mysite.com
    8. Under Host Header, select Rewrite to… and enter my-bucket.s3.us-west-001.backblazeb2.com.

    This rule configures Cloudflare to correctly set the host header in requests to Backblaze B2, allowing Backblaze B2 to parse your bucket name from the host header.

    Configure Cloudflare Caching

    By default, Cloudflare respects the origin web server’s cache headers, and caches only certain files, based on the file extension; see Default Cache Behavior for details. You may need to customize your Cloudflare cache behavior to meet your requirements. See the Cloudflare Cache documentation for details of Cache Rules and other features.

    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>/<filename> to display your file.

    For example, https://photos.mysite.com/img.jpg is mapped to https://my-bucket.s3.us-west-001.backblazeb2.com/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/.

    You must also edit your redirect rule to prevent it from redirecting such requests to the “file not found” page.
    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 index.html, requests https://my-bucket.s3.us-west-001.backblazeb2.com/index.html from the origin, and returns the HTML content to the client.


    Was this article helpful?