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.
B2Api
B2Api allows for account-level operations on a B2 account.
Typical B2Api operations
Perform account authorization. |
|
Create a bucket. |
|
Delete a chosen bucket. |
|
Call |
|
Return the Bucket matching the given bucket_name. |
|
Return the Bucket matching the given bucket_id. |
|
Create a new application key. |
|
List application keys. |
|
Delete application key. |
|
Download a file with the given ID. |
|
Generator that yields a |
|
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 a file by name. |
|
Upload a file on local disk to a B2 file. |
|
Upload bytes in memory to a B2 file. |
|
Pretend that folders exist and yields the information about the files in a folder. |
|
Hide a file. |
|
Delete a file version. |
|
Return an authorization token that is valid only for downloading files from the given bucket. |
|
Get file download URL. |
|
Update various bucket parameters. |
|
Update bucket type. |
|
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.