Cloud Shell is a great environment to run small examples and tests. This guide shows you how to configure Rust and install one of the Cloud Client Libraries in Cloud Shell.
Start up Cloud Shell
- In the Google Cloud console project selector , select a project.
- Open
https://shell.cloud.google.com
to start a new shell. You might be prompted to authorize Cloud Shell to use your credentials for Google Cloud API calls.
Configure Rust
Cloud Shell comes with
rustup
pre-installed. You can use it to install and configure the default version of Rust:rustup default stable
Confirm that you have the most recent version of Rust installed:
cargo --version
Install Rust client libraries in Cloud Shell
Create a new Rust project:
cargo new my-project
Change your directory to the new project:
cd my-project
Add the Secret Manager client library to the new project:
cargo add google-cloud-secretmanager-v1
Add the google-cloud-gax crate to the new project:
cargo add google-cloud-gax
Add the tokio crate to the new project:
cargo add tokio --features macros
Edit
src/main.rs
in your project to use the Secret Manager client library:#[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> { use google_cloud_gax::paginator::ItemPaginator as _; use google_cloud_secretmanager_v1::client::SecretManagerService; let project_id = std::env::args().nth(1).unwrap(); let client = SecretManagerService::builder().build().await?; let mut items = client .list_secrets() .set_parent(format!("projects/{project_id}")) .by_item(); while let Some(item) = items.next().await { println!("{}", item?.name); } Ok(()) }
Run your program, supplying your Google Cloud project ID:
PROJECT_ID=$(gcloud config get project) cargo run ${PROJECT_ID}
The program will print the secrets associated with your project ID. If you don't see any secrets, you might not have any in Secret Manager. Create a secret and rerun the program, and you should see the secret printed in the output.