在开始之前,您应满足以下前提条件:
安装 Rust。如果您尚未安装 Rust,请参阅有关入门的 Rust 文档。
您可以使用以下命令确认是否已安装 Rust(以及 Rust 的版本):
cargo --version安装编辑器或 IDE。
安装 Google Cloud CLI。
创建新的 Rust 项目
如需创建新的 Rust 项目,请运行以下命令:
cargo new my-project
运行以下命令,将目录更改为新项目:
cd my-project
安装 Google Cloud 客户端库
Rust 版 Cloud 客户端库是 Rust 开发者与 Google Cloud 服务(例如 Secret Manager 和 Workflows)集成的惯用方式。
将 Secret Manager 客户端库添加到新项目中:
cargo add google-cloud-secretmanager-v1如果您尚未启用 Secret Manager API,请在 API 和服务中启用它,或运行以下命令:
gcloud services enable secretmanager.googleapis.com将
google-cloud-gax箱添加到新项目中: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()