AccountInfo¶
AccountInfo stores basic information about the account, such as Application Key ID and Application Key,
in order to let b2sdk.v1.B2Api
perform authenticated requests.
There are two usable implementations provided by b2sdk:
b2sdk.v1.InMemoryAccountInfo
- a basic implementation with no persistence
b2sdk.v1.SqliteAccountInfo
- for console and GUI applications
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.v1.
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 thebucket_name:bucket_id
mapping; in case ofInMemoryAccountInfo
the cache will be flushed between executions of the program.
SqliteAccountInfo¶
-
class
b2sdk.v1.
SqliteAccountInfo
[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.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:
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.
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.v1.UrlPoolAccountInfo
, which has groundwork for url pool functionality). If you cannot use it, inherit directly from b2sdk.v1.AbstractAccountInfo
.
>>> from b2sdk.v1 import UrlPoolAccountInfo
>>> class MyAccountInfo(UrlPoolAccountInfo):
...
b2sdk.v1.AbstractAccountInfo
describes the interface, while b2sdk.v1.UrlPoolAccountInfo
and b2sdk.v1.UploadUrlPool
implement a part of the interface for in-memory upload token management.
AccountInfo interface¶
-
class
b2sdk.v1.
AbstractAccountInfo
[source]¶ Abstract class for a holder for all account-related information that needs to be kept between API calls and between invocations of the program.
This includes: account ID, application key ID, application key, auth tokens, API URL, download URL, and uploads URLs.
This class must be THREAD SAFE because it may be used by multiple threads running in the same Python process. It also needs to be safe against multiple processes running at the same time.
-
REALM_URLS
= {'dev': 'http://api.backblazeb2.xyz:8180', 'production': 'https://api.backblazeb2.com', 'staging': 'https://api.backblaze.net'}¶
-
DEFAULT_ALLOWED
= {'bucketId': None, 'bucketName': None, 'capabilities': ['listKeys', 'writeKeys', 'deleteKeys', 'listBuckets', 'writeBuckets', 'deleteBuckets', 'listFiles', 'readFiles', 'shareFiles', 'writeFiles', 'deleteFiles'], 'namePrefix': None}¶
-
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.v1.Bucket) – a Bucket object
-
abstract
get_bucket_id_or_none_from_bucket_name
(bucket_name)[source]¶ Look up the bucket ID for the given bucket name.
-
abstract
clear_bucket_upload_data
(bucket_id)[source]¶ Remove all upload URLs for the given bucket.
- Parameters
bucket_id (str) – a bucket ID
-
abstract
get_account_id
()[source]¶ Return account ID or raises MissingAccountData exception.
- Return type
-
abstract
get_application_key_id
()[source]¶ Return the application key ID used to authenticate.
- Return type
-
abstract
get_account_auth_token
()[source]¶ Return account_auth_token or raises MissingAccountData exception.
- Return type
-
abstract
get_application_key
()[source]¶ Return application_key or raises MissingAccountData exception.
- Return type
-
abstract
get_download_url
()[source]¶ Return download_url or raises MissingAccountData exception.
- Return type
-
abstract
get_minimum_part_size
()[source]¶ Return the minimum number of bytes in a part of a large file.
- Returns
number of bytes
- Return type
-
abstract
get_allowed
()[source]¶ An ‘allowed’ dict, as returned by
b2_authorize_account
. NeverNone
; for account info that was saved before ‘allowed’ existed, returnsDEFAULT_ALLOWED
.- Return type
-
set_auth_data
(account_id, auth_token, api_url, download_url, minimum_part_size, application_key, realm, allowed=None, application_key_id=None)[source]¶ Check permission correctness and stores the results of
b2_authorize_account
.The allowed structure is the one returned by
b2_authorize_account
with an addition of a bucketName field. 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
minimum_part_size (int) – minimum size of the 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
Changed in version 0.1.5: account_id_or_app_key_id renamed to application_key_id
-
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.
-
abstract
_set_auth_data
(account_id, auth_token, api_url, download_url, minimum_part_size, application_key, realm, 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.
-
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.
-
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.
-
abstract
put_large_file_upload_url
(file_id, upload_url, upload_auth_token)[source]¶ Put a large file upload URL into a pool.
-
AccountInfo helper classes¶
-
class
b2sdk.v1.
UrlPoolAccountInfo
[source]¶ Implement part of
AbstractAccountInfo
for upload URL pool management with a simple, key-value storage, such asb2sdk.v1.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
¶ A url pool class to use for small files.
-
LARGE_FILE_UPLOAD_POOL_CLASS
¶ A url pool class to use for large files.
-
-
class
b2sdk.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.