AccountInfo

AccountInfo stores basic information about the account, such as Application Key ID and Application Key, in order to let b2sdk.v2.B2Api perform authenticated requests.

There are two usable implementations provided by b2sdk:

They both provide the full AccountInfo interface.

Note

Backup applications and many server-side applications should implement their own AccountInfo, backed by the metadata/configuration database of the application.

AccountInfo implementations

InMemoryAccountInfo

AccountInfo with no persistence.

class b2sdk.v2.InMemoryAccountInfo[source]

AccountInfo which keeps all data in memory.

Implements all methods of AccountInfo interface.

Hint

Usage of this class is appropriate for secure Web applications which do not wish to persist any user data.

Using this class for applications such as CLI, GUI or backup is discouraged, as InMemoryAccountInfo does not write down the authorization token persistently. That would be slow, as it would force the application to retrieve a new one on every command/click/backup start. Furthermore - an important property of AccountInfo is caching the bucket_name:bucket_id mapping; in case of InMemoryAccountInfo the cache will be flushed between executions of the program.

__init__()[source]

The constructor takes no parameters.

SqliteAccountInfo

class b2sdk.v2.SqliteAccountInfo(file_name=None, last_upgrade_to_run=None, profile=None)[source]

Store account information in an sqlite3 database which is used to manage concurrent access to the data.

The update_done table tracks the schema updates that have been completed.

Parameters:

profile (Optional[str]) –

Implements all methods of AccountInfo interface.

Uses a SQLite database for persistence and access synchronization between multiple processes. Not suitable for usage over NFS.

Underlying database has the following schema:

