b2sdk.api
– API client¶
-
class
b2sdk.api.
B2Api
(account_info=None, cache=None, raw_api=None, max_upload_workers=10)[source]¶ Bases:
object
Provides file-level access to B2 services.
While B2RawApi provides 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.
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)[source]¶ Initializes the API using the given account info.
- Parameters
account_info – an instance of
UrlPoolAccountInfo
, or any custom class derived fromAbstractAccountInfo
cache – an instance of the one of the following classes:
DummyCache
,InMemoryCache
,AuthInfoCache
, or any custom class derived fromAbstractCache
raw_api – an instance of one of the following classes:
B2RawApi
,RawSimulator
, or any custom class derived fromAbstractRawApi
max_upload_workers (int) – a number of upload threads, default is 10
Perform account authorization
Perform automatic account authorization, retrieving all account data from account info object passed during initialization
-
check_bucket_restrictions
(bucket_name)[source]¶ Checks to see if the allowed field from authorize-account has a bucket restriction.
If it does, does the bucket_name for a given api call match that. If not it raises a RestrictedBucket error.
- Parameters
bucket_name (str) – a bucket name
-
create_bucket
(name, bucket_type, bucket_info=None, cors_rules=None, lifecycle_rules=None)[source]¶ Create a bucket
- Parameters
name (str) – bucket name
bucket_type (str) – a bucket type, could be one of the following values: “allPublic”, “allPrivate”
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
- Returns
a Bucket object
- Return type
-
create_key
(capabilities, key_name, valid_duration_seconds=None, bucket_id=None, name_prefix=None)[source]¶ Create a new application key
-
delete_bucket
(bucket)[source]¶ Deletes the bucket remotely. For legacy reasons it returns whatever server sends in response, but API user should not rely on the response: if it doesn’t raise an exception, it means that the operation was a success
- Parameters
bucket (b2sdk.bucket.Bucket) – a Bucket object
-
delete_file_version
(file_id, file_name)[source]¶ Permanently and irrevocably delete one version of a file
- Parameters
file_id – a file ID
file_name (str) – a file name
- Type
file_id: str
-
delete_key
(application_key_id)[source]¶ Delete application key with a given ID
- Parameters
application_key_id – an application key ID
-
download_file_by_id
(file_id, download_dest, progress_listener=None, range_=None)[source]¶ Download a file with a given ID
- Parameters
file_id (str) – a file ID
download_dest (str) – a local file path
progress_listener – an instance of the one of the following classes:
PartProgressReporter
,TqdmProgressListener
,SimpleProgressListener
,DoNothingProgressListener
,ProgressListenerForTest
,SyncFileReporter
, or any sub class ofAbstractProgressListener
range (list) – a list of two integers, the first one is a start position, and the second oe is the end position in the file
- Returns
context manager that returns an object that supports iter_content()
-
get_bucket_by_id
(bucket_id)[source]¶ Return bucket object with a given ID
- Parameters
bucket_id (str) – a bucket ID
- Returns
a Bucket object
- Return type
-
get_bucket_by_name
(bucket_name)[source]¶ Returns the Bucket for the given bucket_name.
- Parameters
bucket_name (str) – The name of the bucket to return.
- Returns
a Bucket object
- Return type
- Raises
b2sdk.exception.NonExistentBucket – if the bucket does not exist in the account
-
get_download_url_for_file_name
(bucket_name, file_name)[source]¶ Returns a URL to download the given file by name.
-
get_download_url_for_fileid
(file_id)[source]¶ Returns a URL to download the given file by ID.
- Parameters
file_id (str) – a file ID
-
list_buckets
(bucket_name=None)[source]¶ Calls b2_list_buckets and returns a list of buckets.
When no bucket name is specified, returns all of the buckets in the account. When a bucket name 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_keys
(start_application_key_id=None)[source]¶ List application keys
- Parameters
start_application_key_id (str) – an application key ID to start from
-