The Cloud Storage client in the Cloud Client Libraries for Rust writes objects by
pulling payload chunks from a type that implements the StreamingSource trait.
If your application detects an error condition while generating or streaming
data, returning an error from your StreamingSource implementation stops the
client library from finalizing the object creation.
This document shows you how to define a custom error type and implement
StreamingSource to return that error mid-stream. You also verify that the
failed upload returns your error without creating the object. If you're new to
the client library, see
Using Cloud Storage with Rust first.
Before you begin
Before running the code in this document, complete the following prerequisites:
- Create or select a Google Cloud project with billing enabled.
- Create a Cloud Storage bucket in your project.
- Configure Application Default Credentials and grant your principal an
Identity and Access Management (IAM) role with permission to create objects in the
bucket, such as Storage Object User (
roles/storage.objectUser).
Dependencies
Add the Cloud Storage client library and supporting crates to your
Cargo.toml file:
cargo add google-cloud-storage anyhow bytes
Create a custom error type
To cancel an object write before finalization, your
StreamingSource implementation must return an error that
implements std::error::Error + Send + Sync + 'static. You can use any
existing error type that meets these bounds or define a custom enum for your
application. Complete the following steps to define a custom error enum:
Define a custom error enum (
MyError):Implement the standard
Errortrait so the client library can preserve your error as the underlying cause:Implement the
Displaytrait required bystd::error::Error:
Implement a custom streaming source
Next, create a type that generates synthetic chunks and returns an error after 41 chunks so the upload stops before completion. Complete the following steps:
Define a struct to track the chunk counter:
Implement
StreamingSourceforMySource, associatingtype Error = MyErrorand returningSome(Err(MyError::ExpectedProblem))on iteration 42:
When next returns Some(Err(...)), the client library stops reading from the
source and cancels the write request without finalizing the object.
Attempt the upload and inspect the error
After implementing StreamingSource, attempt a buffered object write and verify
that the client returns a serialization error wrapping MyError. Because
MySource is a single-pass source that doesn't implement Seek, start the
write with send_buffered instead of send_unbuffered.
Inside an async function that accepts bucket_name in the format
projects/_/buckets/BUCKET_ID, complete the following
steps:
Initialize a
Storageclient:Start the buffered object write using
MySource::default()as the payload source:Verify that the upload fails with a serialization error whose source error is
MyError:Return
Ok(())to complete the function:
When you run attempt_upload, it prints an Upload result Err(...) message
containing MyError::ExpectedProblem. The assertions verify that the client
stopped the upload and returned a serialization error wrapping MyError. Because
the write was canceled before finalization, Cloud Storage doesn't
create the expect-error object in the bucket.
Clean up
Even though the canceled write doesn't create an object, delete any test objects that you created for this document. Deleting test resources when you're finished avoids ongoing charges.
Complete code
The following example combines all the preceding steps into a complete module:
What's next
- Learn how to push data on Cloud Storage object writes.