digraph G {
            label = "generated by sadisplay v0.4.9";
            fontname = "Bitstream Vera Sans"
            fontsize = 8

            node [
                fontname = "Bitstream Vera Sans"
                fontsize = 8
                shape = "plaintext"
            ]

            edge [
                fontname = "Bitstream Vera Sans"
                fontsize = 8
            ]
    

        account [label=<
        <TABLE BGCOLOR="lightyellow" BORDER="0"
            CELLBORDER="0" CELLSPACING="0">
                <TR><TD COLSPAN="2" CELLPADDING="4"
                        ALIGN="CENTER" BGCOLOR="palegoldenrod"
                ><FONT FACE="Helvetica Bold" COLOR="black"
                >account</FONT></TD></TR><TR><TD ALIGN="LEFT" BORDER="0"
        ><FONT FACE="Bitstream Vera Sans">⚪ account_auth_token</FONT
        ></TD><TD ALIGN="LEFT"
        ><FONT FACE="Bitstream Vera Sans">TEXT</FONT
        ></TD></TR> <TR><TD ALIGN="LEFT" BORDER="0"
        ><FONT FACE="Bitstream Vera Sans">⚪ account_id</FONT
        ></TD><TD ALIGN="LEFT"
        ><FONT FACE="Bitstream Vera Sans">TEXT</FONT
        ></TD></TR> <TR><TD ALIGN="LEFT" BORDER="0"
        ><FONT FACE="Bitstream Vera Sans">⚪ account_id_or_app_key_id</FONT
        ></TD><TD ALIGN="LEFT"
        ><FONT FACE="Bitstream Vera Sans">TEXT</FONT
        ></TD></TR> <TR><TD ALIGN="LEFT" BORDER="0"
        ><FONT FACE="Bitstream Vera Sans">⚪ allowed</FONT
        ></TD><TD ALIGN="LEFT"
        ><FONT FACE="Bitstream Vera Sans">TEXT</FONT
        ></TD></TR> <TR><TD ALIGN="LEFT" BORDER="0"
        ><FONT FACE="Bitstream Vera Sans">⚪ api_url</FONT
        ></TD><TD ALIGN="LEFT"
        ><FONT FACE="Bitstream Vera Sans">TEXT</FONT
        ></TD></TR> <TR><TD ALIGN="LEFT" BORDER="0"
        ><FONT FACE="Bitstream Vera Sans">⚪ application_key</FONT
        ></TD><TD ALIGN="LEFT"
        ><FONT FACE="Bitstream Vera Sans">TEXT</FONT
        ></TD></TR> <TR><TD ALIGN="LEFT" BORDER="0"
        ><FONT FACE="Bitstream Vera Sans">⚪ download_url</FONT
        ></TD><TD ALIGN="LEFT"
        ><FONT FACE="Bitstream Vera Sans">TEXT</FONT
        ></TD></TR> <TR><TD ALIGN="LEFT" BORDER="0"
        ><FONT FACE="Bitstream Vera Sans">⚪ minimum_part_size</FONT
        ></TD><TD ALIGN="LEFT"
        ><FONT FACE="Bitstream Vera Sans">INTEGER</FONT
        ></TD></TR> <TR><TD ALIGN="LEFT" BORDER="0"
        ><FONT FACE="Bitstream Vera Sans">⚪ realm</FONT
        ></TD><TD ALIGN="LEFT"
        ><FONT FACE="Bitstream Vera Sans">TEXT</FONT
        ></TD></TR>
        </TABLE>
    >]
    

        bucket [label=<
        <TABLE BGCOLOR="lightyellow" BORDER="0"
            CELLBORDER="0" CELLSPACING="0">
                <TR><TD COLSPAN="2" CELLPADDING="4"
                        ALIGN="CENTER" BGCOLOR="palegoldenrod"
                ><FONT FACE="Helvetica Bold" COLOR="black"
                >bucket</FONT></TD></TR><TR><TD ALIGN="LEFT" BORDER="0"
        ><FONT FACE="Bitstream Vera Sans">⚪ bucket_id</FONT
        ></TD><TD ALIGN="LEFT"
        ><FONT FACE="Bitstream Vera Sans">TEXT</FONT
        ></TD></TR> <TR><TD ALIGN="LEFT" BORDER="0"
        ><FONT FACE="Bitstream Vera Sans">⚪ bucket_name</FONT
        ></TD><TD ALIGN="LEFT"
        ><FONT FACE="Bitstream Vera Sans">TEXT</FONT
        ></TD></TR>
        </TABLE>
    >]
    

        bucket_upload_url [label=<
        <TABLE BGCOLOR="lightyellow" BORDER="0"
            CELLBORDER="0" CELLSPACING="0">
                <TR><TD COLSPAN="2" CELLPADDING="4"
                        ALIGN="CENTER" BGCOLOR="palegoldenrod"
                ><FONT FACE="Helvetica Bold" COLOR="black"
                >bucket_upload_url</FONT></TD></TR><TR><TD ALIGN="LEFT" BORDER="0"
        ><FONT FACE="Bitstream Vera Sans">⚪ bucket_id</FONT
        ></TD><TD ALIGN="LEFT"
        ><FONT FACE="Bitstream Vera Sans">TEXT</FONT
        ></TD></TR> <TR><TD ALIGN="LEFT" BORDER="0"
        ><FONT FACE="Bitstream Vera Sans">⚪ upload_auth_token</FONT
        ></TD><TD ALIGN="LEFT"
        ><FONT FACE="Bitstream Vera Sans">TEXT</FONT
        ></TD></TR> <TR><TD ALIGN="LEFT" BORDER="0"
        ><FONT FACE="Bitstream Vera Sans">⚪ upload_url</FONT
        ></TD><TD ALIGN="LEFT"
        ><FONT FACE="Bitstream Vera Sans">TEXT</FONT
        ></TD></TR>
        </TABLE>
    >]
    

        update_done [label=<
        <TABLE BGCOLOR="lightyellow" BORDER="0"
            CELLBORDER="0" CELLSPACING="0">
                <TR><TD COLSPAN="2" CELLPADDING="4"
                        ALIGN="CENTER" BGCOLOR="palegoldenrod"
                ><FONT FACE="Helvetica Bold" COLOR="black"
                >update_done</FONT></TD></TR><TR><TD ALIGN="LEFT" BORDER="0"
        ><FONT FACE="Bitstream Vera Sans">⚪ update_number</FONT
        ></TD><TD ALIGN="LEFT"
        ><FONT FACE="Bitstream Vera Sans">INTEGER</FONT
        ></TD></TR>
        </TABLE>
    >]
    
	edge [
		arrowhead = empty
	]
	edge [
		arrowhead = ediamond
		arrowtail = open
	]
}

Hint

Usage of this class is appropriate for interactive applications installed on a user’s machine (i.e.: CLI and GUI applications).

Usage of this class might be appropriate for non-interactive applications installed on the user’s machine, such as backup applications. An alternative approach that should be considered is to store the AccountInfo data alongside the configuration of the rest of the application.

__init__(file_name=None, last_upgrade_to_run=None, profile=None)[source]

