本頁將引導您完成使用 Google Cloud CLI、用戶端程式庫和 Spanner 驅動程式的必要設定步驟。
如要使用 Google Cloud 控制台操作 Spanner,請參閱使用控制台的快速入門導覽課程。
必要的角色
如要取得使用及操作 Spanner 資料庫所需的權限,請要求管理員授予下列 IAM 角色:
-
讀取及寫入資料:
執行個體上的 Cloud Spanner 資料庫使用者 (
roles/spanner.databaseUser
) -
資料庫的唯讀存取權:
執行個體上的 Cloud Spanner 資料庫讀取者 (
roles/spanner.databaseReader
)
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。
設定 Google Cloud 專案
-
前往 Google Cloud 控制台的「專案」頁面。
- 建立新 Google Cloud 專案,或按一下專案名稱開啟現有專案。
-
開啟終端機視窗,將專案設為 Google Cloud CLI 的預設專案,並將
MY_PROJECT_ID
替換為專案 ID (而非專案名稱):gcloud config set project MY_PROJECT_ID
-
Make sure that billing is enabled for your Google Cloud project.
您也可以瞭解 Spanner 定價。
-
在專案啟用 Cloud Spanner API。
注意:如果您使用其他專案中的服務帳戶存取 Spanner 執行個體,則必須在這兩個專案中啟用 Spanner API。
設定驗證方法
Select the tabs for how you plan to access the API:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
- 開始使用 C++ 版 Spanner
- 開始使用 C#版 Spanner
- 開始使用 Go 中的 Spanner
- 開始使用 Java 版 Spanner
- 開始透過 JDBC 使用 Spanner
- 開始透過 Node.js 使用 Spanner
- 開始透過 PHP 使用 Spanner
- 開始透過 Python 使用 Spanner
- 開始透過 Ruby 使用 Spanner
- 開始使用適用於 REST 的 Spanner
- 開始使用 gcloud 中的 Spanner
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.
如要設定 gcloud CLI,讓它使用服務帳戶模擬功能向 Google API 進行驗證,而不是使用您的使用者憑證,請執行下列指令:
gcloud config set auth/impersonate_service_account SERVICE_ACCT_EMAIL
詳情請參閱「服務帳戶模擬」。
Terraform
如要在本機開發環境中使用 Terraform 程式碼,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
詳情請參閱 Set up authentication for a local development environment。
用戶端程式庫
如要在本機開發環境中使用用戶端程式庫,請安裝並初始化 gcloud CLI,然後使用您的使用者憑證設定應用程式預設憑證。
詳情請參閱 Set up authentication for a local development environment。
如要設定本機 ADC 檔案,使用服務帳戶模擬功能向 Google API 進行驗證,而非使用您的使用者憑證,請執行下列指令:
gcloud auth application-default login --impersonate-service-account=SERVICE_ACCT_EMAIL
詳情請參閱「服務帳戶模擬」。
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」。
您可以使用服務帳戶模擬功能,為 REST API 要求產生存取權杖。 詳情請參閱「模擬服務帳戶」。
執行 Google Cloud CLI
完成開發環境和驗證程序的設定後,請執行 gcloud
指令列工具,進而與 Spanner 互動:
gcloud spanner instance-configs list
您應該會看到專案可存取的 Spanner 執行個體設定清單,包括單區域、雙區域和多區域設定。詳情請參閱執行個體總覽。
您已完成設定!
後續步驟
瞭解如何使用 Cloud 用戶端程式庫和驅動程式,建立 Spanner 執行個體、資料庫、資料表和索引,然後在 Spanner 中儲存、查詢和讀取資料。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-15 (世界標準時間)。