Integrate Fastly Compute@Edge with Backblaze B2
    • Dark
      Light

    Integrate Fastly Compute@Edge with Backblaze B2

    • Dark
      Light

    Article Summary

    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. 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 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.

    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.

    Create an Application Key

    You must create an app key with access to the bucket that you created in the previous task.

    Application keys control access to your Backblaze B2 Cloud Storage account and the buckets that are contained in your account.

    1. Sign in to your Backblaze account.
    2. In the left navigation menu under Account, click Application Keys.
    3. 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.
    4. Select All or a specific bucket in the Allow Access to Bucket(s) dropdown menu.
    5. Optionally, select your access type (Read and Write, Read Only, or Write Only).
    6. Optionally, select the Allow List All Bucket Names checkbox (required for the B2 Native API b2_list_buckets and the S3-Compatible API S3 List Buckets operations).
    7. 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.
    8. 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).
    9. Click Create New Key, and note the resulting keyID and applicationKey values.
    Note
    When you create a new app key, the response contains the actual key string, for example N2Zug0evLcHDlh_L0Z0AJhiGGdY. You can always find the keyID on this page, but for security, the applicationKey appears only once. Make sure you copy and securely save this value elsewhere.

    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:

    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): [[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

    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 web UI, and has the form s3.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 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.


    Was this article helpful?