Initialize SqliteAccountInfo.

The exact algorithm used to determine the location of the database file is not API in any sense. If the location of the database file is required (for cleanup, etc), do not assume a specific resolution: instead, use self.filename to get the actual resolved location.

SqliteAccountInfo currently checks locations in the following order:

If profile arg is provided:

  • ~/.b2db-{profile}.sqlite if it already exists

  • $XDG_CONFIG_HOME/b2/db-<profile>.sqlite on XDG-compatible OSes (Linux, BSD)

  • ~/.b2db-{profile}.sqlite

Otherwise:

  • file_name, if truthy

  • B2_ACCOUNT_INFO env var’s value, if set

  • ~/.b2_account_info, if it already exists

  • $XDG_CONFIG_HOME/b2/account_info on XDG-compatible OSes (Linux, BSD)

  • ~/.b2_account_info, as default

If the directory $XDG_CONFIG_HOME/b2 does not exist (and is needed), it is created.

Parameters:
  • file_name (str) – The sqlite file to use; overrides the default.

  • last_upgrade_to_run (int) – For testing only, override the auto-update on the db.

  • profile (Optional[str]) –

classmethod get_user_account_info_path(file_name=None, profile=None)[source]
Parameters:
clear()[source]

Remove all info about accounts and buckets.

set_auth_data_with_schema_0_for_test(account_id, auth_token, api_url, download_url, minimum_part_size, application_key, realm)[source]

Set authentication data for tests.

Parameters:
  • account_id (str) – an account ID

  • auth_token (str) – an authentication token

  • api_url (str) – an API URL

  • download_url (str) – a download URL

  • minimum_part_size (int) – a minimum part size

  • application_key (str) – an application key

  • realm (str) – a realm to authorize account in

get_application_key()[source]

Return application_key or raises MissingAccountData exception.

Return type:

str

get_account_id()[source]

Return account ID or raises MissingAccountData exception.

Return type:

str

get_application_key_id()[source]

Return an application key ID. The ‘account_id_or_app_key_id’ column was not in the original schema, so it may be NULL.

Nota bene - this is the only place where we are not renaming account_id_or_app_key_id to application_key_id because it requires a column change.

application_key_id == account_id_or_app_key_id

Return type:

str

get_api_url()[source]

Return api_url or raises MissingAccountData exception.

Return type:

str

get_account_auth_token()[source]

Return account_auth_token or raises MissingAccountData exception.

Return type:

str

get_download_url()[source]

Return download_url or raises MissingAccountData exception.

Return type:

str

get_realm()[source]

Return realm or raises MissingAccountData exception.

Return type:

str

Return the recommended number of bytes in a part of a large file.

Returns:

number of bytes

Return type:

int

get_absolute_minimum_part_size()[source]

Return the absolute minimum number of bytes in a part of a large file.

Returns:

number of bytes

Return type:

int

get_allowed()[source]

Return ‘allowed’ dictionary info. Example:

{
    "bucketId": null,
    "bucketName": null,
    "capabilities": [
        "listKeys",
        "writeKeys"
    ],
    "namePrefix": null
}

The ‘allowed’ column was not in the original schema, so it may be NULL.

Return type:

dict

get_s3_api_url()[source]

Return s3_api_url or raises MissingAccountData exception.

Return type:

str

refresh_entire_bucket_name_cache(name_id_iterable)[source]

Remove all previous name-to-id mappings and stores new ones.

Parameters:

name_id_iterable (iterable) – an iterable of tuples of the form (name, id)

save_bucket(bucket)[source]

Remember the ID for the given bucket name.

Parameters:

bucket (b2sdk.v2.Bucket) – a Bucket object

remove_bucket_name(bucket_name)[source]

Remove one entry from the bucket name cache.

Parameters:

bucket_name (str) – a bucket name

get_bucket_id_or_none_from_bucket_name(bucket_name)[source]

Look up the bucket ID for the given bucket name.

Parameters:

bucket_name (str) – a bucket name

Return bucket ID or None:

Return type:

str, None

get_bucket_name_or_none_from_bucket_id(bucket_id)[source]

Look up the bucket name for the given bucket id.

Parameters:

bucket_id (str) –

Return type:

Optional[str]

list_bucket_names_ids()[source]

List buckets in the cache.

Return type:

list[tuple[str, str]]

Returns:

list of tuples (bucket_name, bucket_id)

