B2 Api client¶
-
class
b2sdk.v1.
B2Api
[source]¶ -
SESSION_CLASS
¶ alias of
b2sdk.v1.session.B2Session
-
BUCKET_FACTORY_CLASS
¶ alias of
b2sdk.v1.bucket.BucketFactory
-
BUCKET_CLASS
¶ alias of
b2sdk.v1.bucket.Bucket
-
FILE_VERSION_FACTORY_CLASS
¶ alias of
b2sdk.v1.file_version.FileVersionInfoFactory
-
__init__
(account_info: Optional[b2sdk.v1.account_info.AbstractAccountInfo] = None, cache: Optional[b2sdk.v1.cache.AbstractCache] = None, raw_api: Optional[b2sdk.raw_api.B2RawHTTPApi] = None, max_upload_workers: int = 10, max_copy_workers: int = 10, api_config: Optional[b2sdk.api_config.B2HttpApiConfig] = None)[source]¶ Initialize the API using the given account info.
- Parameters
account_info – To learn more about Account Info objects, see here
SqliteAccountInfo
cache – It is used by B2Api to cache the mapping between bucket name and bucket ids. default is
DummyCache
max_upload_workers – a number of upload threads
max_copy_workers – a number of copy threads
raw_api –
api_config –
-
get_file_info
(file_id: str) → Dict[str, Any][source]¶ Gets info about file version.
- Parameters
file_id (str) – the id of the file.
-
get_bucket_by_id
(bucket_id)[source]¶ Return a bucket object with a given ID. Unlike
get_bucket_by_name
, this method does not need to make any API calls.- Parameters
bucket_id (str) – a bucket ID
- Returns
a Bucket object
- Return type
-
check_bucket_restrictions
(bucket_name)[source]¶ 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
b2sdk.v1.exception.RestrictedBucket
error.- Parameters
bucket_name (str) – a bucket name
- Raises
b2sdk.v1.exception.RestrictedBucket – if the account is not allowed to use this bucket
-
cancel_large_file
(file_id: str) → b2sdk.v1.file_version.FileVersionInfo[source]¶ Cancel a large file upload.
-
download_file_by_id
(file_id: str, download_dest: AbstractDownloadDestination, progress_listener: Optional[v2.AbstractProgressListener] = None, range_: Optional[Tuple[int, int]] = None, encryption: Optional[v2.EncryptionSetting] = None) → dict[source]¶ -
download_file_by_id
(file_id: str, progress_listener: Optional[v2.AbstractProgressListener] = None, range_: Optional[Tuple[int, int]] = None, encryption: Optional[v2.EncryptionSetting] = None) → v2.DownloadedFile Download a file with the given ID.
- Parameters
file_id – a file ID
download_dest – an instance of the one of the following classes:
DownloadDestLocalFile
,DownloadDestBytes
,PreSeekedDownloadDest
, or any sub class ofAbstractDownloadDestination
progress_listener – an instance of the one of the following classes:
PartProgressReporter
,TqdmProgressListener
,SimpleProgressListener
,DoNothingProgressListener
,ProgressListenerForTest
,SyncFileReporter
, or any sub class ofAbstractProgressListener
range – a list of two integers, the first one is a start position, and the second one is the end position in the file
encryption – encryption settings (
None
if unknown)
-
list_keys
(start_application_key_id=None) → dict[source]¶ List application keys. Perform a single request and return at most
self.DEFAULT_LIST_KEY_COUNT
keys, as well as the value to supply to the next call asstart_application_key_id
, if not all keys were retrieved.- Parameters
start_application_key_id – an application key ID to start from or
None
to start from the beginning
-
DEFAULT_LIST_KEY_COUNT
= 1000¶
-
DOWNLOAD_VERSION_FACTORY_CLASS
¶ alias of
b2sdk.file_version.DownloadVersionFactory
-
property
account_info
¶
Perform account authorization.
- Parameters
realm (str) – a realm to authorize account in (usually just “production”)
application_key_id (str) – application key ID
application_key (str) – user’s application key
Perform automatic account authorization, retrieving all account data from account info object passed during initialization.
-
property
cache
¶
-
check_bucket_id_restrictions
(bucket_id: str)[source]¶ Check to see if the allowed field from authorize-account has a bucket restriction.
If it does, checks if the bucket_id for a given api call matches that. If not, it raises a
b2sdk.v1.exception.RestrictedBucket
error.- Raises
b2sdk.v1.exception.RestrictedBucket – if the account is not allowed to use this bucket
-
check_bucket_name_restrictions
(bucket_name: str)[source]¶ 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
b2sdk.v1.exception.RestrictedBucket
error.- Raises
b2sdk.v1.exception.RestrictedBucket – if the account is not allowed to use this bucket
-
create_bucket
(name, bucket_type, bucket_info=None, cors_rules=None, lifecycle_rules=None, default_server_side_encryption: Optional[b2sdk.encryption.setting.EncryptionSetting] = None, is_file_lock_enabled: Optional[bool] = 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
default_server_side_encryption (b2sdk.v1.EncryptionSetting) – default server side encryption settings (
None
if unknown)is_file_lock_enabled (bool) – boolean value specifies whether bucket is File Lock-enabled
- Returns
a Bucket object
- Return type
-
create_key
(capabilities: List[str], key_name: str, valid_duration_seconds: Optional[int] = None, bucket_id: Optional[str] = None, name_prefix: Optional[str] = None)[source]¶ Create a new application key.
- Parameters
capabilities – a list of capabilities
key_name – a name of a key
valid_duration_seconds – key auto-expire time after it is created, in seconds, or
None
to not expirebucket_id – a bucket ID to restrict the key to, or
None
to not restrictname_prefix – a remote filename prefix to restrict the key to or
None
to not restrict
-
delete_bucket
(bucket)[source]¶ Delete a chosen bucket.
- Parameters
bucket (b2sdk.v1.Bucket) – a bucket to delete
- Return type
-
delete_file_version
(file_id: str, file_name: str) → b2sdk.file_version.FileIdAndName[source]¶ Permanently and irrevocably delete one version of a file.
-
delete_key_by_id
(application_key_id: str)[source]¶ Delete application key.
- Parameters
application_key_id – an application key ID
-
get_bucket_by_name
(bucket_name)[source]¶ Return the Bucket matching the given bucket_name.
- Parameters
bucket_name (str) – the name of the bucket to return
- Returns
a Bucket object
- Return type
- Raises
b2sdk.v1.exception.NonExistentBucket – if the bucket does not exist in the account
-
get_download_url_for_file_name
(bucket_name, file_name)[source]¶ Return a URL to download the given file by name.
-
get_download_url_for_fileid
(file_id)[source]¶ Return a URL to download the given file by ID.
- Parameters
file_id (str) – a file ID
-
list_buckets
(bucket_name=None, bucket_id=None)[source]¶ Call
b2_list_buckets
and 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 bucket id, or the request will be unauthorized.
- Parameters
- Return type
-
list_parts
(file_id, start_part_number=None, batch_size=None)[source]¶ Generator that yields a
b2sdk.v1.Part
for each of the parts that have been uploaded.
-
property
raw_api
¶ Warning
B2RawHTTPApi
attribute is deprecated.B2Session
expose allB2RawHTTPApi
methods now.
-
update_file_legal_hold
(file_id: str, file_name: str, legal_hold: b2sdk.file_lock.LegalHold)[source]¶
-
update_file_retention
(file_id: str, file_name: str, file_retention: b2sdk.file_lock.FileRetentionSetting, bypass_governance: bool = False)[source]¶
-
delete_key
(application_key_id)[source]¶ Delete application key.
- Parameters
application_key – an application key
-
-
class
b2sdk.v1.
B2HttpApiConfig
[source]¶ -
DEFAULT_RAW_API_CLASS
¶ alias of
b2sdk.raw_api.B2RawHTTPApi
-
__init__
(http_session_factory: Callable[[], requests.sessions.Session] = <class 'requests.sessions.Session'>, install_clock_skew_hook: bool = True, user_agent_append: Optional[str] = None, _raw_api_class: Optional[Type[b2sdk.raw_api.AbstractRawApi]] = None)[source]¶ A structure with params to be passed to low level API.
- Parameters
http_session_factory – a callable that returns a requests.Session object (or a compatible one)
install_clock_skew_hook – if True, install a clock skew hook
user_agent_append – if provided, the string will be appended to the User-Agent
_raw_api_class – AbstractRawApi-compliant class
-