B2 Bucket

class b2sdk.v2.Bucket[source]
get_fresh_state() Bucket[source]

Fetch all the information about this bucket and return a new bucket object. This method does NOT change the object it is called on.

DEFAULT_CONTENT_TYPE = 'b2/x-auto'
__init__(api, id_, name=None, type_=None, bucket_info=None, cors_rules=None, lifecycle_rules=None, revision=None, bucket_dict=None, options_set=None, default_server_side_encryption: ~b2sdk.encryption.setting.EncryptionSetting = <EncryptionSetting(EncryptionMode.UNKNOWN, None, None)>, default_retention: ~b2sdk.file_lock.BucketRetentionSetting = BucketRetentionSetting('unknown', None), is_file_lock_enabled: ~typing.Optional[bool] = None, replication: ~typing.Optional[~b2sdk.replication.setting.ReplicationConfiguration] = None)[source]
Parameters
  • api (b2sdk.v2.B2Api) – an API object

  • id (str) – a bucket id

  • name (str) – a bucket name

  • type (str) – a bucket type

  • bucket_info (dict) – an info to store with a bucket

  • cors_rules (dict) – CORS rules to store with a bucket

  • lifecycle_rules (list) – lifecycle rules of the bucket

  • revision (int) – a bucket revision number

  • bucket_dict (dict) – a dictionary which contains bucket parameters

  • options_set (set) – set of bucket options strings

  • default_server_side_encryption (b2sdk.v2.EncryptionSetting) – default server side encryption settings

  • default_retention (b2sdk.v2.BucketRetentionSetting) – default retention setting

  • is_file_lock_enabled (bool) – whether file locking is enabled or not

  • replication (b2sdk.v2.ReplicationConfiguration) – replication rules for the bucket

as_dict()[source]

Return bucket representation as a dictionary.

Return type

dict

cancel_large_file(file_id)[source]

Cancel a large file transfer.

Parameters

file_id (str) – a file ID

concatenate(outbound_sources, file_name, content_type=None, file_info=None, progress_listener=None, recommended_upload_part_size=None, continue_large_file_id=None, encryption: Optional[EncryptionSetting] = None, file_retention: Optional[FileRetentionSetting] = None, legal_hold: Optional[LegalHold] = None, min_part_size=None, max_part_size=None)[source]

Creates a new file in this bucket by concatenating multiple remote or local sources.

Parameters
  • outbound_sources (list[b2sdk.v2.OutboundTransferSource]) – list of outbound sources (remote or local)

  • new_file_name (str) – file name of the new file

  • content_type (str,None) – content_type for the new file, if None content_type would be automatically determined from file name or it may be copied if it resolves as single part remote source copy

  • file_info (dict,None) – file_info for the new file, if None it will be set to empty dict or it may be copied if it resolves as single part remote source copy

  • progress_listener (b2sdk.v2.AbstractProgressListener,None) – a progress listener object to use, or None to not report progress

  • recommended_upload_part_size (int,None) – the recommended part size to use for uploading local sources or None to determine automatically, but remote sources would be copied with maximum possible part size

  • continue_large_file_id (str,None) – large file id that should be selected to resume file creation for multipart upload/copy, None for automatic search for this id

  • encryption (b2sdk.v2.EncryptionSetting) – encryption settings (None if unknown)

  • file_retention (b2sdk.v2.FileRetentionSetting) – file retention setting

  • legal_hold (bool) – legal hold setting

  • min_part_size (int) – lower limit of part size for the transfer planner, in bytes

  • max_part_size (int) – upper limit of part size for the transfer planner, in bytes

concatenate_stream(outbound_sources_iterator, file_name, content_type=None, file_info=None, progress_listener=None, recommended_upload_part_size=None, continue_large_file_id=None, encryption: Optional[EncryptionSetting] = None, file_retention: Optional[FileRetentionSetting] = None, legal_hold: Optional[LegalHold] = None)[source]

Creates a new file in this bucket by concatenating stream of multiple remote or local sources.