BUCKET_UPLOAD_POOL_CLASS

alias of UploadUrlPool

DEFAULT_ALLOWED = {'bucketId': None, 'bucketName': None, 'capabilities': ['listKeys', 'writeKeys', 'deleteKeys', 'listBuckets', 'listAllBucketNames', 'readBuckets', 'writeBuckets', 'deleteBuckets', 'readBucketEncryption', 'writeBucketEncryption', 'readBucketRetentions', 'writeBucketRetentions', 'readFileRetentions', 'writeFileRetentions', 'readFileLegalHolds', 'writeFileLegalHolds', 'readBucketReplications', 'writeBucketReplications', 'bypassGovernance', 'listFiles', 'readFiles', 'shareFiles', 'writeFiles', 'deleteFiles', 'readBucketNotifications', 'writeBucketNotifications'], 'namePrefix': None}
LARGE_FILE_UPLOAD_POOL_CLASS

alias of UploadUrlPool

classmethod all_capabilities()

Return a list of all possible capabilities.

Return type:

list

classmethod allowed_is_valid(allowed)

Make sure that all of the required fields are present, and that bucketId is set if bucketName is.

If the bucketId is for a bucket that no longer exists, or the capabilities do not allow for listBuckets, then we will not have a bucketName.

Parameters:

allowed (dict) – the structure to use for old account info that was saved without ‘allowed’

Return type:

bool

clear_bucket_upload_data(bucket_id)

Remove all upload URLs for the given bucket.

Parameters:

bucket_id (str) – a bucket ID

clear_large_file_upload_urls(file_id)

Clear the pool of URLs for a given file ID.

Parameters:

file_id (str) – a file ID

is_master_key()
Return type:

bool

is_same_account(account_id, realm)

Check whether cached account is the same as the one provided.

Parameters:
  • account_id (str) – account ID

  • realm (str) – authorization realm

  • account_id

  • realm

Return type:

bool

is_same_key(application_key_id, realm)

Check whether cached application key is the same as the one provided.

Parameters:
  • application_key_id (str) – application key ID

  • realm (str) – authorization realm

Return type:

bool

put_bucket_upload_url(bucket_id, upload_url, upload_auth_token)

Add an (upload_url, upload_auth_token) pair to the pool available for the bucket.

Parameters:
  • bucket_id (str) – a bucket ID

  • upload_url (str) – an upload URL

  • upload_auth_token (str) – an upload authentication token

Return type:

tuple

put_large_file_upload_url(file_id, upload_url, upload_auth_token)

Put a large file upload URL into a pool.

Parameters:
  • file_id (str) – a file ID

  • upload_url (str) – an upload URL

  • upload_auth_token (str) – an upload authentication token

set_auth_data(account_id, auth_token, api_url, download_url, recommended_part_size, absolute_minimum_part_size, application_key, realm, s3_api_url, allowed, application_key_id)

Check permission correctness and stores the results of b2_authorize_account.

The allowed structure is the one returned by b2_authorize_account, e.g.

{
  "absoluteMinimumPartSize": 5000000,
  "accountId": "YOUR_ACCOUNT_ID",
  "allowed": {
    "bucketId": "BUCKET_ID",
    "bucketName": "BUCKET_NAME",
    "capabilities": [
      "listBuckets",
      "listFiles",
      "readFiles",
      "shareFiles",
      "writeFiles",
      "deleteFiles"
    ],
    "namePrefix": null
  },
  "apiUrl": "https://apiNNN.backblazeb2.com",
  "authorizationToken": "4_0022623512fc8f80000000001_0186e431_d18d02_acct_tH7VW03boebOXayIc43-sxptpfA=",
  "downloadUrl": "https://f002.backblazeb2.com",
  "recommendedPartSize": 100000000,
  "s3ApiUrl": "https://s3.us-west-NNN.backblazeb2.com"
}

For keys with bucket restrictions, the name of the bucket is looked up and stored as well. The console_tool does everything by bucket name, so it’s convenient to have the restricted bucket name handy.

Parameters:
  • account_id (str) – user account ID

  • auth_token (str) – user authentication token

  • api_url (str) – an API URL

  • download_url (str) – path download URL

  • recommended_part_size (int) – recommended size of a file part

  • absolute_minimum_part_size (int) – minimum size of a file part

  • application_key (str) – application key

  • realm (str) – a realm to authorize account in

  • allowed (dict) – the structure to use for old account info that was saved without ‘allowed’

  • application_key_id (str) – application key ID

  • s3_api_url (str) – S3-compatible API URL

