Cloud Shell 是运行小型示例和测试的绝佳环境。本指南介绍如何在 Cloud Shell 中配置 Rust 并安装其中一个 Cloud 客户端库。
启动 Cloud Shell
- 在 Google Cloud 控制台的项目选择器中,选择一个项目。
- 打开
https://shell.cloud.google.com以启动新的 shell。系统可能会提示您授权 Cloud Shell 使用您的凭据进行Google Cloud API 调用。
配置 Rust
Cloud Shell 预装了
rustup。您可以使用它来安装和配置默认版本的 Rust:rustup default stable确认您安装的是最新版 Rust:
cargo --version
在 Cloud Shell 中安装 Rust 客户端库
创建新的 Rust 项目:
cargo new my-project将目录更改为新项目:
cd my-project将 Secret Manager 客户端库添加到新项目中:
cargo add google-cloud-secretmanager-v1将 google-cloud-gax crate 添加到新项目中:
cargo add google-cloud-gax将 tokio 箱添加到新项目中:
cargo add tokio --features macros修改项目中的
src/main.rs以使用 Secret Manager 客户端库:#[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(()) }运行程序,并提供您的 Google Cloud 项目 ID:
PROJECT_ID=$(gcloud config get project) cargo run ${PROJECT_ID}该程序将输出与您的项目 ID 关联的 Secret。如果您没有看到任何密文,则可能表示您在 Secret Manager 中没有任何密文。创建密文并重新运行程序,您应该会在输出中看到打印的密文。