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:
- Authenticate with B2 to obtain an Authorization object.
- Create an UploadFile request.
- Call get_upload_authorization to get an UploadAuthorization for a bucket.
- 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:
- Authenticate with B2 to obtain an Authorization object.
- 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.
- 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.
- Create an UploadFilePart object via the UploadFilePartBuilder.
- Use the
UploadPartAuthorization
andUploadFilePart
to call upload_file_part with the file data to upload. - Call UploadFilePart::create_next_part to create a new upload part request.
- Repeat steps 5 and 6 until all parts have been uploaded.
- 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§
- Byte
Range - A byte-range to retrieve a portion of a file.
- Cache
Control - A Cache-Control header.
- Cancelled
File Upload - A large file that was cancelled prior to upload completion.
- Content
Encoding - Specify the compression algorithm.
- Copy
File - A request to copy a file from a bucket, potentially to a different bucket.
- Copy
File Builder - A builder to create a CopyFile request.
- Copy
File Part - A request to copy from an existing file to a part of a large file.
- Copy
File Part Builder - A builder to create a CopyFilePart request.
- Deleted
File - Download
Authorization - A capability token that authorizes downloading files from a private bucket.
- Download
Authorization Request - A request to obtain a DownloadAuthorization.
- Download
Authorization Request Builder - A builder to create a DownloadAuthorizationRequest.
- Download
File - A request to download a file or a portion of a file from the B2 API.
- Download
File Builder - Expires
- HTTP
Expires
header - File
- Metadata of a file stored in B2.
- File
Legal Hold - Determines whether there is a legal hold on a file.
- File
Part - A part of a large file currently being uploaded.
- File
Retention - The retention settings for a file.
- File
Retention Setting - Sets the file retention mode and date/time during which the retention rule applies.
- List
File Names - A request to list the names of files stored in a bucket.
- List
File Names Builder - A builder for a ListFileNames request.
- List
File Parts - List
File Parts Builder - List
File Versions - A request to list the names of files stored in a bucket.
- List
File Versions Builder - A builder for a ListFileVersions request.
- List
Unfinished Large Files - List
Unfinished Large Files Builder - Mime
- An IANA media type.
- Start
Large File - A request to prepare to upload a large file.
- Start
Large File Builder - A builder for a StartLargeFile request.
- Update
File Legal Hold - A request to enable or disable a legal hold on a specific file.
- Update
File Legal Hold Builder - A builder for an UpdateFileLegalHold request.
- Update
File Retention - A request to update file retention settings on a file.
- Update
File Retention Builder - A builder for an UpdateFileRetention request.
- Upload
Authorization - An authorization to upload a file to a B2 bucket.
- Upload
File - A request to upload a file to B2.
- Upload
File Builder - A builder to create an UploadFile request.
- Upload
File Part - A request to upload part of a large file.
- Upload
File Part Builder - A builder for an UploadFilePart request.
- Upload
Part Authorization - An authorization to upload file contents to a B2 file.
Enums§
- Bypass
Governance - Declare whether to bypass file lock restrictions when performing an action on a File.
- Download
Auth - Allow downloading files via an
Authorization
orDownloadAuthorization
. - File
Action - The action taken that resulted in a File object returned by the B2 API.
- Metadata
Directive - Describe the action to take with file metadata when copying a file.
Functions§
- cancel_
large_ file - Cancel the uploading of a large file and delete any parts already uploaded.
- cancel_
large_ file_ by_ id - Cancel the uploading of a large file and delete any parts already uploaded.
- copy_
file - Copy an existing file to a new file, possibly on a different bucket.
- copy_
file_ part - Copy from an existing file to a new large file.
- delete_
file_ version - Delete a version of a file.
- delete_
file_ version_ by_ name_ id - Delete a version of a file.
- download_
file - Download a file from the B2 service.
- download_
file_ headers - Retrieve the headers that will be returned when the specified file is downloaded.
- download_
file_ headers_ by_ id - Retrieve the headers that will be returned when the specified file is downloaded.
- finish_
large_ file_ upload - Complete the upload of a large file, merging all parts into a single File.
- finish_
large_ file_ upload_ by_ id - Complete the upload of a large file, merging all parts into a single File.
- get_
download_ authorization - Generate a download authorization token to download files with a specific prefix from a private B2 bucket.
- get_
file_ info - Retrieve metadata about a file stored in B2.
- get_
upload_ authorization - Obtain an authorization to upload files to a bucket.
- get_
upload_ authorization_ by_ id - Obtain an authorization to upload files to a bucket.
- get_
upload_ part_ authorization - Get an UploadPartAuthorization to upload data to a new B2 file.
- get_
upload_ part_ authorization_ by_ id - Get an UploadPartAuthorization to upload data to a new B2 file.
- hide_
file - Hide a file so that it cannot be downloaded by name.
- hide_
file_ by_ name - Hide a file so that it cannot be downloaded by name.
- list_
file_ names - Get a list of file names in a bucket.
- list_
file_ parts - List the parts of a large file that has not yet been completed.
- list_
file_ versions - List all versions of the files contained in a bucket.
- list_
unfinished_ large_ files - Get a list of the unfinished large files stored by B2.
- start_
large_ file - Prepare to upload a large file in multiple parts.
- update_
file_ legal_ hold - Enable or disable a legal hold on a file.
- update_
file_ retention - Modify the file lock retention settings for a file.
- upload_
file - Upload a file to a B2 bucket.
- upload_
file_ part - Upload a part of a large file to B2.