Expand description

Backblaze B2 API calls for working with files.

See the B2 documentation on uploading files for an overview of the process.

Uploading Files

For files larger than 5 GB, see Uploading Large Files.

To upload a file:

  1. Authenticate with B2 to obtain an Authorization object.
  2. Create an UploadFile request.
  3. Call get_upload_authorization to get an UploadAuthorization for a bucket.
  4. Call upload_file with your UploadAuthorization, UploadFile request, and file data.

You can upload multiple files with a single UploadAuthorization, but only one at a time. To upload multiple files in parallel, each thread or task needs to obtain its own UploadAuthorization.

Uploading Large Files

To upload a large file:

  1. Authenticate with B2 to obtain an Authorization object.
  2. Create a StartLargeFile object with the destination bucket and new file’s information, then pass it to start_large_file. You will receive a File object.
  3. Pass the File to get_upload_part_authorization to receive an UploadPartAuthorization.
    • You can upload parts in separate threads for better performance; each thread must call get_upload_part_authorization and use its respective authorization when uploading data.
  4. Create an UploadFilePart object via the UploadFilePartBuilder.
  5. Use the UploadPartAuthorization and UploadFilePart to call upload_file_part with the file data to upload.
  6. Call UploadFilePart::create_next_part to create a new upload part request.
  7. Repeat steps 5 and 6 until all parts have been uploaded.
  8. Call finish_large_file_upload to merge the file parts into a single File. After finishing the file, it can be treated like any other uploaded file.

Examples

use std::env;
use anyhow;
use b2_client::{self as b2, HttpClient as _};

async fn upload_file(name: &str, bucket: b2::Bucket, data: &[u8])
-> anyhow::Result<b2::File> {
    let key = env::var("B2_KEY").ok().unwrap();
    let key_id = env::var("B2_KEY_ID").ok().unwrap();

    let client = b2::client::SurfClient::default();
    let mut auth = b2::authorize_account(client, &key, &key_id).await?;

    let mut upload_auth = b2::get_upload_authorization(&mut auth, &bucket)
        .await?;

    let checksum = calculate_sha1(&data);

    let file = b2::UploadFile::builder()
        .file_name(name)?
        .sha1_checksum(&checksum)
        .build()?;

    Ok(b2::upload_file(&mut upload_auth, file, &data).await?)
}
use std::env;
use anyhow;
use b2_client::{self as b2, HttpClient as _};

async fn upload_large_file(
    name: &str,
    data_part1: &[u8],
    data_part2: &[u8],
) -> anyhow::Result<b2::File> {
    let key = env::var("B2_KEY").ok().unwrap();
    let key_id = env::var("B2_KEY_ID").ok().unwrap();

    let client = b2::client::SurfClient::default();
    let mut auth = b2::authorize_account(client, &key, &key_id).await?;

    let file = b2::StartLargeFile::builder()
        .bucket_id("some-bucket-id")
        .file_name(name)?
        .content_type("text/plain")
        .build()?;

    let file = b2::start_large_file(&mut auth, file).await?;

    let mut upload_auth = b2::get_upload_part_authorization(
        &mut auth,
        &file
    ).await?;

    // Assuming a `calculate_sha1` function is defined:
    let sha1 = calculate_sha1(&data_part1);
    let sha2 = calculate_sha1(&data_part2);

    let upload_req = b2::UploadFilePart::builder()
        .part_sha1_checksum(&sha1)
        .build();

    let _part1 = b2::upload_file_part(
        &mut upload_auth,
        &upload_req,
        &data_part1,
    ).await?;

    let upload_req = upload_req.create_next_part(Some(&sha2))?;

    let _part2 = b2::upload_file_part(
        &mut upload_auth,
        &upload_req,
        &data_part2,
    ).await?;

    Ok(b2::finish_large_file_upload(
        &mut auth,
        &file,
        &[sha1, sha2],
    ).await?)
}

Structs

A byte-range to retrieve a portion of a file.

A Cache-Control header.

A large file that was cancelled prior to upload completion.

Specify the compression algorithm.

A request to copy a file from a bucket, potentially to a different bucket.

A builder to create a CopyFile request.

A request to copy from an existing file to a part of a large file.

A builder to create a CopyFilePart request.

A capability token that authorizes downloading files from a private bucket.

A request to download a file or a portion of a file from the B2 API.

HTTP Expires header

Metadata of a file stored in B2.

Determines whether there is a legal hold on a file.

A part of a large file currently being uploaded.

The retention settings for a file.

Sets the file retention mode and date/time during which the retention rule applies.

A request to list the names of files stored in a bucket.

A builder for a ListFileNames request.

A request to list the names of files stored in a bucket.

A builder for a ListFileVersions request.

An IANA media type.

A request to prepare to upload a large file.

A builder for a StartLargeFile request.

A request to enable or disable a legal hold on a specific file.

A request to update file retention settings on a file.

An authorization to upload a file to a B2 bucket.

A request to upload a file to B2.

A builder to create an UploadFile request.

A request to upload part of a large file.

A builder for an UploadFilePart request.

An authorization to upload file contents to a B2 file.

Enums

Declare whether to bypass file lock restrictions when performing an action on a File.

Allow downloading files via an Authorization or DownloadAuthorization.

The action taken that resulted in a File object returned by the B2 API.

Describe the action to take with file metadata when copying a file.

Functions

Cancel the uploading of a large file and delete any parts already uploaded.

Cancel the uploading of a large file and delete any parts already uploaded.

Copy an existing file to a new file, possibly on a different bucket.

Copy from an existing file to a new large file.

Delete a version of a file.

Delete a version of a file.

Download a file from the B2 service.

Retrieve the headers that will be returned when the specified file is downloaded.

Retrieve the headers that will be returned when the specified file is downloaded.

Complete the upload of a large file, merging all parts into a single File.

Complete the upload of a large file, merging all parts into a single File.

Generate a download authorization token to download files with a specific prefix from a private B2 bucket.

Retrieve metadata about a file stored in B2.

Obtain an authorization to upload files to a bucket.

Obtain an authorization to upload files to a bucket.

Get an UploadPartAuthorization to upload data to a new B2 file.

Get an UploadPartAuthorization to upload data to a new B2 file.

Hide a file so that it cannot be downloaded by name.

Hide a file so that it cannot be downloaded by name.

Get a list of file names in a bucket.

List the parts of a large file that has not yet been completed.

List all versions of the files contained in a bucket.

Get a list of the unfinished large files stored by B2.

Prepare to upload a large file in multiple parts.

Enable or disable a legal hold on a file.

Modify the file lock retention settings for a file.

Upload a file to a B2 bucket.

Upload a part of a large file to B2.