Google Cloud Storage is a managed service for storing unstructured data.
The Rust client library provides an idiomatic API to access this service. The client library resumes interrupted downloads and uploads, and automatically performs integrity checks on the data. For metadata operations, the client library can retry failed requests, and automatically poll long-running operations.
This guide shows you how to create a Cloud Storage bucket, upload an object to this bucket, and then read the object back.
Before you start this guide, create a Google Cloud project with billing enabled.
Add the client library dependency
cargo add google-cloud-storage
Create a storage bucket
The client to perform operations on buckets and object metadata is called
StorageControl
.
use google_cloud_storage::model::{Bucket, bucket::IamConfig, bucket::iam_config::UniformBucketLevelAccess};
use google_cloud_storage::client::StorageControl;
let control = StorageControl::builder().build().await?;
To create a bucket, you must provide your project name and the bucket ID.
let bucket = control
.create_bucket()
.set_parent("projects/_")
.set_bucket_id(bucket_id)
.set_bucket(
gcs::model::Bucket::new()
.set_project(format!("projects/{project_id}"))
)
You can also provide other attributes for the bucket. For example, if you want all objects in the bucket to use the same permissions, you can enable Uniform bucket-level access.
Bucket::new()
.set_project(format!("projects/{project_id}"))
.set_iam_config(
IamConfig::new()
.set_uniform_bucket_level_access(
UniformBucketLevelAccess::new()
.set_enabled(true),
),
),
Then send this request and wait for the response.
.send()
.await?;
println!("bucket successfully created {bucket:?}");
Upload an object
The client to perform operations on object data is called Storage.
use google_cloud_storage::client::Storage;
let client = Storage::builder().build().await?;
In this case, you create an object called hello.txt
with the greeting
Hello World!.
let object = client
.upload_object(&bucket.name, "hello.txt", "Hello World!")
.send_buffered()
.await?;
println!("object successfully uploaded {object:?}");
Download an object
To download the contents of an object, use the read_object()
method.
let mut reader = client.read_object(&bucket.name, "hello.txt").send().await?;
let mut contents = Vec::new();
while let Some(chunk) = reader.next().await.transpose()? {
contents.extend_from_slice(&chunk);
}
println!(
"object contents successfully downloaded {:?}",
bytes::Bytes::from_owner(contents)
);
Clean up resources
Finally, remove the object and bucket to clean up the resources used in this guide.
control
.delete_object()
.set_bucket(&bucket.name)
.set_object(&object.name)
.set_generation(object.generation)
.send()
.await?;
control
.delete_bucket()
.set_name(&bucket.name)
.send()
.await?;