Parameters
  • outbound_sources_iterator (iterator[b2sdk.v2.OutboundTransferSource]) – iterator of outbound sources

  • new_file_name (str) – file name of the new file

  • content_type (str,None) – content_type for the new file, if None content_type would be automatically determined or it may be copied if it resolves as single part remote source copy

  • file_info (dict,None) – file_info for the new file, if None it will be set to empty dict or it may be copied if it resolves as single part remote source copy

  • progress_listener (b2sdk.v2.AbstractProgressListener,None) – a progress listener object to use, or None to not report progress

  • recommended_upload_part_size (int,None) – the recommended part size to use for uploading local sources or None to determine automatically, but remote sources would be copied with maximum possible part size

  • continue_large_file_id (str,None) – large file id that should be selected to resume file creation for multipart upload/copy, if None in multipart case it would always start a new large file

  • encryption (b2sdk.v2.EncryptionSetting) – encryption setting (None if unknown)

  • file_retention (b2sdk.v2.FileRetentionSetting) – file retention setting

  • legal_hold (bool) – legal hold setting

copy(file_id, new_file_name, content_type=None, file_info=None, offset=0, length=None, progress_listener=None, destination_encryption: Optional[EncryptionSetting] = None, source_encryption: Optional[EncryptionSetting] = None, source_file_info: Optional[dict] = None, source_content_type: Optional[str] = None, file_retention: Optional[FileRetentionSetting] = None, legal_hold: Optional[LegalHold] = None, min_part_size=None, max_part_size=None)[source]

Creates a new file in this bucket by (server-side) copying from an existing file.

Parameters
  • file_id (str) – file ID of existing file to copy from

  • new_file_name (str) – file name of the new file

  • content_type (str,None) – content_type for the new file, if None and b2_copy_file will be used content_type will be copied from source file - otherwise content_type would be automatically determined

  • file_info (dict,None) – file_info for the new file, if None will and b2_copy_file will be used file_info will be copied from source file - otherwise it will be set to empty dict

  • offset (int) – offset of existing file that copy should start from

  • length (int,None) – number of bytes to copy, if None then offset have to be 0 and it will use b2_copy_file without range parameter so it may fail if file is too large. For large files length have to be specified to use b2_copy_part instead.

  • progress_listener (b2sdk.v2.AbstractProgressListener,None) – a progress listener object to use for multipart copy, or None to not report progress

  • destination_encryption (b2sdk.v2.EncryptionSetting) – encryption settings for the destination (None if unknown)

  • source_encryption (b2sdk.v2.EncryptionSetting) – encryption settings for the source (None if unknown)

  • source_file_info (dict,None) – source file’s file_info dict, useful when copying files with SSE-C

  • source_content_type (str,None) – source file’s content type, useful when copying files with SSE-C

  • file_retention (b2sdk.v2.FileRetentionSetting) – file retention setting for the new file.

  • legal_hold (bool) – legal hold setting for the new file.

  • min_part_size (int) – lower limit of part size for the transfer planner, in bytes

  • max_part_size (int) – upper limit of part size for the transfer planner, in bytes

create_file(write_intents, file_name, content_type=None, file_info=None, progress_listener=None, recommended_upload_part_size=None, continue_large_file_id=None, encryption: Optional[EncryptionSetting] = None, file_retention: Optional[FileRetentionSetting] = None, legal_hold: Optional[LegalHold] = None, min_part_size=None, max_part_size=None)[source]

Creates a new file in this bucket using an iterable (list, tuple etc) of remote or local sources.

Source ranges can overlap and remote sources will be prioritized over local sources (when possible). For more information and usage examples please see Advanced usage patterns.

