- Print
- DarkLight
Fastly Compute@Edge Integration with Backblaze B2
- Print
- DarkLight
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 Cloud Storage account, click here.
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.
- Sign in to the Backblaze web console.
- In the user menu in the upper-right corner of the page, select My Settings.
- Under Enabled Products, select the checkbox to enable B2 Cloud Storage.
- Review the Terms and Conditions, and click OK to accept them.
Create a Bucket
You can create a new public bucket, or you can use an existing public bucket. Do not change the visibility on your bucket to private until you set up your app.
- Sign in to the Backblaze web console.
- In the left navigation menu under B2 Cloud Storage, click Buckets.
- Click Create a Bucket.
- 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. - Select a privacy setting: Private or Public.NoteYou can change a bucket's privacy settings at any time.
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. 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. - If applicable, enable a Backblaze B2 server-side encryption key.
- Enable Object Lock to restrict a file from being modified or deleted for a specified period of time.
- Click Create a Bucket, and copy the value that is in the Endpoint field; you may need this value later.
- Click Lifecycle Settings to control how long to keep the files in your new bucket.
Create an Application Key
You must create an app key with access to the bucket that you created in the previous task.
- Sign in to the Backblaze web console.
- In the left navigation menu under B2 Cloud Storage, click Application Keys.
- Click Add a New Application Key, and enter an app key name.
You cannot search an app key by this name; therefore, app key names are not required to be globally unique. Key names are limited to 100 characters and can contain letters, numbers, and "-", but not I18N characters, such as é, à, and ü. - Select All or select a specific bucket in the Allow Access to Bucket(s) menu.
- Optionally, select your access type (Read and Write, Read Only, or Write Only).
- Optionally, select Allow List All Bucket Names.
This option is required for the B2 Native API b2_list_buckets and the S3-Compatible API S3 List Buckets operations. - Optionally, enter a file name prefix to restrict application key access only to files with that prefix.
Depending on what you selected in step #4, this limits application key access to files with the specified prefix for all buckets or just the selected bucket. - Optionally, enter a positive integer to limit the time, in seconds, before the application key expires.
The value must be less than 1000 days (in seconds). - Click Create New Key, and note the resulting keyID and applicationKey values.
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:
- 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): [[email protected]]
✓ Fetching package template
✓ Reading package manifest
✓ Setting package name in manifest to "my-app"
✓ Setting description in manifest
✓ Setting authors in manifest to '[email protected]'
✓ 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
- 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 web console, and has the forms3.us-west-001.backblazeb2.com
. - Your Backblaze B2 app 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.