Changed in version 0.1.5: account_id_or_app_key_id renamed to application_key_id

take_bucket_upload_url(bucket_id)

Return a pair (upload_url, upload_auth_token) that has been removed from the pool for this bucket, or (None, None) if there are no more left.

Parameters:

bucket_id (str) – a bucket ID

Return type:

tuple

take_large_file_upload_url(file_id)

Take the chosen large file upload URL from the pool.

Parameters:

file_id (str) – a file ID

Implementing your own

When building a server-side application or a web service, you might want to implement your own AccountInfo class backed by a database. In such case, you should inherit from b2sdk.v2.UrlPoolAccountInfo, which has groundwork for url pool functionality). If you cannot use it, inherit directly from b2sdk.v2.AbstractAccountInfo.

>>> from b2sdk.v2 import UrlPoolAccountInfo
>>> class MyAccountInfo(UrlPoolAccountInfo):
        ...

b2sdk.v2.AbstractAccountInfo describes the interface, while b2sdk.v2.UrlPoolAccountInfo and b2sdk.v2.UploadUrlPool implement a part of the interface for in-memory upload token management.

AccountInfo interface

class b2sdk.v2.AbstractAccountInfo[source]
list_bucket_names_ids()[source]

List buckets in the cache.

Returns:

list of tuples (bucket_name, bucket_id)

DEFAULT_ALLOWED = {'bucketId': None, 'bucketName': None, 'capabilities': ['listKeys', 'writeKeys', 'deleteKeys', 'listBuckets', 'listAllBucketNames', 'readBuckets', 'writeBuckets', 'deleteBuckets', 'readBucketEncryption', 'writeBucketEncryption', 'readBucketRetentions', 'writeBucketRetentions', 'readFileRetentions', 'writeFileRetentions', 'readFileLegalHolds', 'writeFileLegalHolds', 'readBucketReplications', 'writeBucketReplications', 'bypassGovernance', 'listFiles', 'readFiles', 'shareFiles', 'writeFiles', 'deleteFiles', 'readBucketNotifications', 'writeBucketNotifications'], 'namePrefix': None}
_abc_impl = <_abc._abc_data object>
abstract _set_auth_data(account_id, auth_token, api_url, download_url, recommended_part_size, absolute_minimum_part_size, application_key, realm, s3_api_url, allowed, application_key_id)[source]

Actually store the auth data. Can assume that ‘allowed’ is present and valid.

All of the information returned by b2_authorize_account is saved, because all of it is needed at some point.

classmethod all_capabilities()[source]

Return a list of all possible capabilities.

Return type:

list

classmethod allowed_is_valid(allowed)[source]

Make sure that all of the required fields are present, and that bucketId is set if bucketName is.

If the bucketId is for a bucket that no longer exists, or the capabilities do not allow for listBuckets, then we will not have a bucketName.

Parameters:

allowed (dict) – the structure to use for old account info that was saved without ‘allowed’

Return type:

bool

abstract clear()[source]

Remove all stored information.

abstract clear_bucket_upload_data(bucket_id)[source]

Remove all upload URLs for the given bucket.

Parameters:

bucket_id (str) – a bucket ID

abstract clear_large_file_upload_urls(file_id)[source]

Clear the pool of URLs for a given file ID.

Parameters:

file_id (str) – a file ID

abstract get_absolute_minimum_part_size()[source]

Return the absolute minimum number of bytes in a part of a large file.

Returns:

number of bytes

Return type:

int

abstract get_account_auth_token()[source]

Return account_auth_token or raises MissingAccountData exception.

Return type:

str

abstract get_account_id()[source]

Return account ID or raises MissingAccountData exception.

Return type:

str

abstract get_allowed()[source]

An ‘allowed’ dict, as returned by b2_authorize_account. Never None; for account info that was saved before ‘allowed’ existed, returns DEFAULT_ALLOWED.

Return type:

dict

abstract get_api_url()[source]

Return api_url or raises MissingAccountData exception.

Return type:

str

abstract get_application_key()[source]

Return application_key or raises MissingAccountData exception.

Return type:

str

abstract get_application_key_id()[source]

Return the application key ID used to authenticate.

Return type:

str

abstract get_bucket_id_or_none_from_bucket_name(bucket_name)[source]