Parameters
  • write_intents (list[b2sdk.v2.WriteIntent]) – list of write intents (remote or local sources)

  • new_file_name (str) – file name of the new file

  • content_type (str,None) – content_type for the new file, if None content_type would be automatically determined or it may be copied if it resolves as single part remote source copy

  • file_info (dict,None) – file_info for the new file, if None it will be set to empty dict or it may be copied if it resolves as single part remote source copy

  • progress_listener (b2sdk.v2.AbstractProgressListener,None) – a progress listener object to use, or None to not report progress

  • recommended_upload_part_size (int,None) – the recommended part size to use for uploading local sources or None to determine automatically, but remote sources would be copied with maximum possible part size

  • continue_large_file_id (str,None) – large file id that should be selected to resume file creation for multipart upload/copy, None for automatic search for this id

  • encryption (b2sdk.v2.EncryptionSetting) – encryption settings (None if unknown)

  • file_retention (b2sdk.v2.FileRetentionSetting) – file retention setting

  • legal_hold (bool) – legal hold setting

  • min_part_size (int) – lower limit of part size for the transfer planner, in bytes

  • max_part_size (int) – upper limit of part size for the transfer planner, in bytes

create_file_stream(write_intents_iterator, file_name, content_type=None, file_info=None, progress_listener=None, recommended_upload_part_size=None, continue_large_file_id=None, encryption: Optional[EncryptionSetting] = None, file_retention: Optional[FileRetentionSetting] = None, legal_hold: Optional[LegalHold] = None, min_part_size=None, max_part_size=None)[source]

Creates a new file in this bucket using a stream of multiple remote or local sources.

Source ranges can overlap and remote sources will be prioritized over local sources (when possible). For more information and usage examples please see Advanced usage patterns.

Parameters
  • write_intents_iterator (iterator[b2sdk.v2.WriteIntent]) – iterator of write intents which are sorted ascending by destination_offset

  • new_file_name (str) – file name of the new file

  • content_type (str,None) – content_type for the new file, if None content_type would be automatically determined or it may be copied if it resolves as single part remote source copy

  • file_info (dict,None) – file_info for the new file, if None it will be set to empty dict or it may be copied if it resolves as single part remote source copy

  • progress_listener (b2sdk.v2.AbstractProgressListener,None) – a progress listener object to use, or None to not report progress

  • recommended_upload_part_size (int,None) – the recommended part size to use for uploading local sources or None to determine automatically, but remote sources would be copied with maximum possible part size

  • continue_large_file_id (str,None) – large file id that should be selected to resume file creation for multipart upload/copy, if None in multipart case it would always start a new large file

  • encryption (b2sdk.v2.EncryptionSetting) – encryption settings (None if unknown)

  • file_retention (b2sdk.v2.FileRetentionSetting) – file retention setting

  • legal_hold (bool) – legal hold setting

  • min_part_size (int) – lower limit of part size for the transfer planner, in bytes

  • max_part_size (int) – upper limit of part size for the transfer planner, in bytes

delete_file_version(file_id, file_name)[source]

Delete a file version.

Parameters
  • file_id (str) – a file ID

  • file_name (str) – a file name

download_file_by_id(file_id: str, progress_listener: Optional[AbstractProgressListener] = None, range_: Optional[Tuple[int, int]] = None, encryption: Optional[EncryptionSetting] = None) DownloadedFile[source]

Download a file by ID.

Note

download_file_by_id actually belongs in b2sdk.v2.B2Api, not in b2sdk.v2.Bucket; we just provide a convenient redirect here

Parameters
  • file_id – a file ID

  • progress_listener – a progress listener object to use, or None to not track progress

  • range – two integer values, start and end offsets

  • encryption – encryption settings (None if unknown)

download_file_by_name(file_name: str, progress_listener: Optional[AbstractProgressListener] = None, range_: Optional[Tuple[int, int]] = None, encryption: Optional[EncryptionSetting] = None) DownloadedFile[source]

Download a file by name.

See also

Synchronizer, a high-performance utility that synchronizes a local folder with a Bucket.

Parameters
  • file_name – a file name

  • progress_listener – a progress listener object to use, or None to not track progress

  • range – two integer values, start and end offsets

  • encryption – encryption settings (None if unknown)

get_download_authorization(file_name_prefix, valid_duration_in_seconds)[source]

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

Parameters
  • file_name_prefix (str) – a file name prefix, only files that match it could be downloaded

  • valid_duration_in_seconds (int) – a token is valid only during this amount of seconds

