B2 Api client¶
Provide file-level access to B2 services.
b2sdk.v1.B2RawApiprovides direct access to the B2 web APIs, this class handles several things that simplify the task of uploading and downloading files:
re-acquires authorization tokens when they expire
retrying uploads when an upload URL is busy
breaking large files into parts
emulating a directory structure (B2 buckets are flat)
Adds an object-oriented layer on top of the raw API, so that buckets and files returned are Python objects with accessor methods.
The class also keeps a cache of information needed to access the service, such as auth tokens and upload URLs.
__init__(account_info=None, cache=None, raw_api=None, max_upload_workers=10, max_copy_workers=10)¶
Initialize the API using the given account info.
cache – an instance of the one of the following classes:
AuthInfoCache, or any custom class derived from
AbstractCacheIt is used by B2Api to cache the mapping between bucket name and bucket ids. default is
raw_api – an instance of one of the following classes:
RawSimulator, or any custom class derived from
AbstractRawApiIt makes network-less unit testing simple by using
RawSimulator, in tests and
B2RawApiin production. default is
max_upload_workers (int) – a number of upload threads, default is 10
max_copy_workers (int) – a number of copy threads, default is 10
Perform automatic account authorization, retrieving all account data from account info object passed during initialization.
Perform account authorization.
create_bucket(name, bucket_type, bucket_info=None, cors_rules=None, lifecycle_rules=None)¶
Create a bucket.
name (str) – bucket name
bucket_type (str) – a bucket type, could be one of the following values:
bucket_info (dict) – additional bucket info to store with the bucket
cors_rules (dict) – bucket CORS rules to store with the bucket
lifecycle_rules (dict) – bucket lifecycle rules to store with the bucket
a Bucket object
- Return type
download_file_by_id(file_id, download_dest, progress_listener=None, range_=None)¶
Download a file with the given ID.
file_id (str) – a file ID
download_dest – an instance of the one of the following classes:
PreSeekedDownloadDest, or any sub class of
progress_listener – an instance of the one of the following classes:
SyncFileReporter, or any sub class of
range (list) – a list of two integers, the first one is a start position, and the second one is the end position in the file
context manager that returns an object that supports iter_content()
Return a bucket object with a given ID. Unlike
get_bucket_by_name, this method does not need to make any API calls.
Return the Bucket matching the given bucket_name.
Delete a chosen bucket.
b2_list_bucketsand return a list of buckets.
When no bucket name nor ID is specified, returns all of the buckets in the account. When a bucket name or ID is given, returns just that bucket. When authorized with an application key restricted to one bucket, you must specify the bucket name, or the request will be unauthorized.
list_parts(file_id, start_part_number=None, batch_size=None)¶
Generator that yields a
b2sdk.v1.Partfor each of the parts that have been uploaded.
Cancel a large file upload.
Permanently and irrevocably delete one version of a file.
Return a URL to download the given file by ID.
file_id (str) – a file ID
Return a URL to download the given file by name.
create_key(capabilities, key_name, valid_duration_seconds=None, bucket_id=None, name_prefix=None)¶
Create a new application key.
capabilities (list) – a list of capabilities
key_name (str) – a name of a key
Delete application key.
List application keys.
get_file_info(file_id: str) → Dict[str, Any]¶
Legacy interface which just returns whatever remote API returns.
Check to see if the allowed field from authorize-account has a bucket restriction.
If it does, checks if the bucket_name for a given api call matches that. If not, it raises a