Look up the bucket ID for the given bucket name.

Parameters:

bucket_name (str) – a bucket name

Return bucket ID or None:

Return type:

str, None

abstract get_bucket_name_or_none_from_bucket_id(bucket_id)[source]

Look up the bucket name for the given bucket id.

Parameters:

bucket_id (str) –

Return type:

Optional[str]

abstract get_download_url()[source]

Return download_url or raises MissingAccountData exception.

Return type:

str

abstract get_realm()[source]

Return realm or raises MissingAccountData exception.

Return type:

str

Return the recommended number of bytes in a part of a large file.

Returns:

number of bytes

Return type:

int

abstract get_s3_api_url()[source]

Return s3_api_url or raises MissingAccountData exception.

Return type:

str

is_master_key()[source]
Return type:

bool

is_same_account(account_id, realm)[source]

Check whether cached account is the same as the one provided.

Parameters:
  • account_id (str) – account ID

  • realm (str) – authorization realm

  • account_id

  • realm

Return type:

bool

is_same_key(application_key_id, realm)[source]

Check whether cached application key is the same as the one provided.

Parameters:
  • application_key_id (str) – application key ID

  • realm (str) – authorization realm

Return type:

bool

abstract put_bucket_upload_url(bucket_id, upload_url, upload_auth_token)[source]

Add an (upload_url, upload_auth_token) pair to the pool available for the bucket.

Parameters:
  • bucket_id (str) – a bucket ID

  • upload_url (str) – an upload URL

  • upload_auth_token (str) – an upload authentication token

Return type:

tuple

abstract put_large_file_upload_url(file_id, upload_url, upload_auth_token)[source]

Put a large file upload URL into a pool.

Parameters:
  • file_id (str) – a file ID

  • upload_url (str) – an upload URL

  • upload_auth_token (str) – an upload authentication token

abstract refresh_entire_bucket_name_cache(name_id_iterable)[source]

Remove all previous name-to-id mappings and stores new ones.

Parameters:

name_id_iterable (iterable) – an iterable of tuples of the form (name, id)

abstract remove_bucket_name(bucket_name)[source]

Remove one entry from the bucket name cache.

Parameters:

bucket_name (str) – a bucket name

abstract save_bucket(bucket)[source]

Remember the ID for the given bucket name.

Parameters:

bucket (b2sdk.v2.Bucket) – a Bucket object

set_auth_data(account_id, auth_token, api_url, download_url, recommended_part_size, absolute_minimum_part_size, application_key, realm, s3_api_url, allowed, application_key_id)[source]

Check permission correctness and stores the results of b2_authorize_account.

The allowed structure is the one returned by b2_authorize_account, e.g.

{
  "absoluteMinimumPartSize": 5000000,
  "accountId": "YOUR_ACCOUNT_ID",
  "allowed": {
    "bucketId": "BUCKET_ID",
    "bucketName": "BUCKET_NAME",
    "capabilities": [
      "listBuckets",
      "listFiles",
      "readFiles",
      "shareFiles",
      "writeFiles",
      "deleteFiles"
    ],
    "namePrefix": null
  },
  "apiUrl": "https://apiNNN.backblazeb2.com",
  "authorizationToken": "4_0022623512fc8f80000000001_0186e431_d18d02_acct_tH7VW03boebOXayIc43-sxptpfA=",
  "downloadUrl": "https://f002.backblazeb2.com",
  "recommendedPartSize": 100000000,
  "s3ApiUrl": "https://s3.us-west-NNN.backblazeb2.com"
}

For keys with bucket restrictions, the name of the bucket is looked up and stored as well. The console_tool does everything by bucket name, so it’s convenient to have the restricted bucket name handy.

Parameters:
  • account_id (str) – user account ID

  • auth_token (str) – user authentication token

  • api_url (str) – an API URL

  • download_url (str) – path download URL

  • recommended_part_size (int) – recommended size of a file part

  • absolute_minimum_part_size (int) – minimum size of a file part

  • application_key (str) – application key

  • realm (str) – a realm to authorize account in

  • allowed (dict) – the structure to use for old account info that was saved without ‘allowed’

  • application_key_id (str) – application key ID

  • s3_api_url (str) – S3-compatible API URL

Changed in version 0.1.5: account_id_or_app_key_id renamed to application_key_id

abstract take_bucket_upload_url(bucket_id)[source]