get_download_url(filename)[source]

Get file download URL.

Parameters

filename (str) – a file name

Return type

str

get_file_info_by_id(file_id: str) FileVersion[source]

Gets a file version’s by ID.

Parameters

file_id (str) – the id of the file who’s info will be retrieved.

Return type

generator[b2sdk.v2.FileVersion]

get_file_info_by_name(file_name: str) DownloadVersion[source]

Gets a file’s DownloadVersion by name.

Parameters

file_name (str) – the name of the file who’s info will be retrieved.

get_id() str[source]

Return bucket ID.

Return type

str

hide_file(file_name)[source]

Hide a file.

Parameters

file_name (str) – a file name

Return type

b2sdk.v2.FileVersion

list_file_versions(file_name, fetch_count=None)[source]

Lists all of the versions for a single file.

Parameters
  • file_name (str) – the name of the file to list.

  • fetch_count (int,None) – how many entries to list per API call or None to use the default. Acceptable values: 1 - 10000

Return type

generator[b2sdk.v2.FileVersion]

list_parts(file_id, start_part_number=None, batch_size=None)[source]

Get a list of all parts that have been uploaded for a given file.

Parameters
  • file_id (str) – a file ID

  • start_part_number (int) – the first part number to return. defaults to the first part.

  • batch_size (int) – the number of parts to fetch at a time from the server

list_unfinished_large_files(start_file_id=None, batch_size=None, prefix=None)[source]

A generator that yields an b2sdk.v2.UnfinishedLargeFile for each unfinished large file in the bucket, starting at the given file, filtering by prefix.

Parameters
  • start_file_id (str,None) – a file ID to start from or None to start from the beginning

  • batch_size (int,None) – max file count

  • prefix (str,None) – file name prefix filter

Return type

generator[b2sdk.v2.UnfinishedLargeFile]

ls(folder_to_list: str = '', latest_only: bool = True, recursive: bool = False, fetch_count: Optional[int] = 10000, with_wildcard: bool = False)[source]

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

B2 has a flat namespace for the files in a bucket, but there is a convention of using “/” as if there were folders. This method searches through the flat namespace to find the files and “folders” that live within a given folder.

When the recursive flag is set, lists all of the files in the given folder, and all of its sub-folders.

Parameters
  • folder_to_list – the name of the folder to list; must not start with “/”. Empty string means top-level folder

  • latest_only – when False returns info about all versions of a file, when True, just returns info about the most recent versions

  • recursive – if True, list folders recursively

  • fetch_count – how many entries to return or None to use the default. Acceptable values: 1 - 10000

  • with_wildcard – Accepts “*”, “?”, “[]” and “[!]” in folder_to_list, similarly to what shell does. As of 1.19.0 it can only be enabled when recursive is also enabled. Also, in this mode, folder_to_list is considered to be a filename or a pattern.

Return type

generator[tuple[b2sdk.v2.FileVersion, str]]

Returns

generator of (file_version, folder_name) tuples

Note

In case of recursive=True, folder_name is not returned.

set_info(new_bucket_info, if_revision_is=None) Bucket[source]

Update bucket info.

Parameters
  • new_bucket_info (dict) – new bucket info dictionary

  • if_revision_is (int) – revision number, update the info only if revision equals to if_revision_is

set_type(bucket_type) Bucket[source]

Update bucket type.

Parameters

bucket_type (str) – a bucket type (“allPublic” or “allPrivate”)

update(bucket_type: Optional[str] = None, bucket_info: Optional[dict] = None, cors_rules: Optional[dict] = None, lifecycle_rules: Optional[list] = None, if_revision_is: Optional[int] = None, default_server_side_encryption: Optional[EncryptionSetting] = None, default_retention: Optional[BucketRetentionSetting] = None, replication: Optional[ReplicationConfiguration] = None, is_file_lock_enabled: Optional[bool] = None) Bucket[source]

Update various bucket parameters.

