本頁將引導您完成使用 Cloud Spanner API 的必要設定步驟,並搭配使用 Google Cloud CLI、用戶端程式庫和 Spanner 驅動程式。
如要搭配 Google Cloud 控制台使用 Spanner,請參閱使用主控台的快速入門導覽課程。
設定 Google Cloud 專案
-
前往 Google Cloud 主控台的「Projects」頁面。
- 建立新 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
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.
To set up the gcloud CLI to use service account impersonation to authenticate to Google APIs, rather than your user credentials, run the following command:
gcloud config set auth/impersonate_service_account SERVICE_ACCT_EMAIL
For more information, see Service account impersonation.
Terraform
To use Terraform code in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
-
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.
For more information, see Set up authentication for a local development environment.
Client libraries
To use client libraries in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
-
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.
For more information, see Set up authentication for a local development environment.
To set up your local ADC file to use service account impersonation to authenticate to Google APIs, rather than your user credentials, run the following command:
gcloud auth application-default login --impersonate-service-account=SERVICE_ACCT_EMAIL
For more information, see Service account impersonation.
REST
To use the REST API in a local development environment, you use the credentials you provide to the 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.
For more information, see Authenticate for using REST in the Google Cloud authentication documentation.
You can use service account impersonation to generate an access token for REST API requests. For more information, see Impersonated service account.
執行 Google Cloud CLI
完成開發環境和驗證程序的設定後,請執行 gcloud
指令列工具,進而與 Spanner 互動:
gcloud spanner instance-configs list
您應該會看到專案可存取的 Spanner 執行個體設定清單,包括地區、雙區域和多區域設定。詳情請參閱「執行個體總覽」。
您已完成設定!
後續步驟
瞭解如何使用 Cloud 用戶端程式庫和驅動程式建立 Spanner 執行個體、資料庫、資料表和索引,然後在 Spanner 中儲存、查詢和讀取資料。
- 開始使用 C++ 版 Spanner
- 開始使用 C#版 Spanner
- 開始使用 Go 語言的 Spanner
- 開始使用 Java 版 Spanner
- 開始使用 JDBC 中的 Spanner
- 在 Node.js 中開始使用 Spanner
- 開始使用 PHP 中的 Spanner
- 開始使用 Python 中的 Spanner
- 開始使用 Ruby 版 Spanner
- 開始使用 Spanner:REST 語言專篇
- 在 gcloud 中開始使用 Spanner