Google Cloud Rust 適用的用戶端程式庫會使用用戶端做為主要抽象層,與特定服務介接。用戶端會實作為 Rust 結構體,方法則對應服務提供的每個 RPC。如要使用 Rust 用戶端程式庫存取Google Cloud 服務,請先初始化用戶端。
在本指南中,您將初始化用戶端,然後使用該用戶端透過 Secret Manager API 發出 RPC。任何其他服務也適用相同的結構。 Google Cloud
按照本指南操作前,請先完成下列事項:
- 按照其中一個 Secret Manager 入門指南操作,例如「建立 Secret」指南。這些指南會更詳細地說明服務專屬概念,並提供專案必要條件的詳細指引。
- 請按照「進行驗證以使用用戶端程式庫」指南中的操作說明進行。 本指南將說明如何登入,設定本指南中使用的應用程式預設憑證。
依附元件
使用 Rust 時,您必須在 Cargo.toml 檔案中宣告依附元件:
$ cargo add google-cloud-secretmanager-v1
如要初始化用戶端,請先呼叫 Client::builder() 取得適當的 ClientBuilder,然後對該建構工具呼叫 build() 來建立用戶端。
下列程式碼會建立採用預設設定的用戶端,這項設定的設計目的是滿足大多數用途的需求。
let client = SecretManagerService::builder().build().await?;
用戶端成功初始化後,即可用來發出 RPC:
use google_cloud_gax::paginator::Paginator as _;
let mut items = client
.list_locations()
.set_name(format!("projects/{project_id}"))
.by_page();
while let Some(page) = items.next().await {
let page = page?;
for location in page.locations {
println!("{}", location.name);
}
}
這個範例顯示對 list_locations 的呼叫,會傳回服務 (在本例中為 Secret Manager) 支援位置的相關資訊。
範例的輸出內容應如下所示:
projects/123456789012/locations/europe-west8
projects/123456789012/locations/europe-west9
projects/123456789012/locations/us-east5