Parameters
  • bucket_type – a bucket type, e.g. allPrivate or allPublic

  • bucket_info – an info to store with a bucket

  • cors_rules – CORS rules to store with a bucket

  • lifecycle_rules – lifecycle rules to store with a bucket

  • if_revision_is – revision number, update the info only if revision equals to if_revision_is

  • default_server_side_encryption – default server side encryption settings (None if unknown)

  • default_retention – bucket default retention setting

  • replication – replication rules for the bucket

  • is_file_lock_enabled (bool) – specifies whether bucket should get File Lock-enabled

upload(upload_source, file_name, content_type=None, file_info=None, min_part_size=None, progress_listener=None, encryption: Optional[EncryptionSetting] = None, file_retention: Optional[FileRetentionSetting] = None, legal_hold: Optional[LegalHold] = None)[source]

Upload a file to B2, retrying as needed.

The source of the upload is an UploadSource object that can be used to open (and re-open) the file. The result of opening should be a binary file whose read() method returns bytes.

The function opener should return a file-like object, and it must be possible to call it more than once in case the upload is retried.

Parameters
  • upload_source (b2sdk.v2.AbstractUploadSource) – an object that opens the source of the upload

  • file_name (str) – the file name of the new B2 file

  • content_type (str,None) – the MIME type, or None to accept the default based on file extension of the B2 file name

  • file_info (dict,None) – a file info to store with the file or None to not store anything

  • min_part_size (int,None) – the smallest part size to use or None to determine automatically

  • progress_listener (b2sdk.v2.AbstractProgressListener,None) – a progress listener object to use, or None to not report progress

  • encryption (b2sdk.v2.EncryptionSetting) – encryption settings (None if unknown)

  • file_retention (b2sdk.v2.FileRetentionSetting) – file retention setting

  • legal_hold (bool) – legal hold setting

Return type

b2sdk.v2.FileVersion

upload_bytes(data_bytes, file_name, content_type=None, file_infos=None, progress_listener=None, encryption: Optional[EncryptionSetting] = None, file_retention: Optional[FileRetentionSetting] = None, legal_hold: Optional[LegalHold] = None)[source]

Upload bytes in memory to a B2 file.

Parameters
  • data_bytes (bytes) – a byte array to upload

  • file_name (str) – a file name to upload bytes to

  • content_type (str,None) – the MIME type, or None to accept the default based on file extension of the B2 file name

  • file_infos (dict,None) – a file info to store with the file or None to not store anything

  • progress_listener (b2sdk.v2.AbstractProgressListener,None) – a progress listener object to use, or None to not track progress

  • encryption (b2sdk.v2.EncryptionSetting) – encryption settings (None if unknown)

  • file_retention (b2sdk.v2.FileRetentionSetting) – file retention setting

  • legal_hold (bool) – legal hold setting

Return type

generator[b2sdk.v2.FileVersion]

upload_local_file(local_file, file_name, content_type=None, file_infos=None, sha1_sum=None, min_part_size=None, progress_listener=None, encryption: Optional[EncryptionSetting] = None, file_retention: Optional[FileRetentionSetting] = None, legal_hold: Optional[LegalHold] = None)[source]

Upload a file on local disk to a B2 file.

See also

Synchronizer, a high-performance utility that synchronizes a local folder with a bucket.

Parameters
  • local_file (str) – a path to a file on local disk

  • file_name (str) – a file name of the new B2 file

  • content_type (str,None) – the MIME type, or None to accept the default based on file extension of the B2 file name

  • file_infos (dict,None) – a file info to store with the file or None to not store anything

  • sha1_sum (str,None) – file SHA1 hash or None to compute it automatically

  • min_part_size (int) – a minimum size of a part

  • progress_listener (b2sdk.v2.AbstractProgressListener,None) – a progress listener object to use, or None to not report progress

  • encryption (b2sdk.v2.EncryptionSetting) – encryption settings (None if unknown)

  • file_retention (b2sdk.v2.FileRetentionSetting) – file retention setting

  • legal_hold (bool) – legal hold setting

Return type

b2sdk.v2.FileVersion