Tutorial

AccountInfo

AccountInfo object holds information about access keys, tokens, upload urls, as well as a bucket id-name map.

It is the first object that you need to create to use b2sdk. Using AccountInfo, we’ll be able to create a B2Api object to manage a B2 account.

In the tutorial we will use b2sdk.v2.InMemoryAccountInfo:

>>> from b2sdk.v2 import InMemoryAccountInfo
>>> info = InMemoryAccountInfo()  # store credentials, tokens and cache in memory

With the info object in hand, we can now proceed to create a B2Api object.

Note

AccountInfo section provides guidance for choosing the correct AccountInfo class for your application.

Account authorization

>>> from b2sdk.v2 import B2Api
>>> b2_api = B2Api(info)
>>> application_key_id = '4a5b6c7d8e9f'
>>> application_key = '001b8e23c26ff6efb941e237deb182b9599a84bef7'
>>> b2_api.authorize_account("production", application_key_id, application_key)

Tip

Get credentials from B2 website

To find out more about account authorization, see b2sdk.v2.B2Api.authorize_account()

B2Api

B2Api allows for account-level operations on a B2 account.

Typical B2Api operations

authorize_account

Perform account authorization.

create_bucket

Create a bucket.

delete_bucket

Delete a chosen bucket.

list_buckets

Call b2_list_buckets and return a list of buckets.

get_bucket_by_name

Return the Bucket matching the given bucket_name.

get_bucket_by_id

Return the Bucket matching the given bucket_id.

create_key

Create a new application key.

list_keys

List application keys.

delete_key

Delete application key.

download_file_by_id

Download a file with the given ID.

list_parts

Generator that yields a b2sdk.v2.Part for each of the parts that have been uploaded.

cancel_large_file

Cancel a large file upload.

>>> b2_api = B2Api(info)

to find out more, see b2sdk.v2.B2Api.

The most practical operation on B2Api object is b2sdk.v2.B2Api.get_bucket_by_name().

Bucket allows for operations such as listing a remote bucket or transferring files.

Bucket

Initializing a Bucket

Retrieve an existing Bucket

To get a Bucket object for an existing B2 Bucket:

>>> b2_api.get_bucket_by_name("example-mybucket-b2-1",)
Bucket<346501784642eb3e60980d10,example-mybucket-b2-1,allPublic>

Create a new Bucket

To create a bucket:

>>> bucket_name = 'example-mybucket-b2-1'
>>> bucket_type = 'allPublic'  # or 'allPrivate'

>>> b2_api.create_bucket(bucket_name, bucket_type)
Bucket<346501784642eb3e60980d10,example-mybucket-b2-1,allPublic>

You can optionally store bucket info, CORS rules and lifecycle rules with the bucket. See b2sdk.v2.B2Api.create_bucket() for more details.

Note

Bucket name must be unique in B2 (across all accounts!). Your application should be able to cope with a bucket name collision with another B2 user.

Typical Bucket operations

download_file_by_name

Download a file by name.

upload_local_file

Upload a file on local disk to a B2 file.

upload_bytes

Upload bytes in memory to a B2 file.

ls

Pretend that folders exist and yields the information about the files in a folder.

hide_file

Hide a file.

delete_file_version

Delete a file version.

get_download_authorization

Return an authorization token that is valid only for downloading files from the given bucket.

get_download_url

Get file download URL.

update

Update various bucket parameters.

set_type

Update bucket type.

set_info

Update bucket info.

To find out more, see b2sdk.v2.Bucket.

Summary

You now know how to use AccountInfo, B2Api and Bucket objects.

To see examples of some of the methods presented above, visit the quick start guide section.