本頁說明如何建立區域密鑰。密鑰包含一或多個密鑰版本,以及標籤和註解等中繼資料。密鑰的實際內容會儲存在密鑰版本中。
事前準備
-
設定驗證方法。
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
-
前往 Google Cloud 控制台的「Secret Manager」頁面。
-
在「Secret Manager」頁面中,按一下「區域性密鑰」分頁標籤,然後按一下「建立區域性密鑰」。
-
在「建立區域密鑰」頁面的「名稱」欄位中,輸入密鑰名稱。 密鑰名稱可以包含大小寫英文字母、數字、連字號和底線,名稱長度上限為 255 個字元。
-
輸入密鑰的值 (例如
abcd1234
)。密鑰值可採用任何格式,但不得超過 64 KiB。您也可以使用「上傳檔案」選項,上傳含有密鑰值的文字檔。這項動作會自動建立密鑰版本。 -
從「Region」(區域) 清單中,選擇要儲存區域密鑰的位置。
-
按一下「建立密鑰」。
- SECRET_ID:密鑰的 ID 或密鑰的完整 ID
- LOCATION:密鑰的 Google Cloud 位置
- LOCATION:密鑰的 Google Cloud 位置
- PROJECT_ID:專案 ID Google Cloud
- SECRET_ID:密鑰的 ID 或密鑰的完整 ID
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
REST
如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
詳情請參閱 Google Cloud 驗證說明文件中的「Authenticate for using REST」。
必要的角色
如要取得建立密鑰所需的權限,請要求管理員授予您專案、資料夾或機構的 Secret Manager 管理員 (
roles/secretmanager.admin
) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。建立區域性密鑰
您可以使用 Google Cloud 控制台、Google Cloud CLI、Secret Manager API 或 Secret Manager 用戶端程式庫建立密鑰。
控制台
gcloud
使用下方的任何指令資料之前,請先替換以下項目:
執行下列指令:
Linux、macOS 或 Cloud Shell
gcloud secrets create SECRET_ID \ --location=LOCATION
Windows (PowerShell)
gcloud secrets create SECRET_ID ` --location=LOCATION
Windows (cmd.exe)
gcloud secrets create SECRET_ID ^ --location=LOCATION
REST
使用任何要求資料之前,請先替換以下項目:
HTTP 方法和網址:
POST https://secretmanager.LOCATION.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/secrets?secretId=SECRET_ID
JSON 要求主體:
{}
如要傳送要求,請選擇以下其中一個選項:
curl
將要求主體儲存在名為
request.json
的檔案中,然後執行下列指令:curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://secretmanager.LOCATION.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/secrets?secretId=SECRET_ID"PowerShell
將要求主體儲存在名為
request.json
的檔案中,然後執行下列指令:$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://secretmanager.LOCATION.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/secrets?secretId=SECRET_ID" | Select-Object -Expand Content您應該會收到如下的 JSON 回應:
{ "name": "projects/PROJECT_ID/locations/LOCATION/secrets/SECRET_ID", "createTime": "2024-03-25T08:24:13.153705Z", "etag": "\"161477e6071da9\"" }
Go
如要執行這段程式碼,請先設定 Go 開發環境,並安裝 Secret Manager Go SDK。在 Compute Engine 或 GKE 上,您必須使用 cloud-platform 範圍進行驗證。
Java
如要執行這段程式碼,請先設定 Java 開發環境,並安裝 Secret Manager Java SDK。在 Compute Engine 或 GKE 上,您必須使用 cloud-platform 範圍進行驗證。
Node.js
如要執行這段程式碼,請先設定 Node.js 開發環境,並安裝 Secret Manager Node.js SDK。在 Compute Engine 或 GKE 上,您必須使用 cloud-platform 範圍進行驗證。
Python
如要執行這段程式碼,請先設定 Python 開發環境,然後安裝 Secret Manager Python SDK。在 Compute Engine 或 GKE 上,您必須使用 cloud-platform 範圍進行驗證。
新增密鑰版本
Secret Manager 會使用密鑰版本,自動為密鑰資料建立版本。存取、刪除、停用及啟用等金鑰作業,適用於特定密鑰版本。透過 Secret Manager,您可以將密鑰與特定版本 (例如
42
) 或動態別名 (例如latest
) 建立關聯。詳情請參閱「新增密鑰版本」。存取密鑰版本
如要存取特定密鑰版本的私密資料以順利完成驗證,請參閱「存取區域性密鑰版本」。
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-14 (世界標準時間)。
-