Return a pair (upload_url, upload_auth_token) that has been removed from the pool for this bucket, or (None, None) if there are no more left.

Parameters:

bucket_id (str) – a bucket ID

Return type:

tuple

abstract take_large_file_upload_url(file_id)[source]

Take the chosen large file upload URL from the pool.

Parameters:

file_id (str) – a file ID

AccountInfo helper classes

class b2sdk.v2.UrlPoolAccountInfo[source]

Implement part of AbstractAccountInfo for upload URL pool management with a simple, key-value storage, such as b2sdk.v2.UploadUrlPool.

Caution

This class is not part of the public interface. To find out how to safely use it, read this.

BUCKET_UPLOAD_POOL_CLASS

alias of UploadUrlPool

LARGE_FILE_UPLOAD_POOL_CLASS

alias of UploadUrlPool

abstract clear()[source]

Remove all stored information.

put_bucket_upload_url(bucket_id, upload_url, upload_auth_token)[source]

Add an (upload_url, upload_auth_token) pair to the pool available for the bucket.

Parameters:
  • bucket_id (str) – a bucket ID

  • upload_url (str) – an upload URL

  • upload_auth_token (str) – an upload authentication token

Return type:

tuple

clear_bucket_upload_data(bucket_id)[source]

Remove all upload URLs for the given bucket.

Parameters:

bucket_id (str) – a bucket ID

take_bucket_upload_url(bucket_id)[source]

Return a pair (upload_url, upload_auth_token) that has been removed from the pool for this bucket, or (None, None) if there are no more left.

Parameters:

bucket_id (str) – a bucket ID

Return type:

tuple

put_large_file_upload_url(file_id, upload_url, upload_auth_token)[source]

Put a large file upload URL into a pool.

Parameters:
  • file_id (str) – a file ID

  • upload_url (str) – an upload URL

  • upload_auth_token (str) – an upload authentication token

take_large_file_upload_url(file_id)[source]

Take the chosen large file upload URL from the pool.

Parameters:

file_id (str) – a file ID

clear_large_file_upload_urls(file_id)[source]

Clear the pool of URLs for a given file ID.

Parameters:

file_id (str) – a file ID

DEFAULT_ALLOWED = {'bucketId': None, 'bucketName': None, 'capabilities': ['listKeys', 'writeKeys', 'deleteKeys', 'listBuckets', 'listAllBucketNames', 'readBuckets', 'writeBuckets', 'deleteBuckets', 'readBucketEncryption', 'writeBucketEncryption', 'readBucketRetentions', 'writeBucketRetentions', 'readFileRetentions', 'writeFileRetentions', 'readFileLegalHolds', 'writeFileLegalHolds', 'readBucketReplications', 'writeBucketReplications', 'bypassGovernance', 'listFiles', 'readFiles', 'shareFiles', 'writeFiles', 'deleteFiles', 'readBucketNotifications', 'writeBucketNotifications'], 'namePrefix': None}
classmethod all_capabilities()

Return a list of all possible capabilities.

Return type:

list

classmethod allowed_is_valid(allowed)

Make sure that all of the required fields are present, and that bucketId is set if bucketName is.

If the bucketId is for a bucket that no longer exists, or the capabilities do not allow for listBuckets, then we will not have a bucketName.

Parameters:

allowed (dict) – the structure to use for old account info that was saved without ‘allowed’

Return type:

bool

abstract get_absolute_minimum_part_size()

Return the absolute minimum number of bytes in a part of a large file.

Returns:

number of bytes

Return type:

int

abstract get_account_auth_token()

Return account_auth_token or raises MissingAccountData exception.

Return type:

str

abstract get_account_id()

Return account ID or raises MissingAccountData exception.

Return type:

str

abstract get_allowed()

An ‘allowed’ dict, as returned by b2_authorize_account. Never None; for account info that was saved before ‘allowed’ existed, returns DEFAULT_ALLOWED.

Return type:

dict

abstract get_api_url()

Return api_url or raises MissingAccountData exception.

Return type:

str

abstract get_application_key()

Return application_key or raises MissingAccountData exception.

Return type:

str

abstract get_application_key_id()

Return the application key ID used to authenticate.

Return type:

str

abstract get_bucket_id_or_none_from_bucket_name(bucket_name)

Look up the bucket ID for the given bucket name.

Parameters:

bucket_name (str) – a bucket name

Return bucket ID or None:

