Backblaze B2 SDK

2.10.4 · active · verified Wed Apr 15

The `b2sdk` library provides a comprehensive Python SDK for interacting with Backblaze B2 Cloud Storage. It allows developers to manage buckets, upload and download files, handle file versions, and more. The current version is 2.10.4, with releases occurring regularly to add features, fix bugs, and maintain compatibility.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `B2Api` client, authorize with your Backblaze B2 credentials (retrieved from environment variables for security), and then list your B2 buckets. This is a common first step for any interaction with B2 Cloud Storage.

import os
from b2sdk.v3 import B2Api
from b2sdk.v3.account_info.sqlite_account_info import SqliteAccountInfo

# It's recommended to store credentials in environment variables
APPLICATION_KEY_ID = os.environ.get('B2_APPLICATION_KEY_ID', 'YOUR_KEY_ID')
APPLICATION_KEY = os.environ.get('B2_APPLICATION_KEY', 'YOUR_KEY')

if APPLICATION_KEY_ID == 'YOUR_KEY_ID' or APPLICATION_KEY == 'YOUR_KEY':
    print("Please set B2_APPLICATION_KEY_ID and B2_APPLICATION_KEY environment variables.")
else:
    try:
        # Initialize account info storage (can be SqliteAccountInfo, InMemoryAccountInfo, etc.)
        info = SqliteAccountInfo()
        b2_api = B2Api(info)

        # Authorize with Backblaze B2
        b2_api.authorize_account(
            realm='production',  # Or 'test' for integration testing
            application_key_id=APPLICATION_KEY_ID,
            application_key=APPLICATION_KEY
        )
        print("Successfully authorized with Backblaze B2.")

        # Example: List your buckets
        print("\nListing buckets:")
        buckets = b2_api.list_buckets()
        if buckets:
            for bucket in buckets:
                print(f"  - {bucket.name} (ID: {bucket.id_}, Type: {bucket.bucket_type})")
        else:
            print("  No buckets found.")

    except Exception as e:
        print(f"An error occurred: {e}")

view raw JSON →