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:
- Sign up for a Backblaze B2 account.
-
Sign up for a Fastly account. You can publish apps to the default edgecompute.app subdomain free of charge, or you can publish apps to your own paid domain.
Note: If you want to use your own domain, you must purchase Fastly TLS, since Fastly Compute@Edge apps are not accessible on port 80 via plain HTTP. For more information about Fastly limitations and constraints, click here. - Install and configure the fastly command-line interface (CLI).
-
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
-
Enter the following command to change the directory to your new app.
% cd my-app
-
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:
- Create a private bucket in your Backblaze B2 account. Alternatively, if you want to use an existing public bucket, do not change its visibility to private until you set up your app.
- Create an application key with access to the appropriate bucket(s).
-
In Terminal, enter the following command to publish your app to the Fastly Compute@Edge platform.
% fastly compute publish
- 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 forms3.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 defaultedgecompute.app
subdomain, you must specify a bucket name in the configuration or setbucket_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 setbucket_name
to$host
, you must configure the appropriate CNAME record(s) for your custom domain.
- To direct all incoming requests to a single bucket, enter that bucket's name, for example,
- Allowed buckets
This is a comma-separated list of buckets to which the client is allowed access. If you supplied a literal bucket name forbucket_name
, rather than$path
or$host
, you must enter the same bucket name forallowed_buckets
. - Allow list bucket
Set this configuration item totrue
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 theallow_list_bucket
configuration item totrue
to allow this functionality. In most cases, you should setallow_list_bucket
tofalse
.
- Backblaze B2 endpoint
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
- Verify that you can access your content via the Fastly app.
- Change the visibility of the public bucket to private.
- Verify that your content is no longer accessible directly from Backblaze B2.
End users can no longer access your content directly from Backblaze B2.