Return type:

str, None

abstract get_bucket_name_or_none_from_bucket_id(bucket_id)

Look up the bucket name for the given bucket id.

Parameters:

bucket_id (str) –

Return type:

Optional[str]

abstract get_download_url()

Return download_url or raises MissingAccountData exception.

Return type:

str

abstract get_realm()

Return realm or raises MissingAccountData exception.

Return type:

str

Return the recommended number of bytes in a part of a large file.

Returns:

number of bytes

Return type:

int

abstract get_s3_api_url()

Return s3_api_url or raises MissingAccountData exception.

Return type:

str

is_master_key()
Return type:

bool

is_same_account(account_id, realm)

Check whether cached account is the same as the one provided.

Parameters:
  • account_id (str) – account ID

  • realm (str) – authorization realm

  • account_id

  • realm

Return type:

bool

is_same_key(application_key_id, realm)

Check whether cached application key is the same as the one provided.

Parameters:
  • application_key_id (str) – application key ID

  • realm (str) – authorization realm

Return type:

bool

abstract list_bucket_names_ids()

List buckets in the cache.

Return type:

list[tuple[str, str]]

Returns:

list of tuples (bucket_name, bucket_id)

abstract refresh_entire_bucket_name_cache(name_id_iterable)

Remove all previous name-to-id mappings and stores new ones.

Parameters:

name_id_iterable (iterable) – an iterable of tuples of the form (name, id)

abstract remove_bucket_name(bucket_name)

Remove one entry from the bucket name cache.

Parameters:

bucket_name (str) – a bucket name

abstract save_bucket(bucket)

Remember the ID for the given bucket name.

Parameters:

bucket (b2sdk.v2.Bucket) – a Bucket object

set_auth_data(account_id, auth_token, api_url, download_url, recommended_part_size, absolute_minimum_part_size, application_key, realm, s3_api_url, allowed, application_key_id)

Check permission correctness and stores the results of b2_authorize_account.

The allowed structure is the one returned by b2_authorize_account, e.g.

{
  "absoluteMinimumPartSize": 5000000,
  "accountId": "YOUR_ACCOUNT_ID",
  "allowed": {
    "bucketId": "BUCKET_ID",
    "bucketName": "BUCKET_NAME",
    "capabilities": [
      "listBuckets",
      "listFiles",
      "readFiles",
      "shareFiles",
      "writeFiles",
      "deleteFiles"
    ],
    "namePrefix": null
  },
  "apiUrl": "https://apiNNN.backblazeb2.com",
  "authorizationToken": "4_0022623512fc8f80000000001_0186e431_d18d02_acct_tH7VW03boebOXayIc43-sxptpfA=",
  "downloadUrl": "https://f002.backblazeb2.com",
  "recommendedPartSize": 100000000,
  "s3ApiUrl": "https://s3.us-west-NNN.backblazeb2.com"
}

For keys with bucket restrictions, the name of the bucket is looked up and stored as well. The console_tool does everything by bucket name, so it’s convenient to have the restricted bucket name handy.

Parameters:
  • account_id (str) – user account ID

  • auth_token (str) – user authentication token

  • api_url (str) – an API URL

  • download_url (str) – path download URL

  • recommended_part_size (int) – recommended size of a file part

  • absolute_minimum_part_size (int) – minimum size of a file part

  • application_key (str) – application key

  • realm (str) – a realm to authorize account in

  • allowed (dict) – the structure to use for old account info that was saved without ‘allowed’

  • application_key_id (str) – application key ID

  • s3_api_url (str) – S3-compatible API URL

Changed in version 0.1.5: account_id_or_app_key_id renamed to application_key_id

class b2sdk._internal.account_info.upload_url_pool.UploadUrlPool[source]

For each key (either a bucket id or large file id), hold a pool of (url, auth_token) pairs.

Caution

This class is not part of the public interface. To find out how to safely use it, read this.

put(key, url, auth_token)[source]

Add the url and auth token to the pool for the given key.

Parameters:
  • key (str) – bucket ID or large file ID

  • url (str) – bucket or file URL

  • auth_token (str) – authentication token

take(key)[source]

Return a (url, auth_token) if one is available, or (None, None) if not.

Parameters:

key (str) – bucket ID or large file ID

Return type:

tuple

clear_for_key(key)[source]

Remove an item from the pool by key.

Parameters:

key (str) – bucket ID or large file ID