Use Fastly Compute@Edge with Backblaze B2 Cloud Storage

Fastly Compute@Edge enables serverless functions to run as close as possible to the end user. For more information about how you can use Fastly Compute@Edge with your Backblaze B2 account, click here.

Create a Fastly Compute@Edge Application

The Backblaze B2 GitHub repository contains full source code and configuration details for a Fastly Compute@Edge project.

Before you begin, complete the following tasks:

  1. In Terminal, enter the following command to create a directory for your app changing the app name, shown in the example as my-app, to your own app name.
    % mkdir my-app
  2. Enter the following command to change the directory to your new app.
    % cd my-app
  3. Enter the following command to initialize a new Compute@Edge app locally in the Fastly CLI using the Backblaze repository as a template for your own app, supplying your own app name at the prompt.
    % fastly compute init
    --from=https://github.com/backblaze-b2-samples/fastly-b2-rust

The following response is returned.

Creating a new Compute@Edge project (using --from to locate package template).

Press ^C at any time to quit.

WARNING: When using the --from flag, the project language cannot be inferred.
Please either use the --language flag to explicitly set the language or ensure the
project's fastly.toml sets a valid language.

✓ Validating directory permissions

Name: [my-app] 
Description: 
Author (email): [developer@example.com] 

✓ Fetching package template
✓ Reading package manifest
✓ Setting package name in manifest to "my-app"
✓ Setting description in manifest
✓ Setting authors in manifest to 'developer@example.com'
✓ Saving manifest changes
✓ Initializing package

Initialized package fastly-b2-test to:
	/Users/developer/src/myapp

To publish the package (build and deploy), run:
	fastly compute publish

To learn about deploying Compute@Edge projects using third-party orchestration tools, visit:
	https://developer.fastly.com/learning/integrations/orchestration/


SUCCESS: Initialized package fastly-b2-test

Publish Your Fastly Compute@Edge Application

Before you begin, complete the following tasks:

  1. In Terminal, enter the following command to publish your app to the Fastly Compute@Edge platform.
    % fastly compute publish
  2. Enter your app's Fastly service name and domain at the prompts, as well as the following settings:
    • Backblaze B2 endpoint
      This is shown in your bucket's details in the Backblaze B2 web UI, and has the form s3.us-west-001.backblazeb2.com.
    • Your Backblaze B2 Application Key and corresponding ID
    • Bucket name
      This is the name of a Backblaze B2 bucket or a directive to use part of the incoming URL as the bucket name:
      • To direct all incoming requests to a single bucket, enter that bucket's name, for example, acme-images.
      • To use the initial segment of the incoming URL path as the bucket name, enter $path. Incoming URLs must be in the following form:
        https://my-app.edgecompute.app/acme-images/logos/acme.png
        Note: If you use the default edgecompute.app subdomain, you must specify a bucket name in the configuration or set bucket_name to $path and pass the bucket name in the path.
      • To use the initial subdomain in the incoming URL hostname as the bucket name, enter $host. Incoming URLs must be in the following form:
        https://acme-images.my-domain.com/logos/acme.png
        Note: If you set bucket_name to $host, you must configure the appropriate CNAME record(s) for your custom domain.
    • Allowed buckets
      This is a comma-separated list of buckets to which the client is allowed access. If you supplied a literal bucket name for bucket_name, rather than $path or $host, you must enter the same bucket name for allowed_buckets.
    • Allow list bucket
      Set this configuration item to true to allow clients to list bucket contents. Backblaze B2 buckets, even those that are public, do not allow anonymous clients to list the bucket's objects. Set the allow_list_bucket configuration item to true to allow this functionality. In most cases, you should set allow_list_bucket to false.

The Fastly CLI creates the necessary domain, backend, and dictionaries and uploads and activates your app. The following response is returned.

✓ Creating domain 'my-app.edgecompute.app'...
✓ Creating backend 'b2_origin' (host: s3.us-west-001.backblazeb2.com, port: 443)...
✓ Creating dictionary 'bucket_auth'...
✓ Creating dictionary item 'b2_application_key_id'...
✓ Creating dictionary item 'b2_application_key'...
✓ Creating dictionary 'config'...
✓ Creating dictionary item 'allow_list_bucket'...
✓ Creating dictionary item 'allowed_buckets'...
✓ Creating dictionary item 'bucket_name'...
✓ Creating dictionary item 'endpoint'...
✓ Uploading package
✓ Activating version

Manage this service at:
	https://manage.fastly.com/configure/services/O2hGSAMPLEVALUE1cJIAc3

View this service at:
	https://my-app.edgecompute.app


SUCCESS: Deployed package (service O2hGESQKOPSDMtF1cJIAc3, version 1)

Update Your Web Pages

You must update your code to reference your Fastly Compute@Edge app rather than the Backblaze B2 bucket. For example, replace all occurrences of acme-images.s3.us-west-001.backblazeb2.com with images.acme.com.

Set Bucket Visibility

  1. Verify that you can access your content via the Fastly app.
  2. Change the visibility of the public bucket to private.
  3. Verify that your content is no longer accessible directly from Backblaze B2.

End users can no longer access your content directly from Backblaze B2.