Per ottenere le autorizzazioni necessarie per utilizzare e interagire con i database Spanner, chiedi all'amministratore di concederti i seguenti ruoli IAM:
Crea un nuovo Google Cloud progetto o aprine uno esistente facendo clic sul nome del progetto.
...vedi le linee guida per l'assegnazione dei nomi
Apri una finestra del terminale e imposta il tuo progetto come progetto predefinito per
Google Cloud CLI, sostituendo
MY_PROJECT_ID con il tuo ID progetto (non il nome del progetto):
Per configurare gcloud CLI in modo che utilizzi la simulazione dell'identità dei service account per l'autenticazione alle
API di Google, anziché le tue credenziali utente, esegui questo comando:
Per utilizzare il codice Terraform in un ambiente di sviluppo locale, installa e inizializza
gcloud CLI, quindi configura le credenziali predefinite dell'applicazione con le tue credenziali
utente.
Per utilizzare le librerie client in un ambiente di sviluppo locale, installa e inizializza
gcloud CLI, quindi configura le credenziali predefinite dell'applicazione e con le tue credenziali
utente.
Per configurare il file ADC locale in modo che utilizzi la simulazione dell'identità dei service account per l'autenticazione alle API di Google, anziché le credenziali utente, esegui il seguente comando:
Puoi utilizzare l'imitazione dell'account di servizio per generare un token di accesso per le richieste API REST.
Per saperne di più, vedi
Service account con rappresentazione.
Esegui Google Cloud CLI
Ora che hai configurato l'ambiente di sviluppo e l'autenticazione, esegui lo strumento a riga di comando
gcloud per interagire con
Spanner:
gcloudspannerinstance-configslist
Dovresti visualizzare un elenco delle configurazioni di istanza Spanner a cui
il tuo progetto può accedere, incluse le configurazioni regionali, a due regioni e multiregionali. Per saperne di più, consulta la panoramica
delle istanze.
Hai completato la configurazione.
Passaggi successivi
Scopri come utilizzare le librerie client e i driver di Cloud per creare un'istanza, un database, tabelle e indici Spanner. Quindi archivia,
esegui query e leggi i dati in Spanner.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-03 UTC."],[],[],null,["# Set up your environment\n\nThis page walks you through the setup steps required to use the Cloud Spanner API\nwith the [Google Cloud CLI](/sdk/gcloud), [client\nlibraries](/spanner/docs/reference/libraries), and [Spanner\ndrivers](/spanner/docs/drivers-overview).\n\nIf you want to use Spanner with the Google Cloud console, see\n[Quickstart using the console](/spanner/docs/create-query-database-console).\n\nRequired roles\n--------------\n\n\nTo get the permissions that\nyou need to use and interact with Spanner databases,\n\nask your administrator to grant you the\nfollowing IAM roles:\n\n- Read and write data: [Cloud Spanner Database User](/iam/docs/roles-permissions/spanner#spanner.databaseUser) (`roles/spanner.databaseUser`) on the instance\n- Read-only access to databases: [Cloud Spanner Database Reader](/iam/docs/roles-permissions/spanner#spanner.databaseReader) (`roles/spanner.databaseReader`) on the instance\n\n\nFor more information about granting roles, see [Manage access to projects, folders, and organizations](/iam/docs/granting-changing-revoking-access).\n\n\nYou might also be able to get\nthe required permissions through [custom\nroles](/iam/docs/creating-custom-roles) or other [predefined\nroles](/iam/docs/roles-overview#predefined).\n\nSet up a Google Cloud project\n-----------------------------\n\n1.\n Go to the **Projects** page in the Google Cloud console.\n\n\n [Go to the Projects page](https://console.cloud.google.com/project)\n2. Create a new Google Cloud project, or open an existing project by clicking on the project name.\n| **Important:** The name you use must be between 4 and 30 characters. When you type the name, the form will suggest a project ID, which you can edit. The project ID you use must be between 6 and 30 characters, with a lowercase letter as the first character. You can use a dash, lowercase letter, or digit for the remaining characters, but the last character cannot be a dash. You should be aware that some resource identifiers (such as project IDs) might be retained beyond the life of your project. For this reason, avoid storing sensitive information in resource identifiers.\n...see naming guidelines\n3.\n Open a terminal window, and set your project as the default project for\n the Google Cloud CLI, replacing\n `MY_PROJECT_ID` with your project ID (not your project name):\n\n gcloud config set project \u003cvar translate=\"no\"\u003eMY_PROJECT_ID\u003c/var\u003e\n\n4. [Verify that billing is enabled for your Google Cloud project](/billing/docs/how-to/verify-billing-enabled#confirm_billing_is_enabled_on_a_project).\nYou can also [learn about Spanner pricing](/spanner/pricing).\n5.\n Enable the Cloud Spanner API for the project.\n\n\n [Enable the Cloud Spanner API](https://console.cloud.google.com/flows/enableapi?apiid=spanner.googleapis.com)\n\n\n Note: If you use a service account in a different project to access your\n Spanner instance, you need to enable the Spanner\n API in both projects.\n\nSet up authentication\n---------------------\n\nSelect the tabs for how you plan to access the API: \n\n### Console\n\n\nWhen you use the Google Cloud console to access Google Cloud services and\nAPIs, you don't need to set up authentication.\n\n### gcloud\n\n\n[Install](/sdk/docs/install) the Google Cloud CLI.\n\nAfter installation,\n[initialize](/sdk/docs/initializing) the Google Cloud CLI by running the following command:\n\n```bash\ngcloud init\n```\n\n\nIf you're using an external identity provider (IdP), you must first\n[sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n\nTo set up the gcloud CLI to use service account impersonation to authenticate to\nGoogle APIs, rather than your user credentials, run the following command: \n\n```bash\ngcloud config set auth/impersonate_service_account SERVICE_ACCT_EMAIL\n```\n\n\nFor more information, see [Service account impersonation](/spanner/docs/authentication#sa-impersonation).\n\n### Terraform\n\n\nTo use Terraform code in a local development environment, install and initialize the\ngcloud CLI, and then set up Application Default Credentials with your user\ncredentials.\n\n1. [Install](/sdk/docs/install) the Google Cloud CLI.\n2. If you're using an external identity provider (IdP), you must first [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n3. If you're using a local shell, then create local authentication credentials for your user account: \n\n```bash\ngcloud auth application-default login\n```\n4. You don't need to do this if you're using Cloud Shell.\n5. 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](/iam/docs/workforce-log-in-gcloud).\n\n\nFor more information, see\n[Set up authentication for a local development environment](/spanner/docs/authentication#local-development).\n\n### Client libraries\n\n\nTo use client libraries in a local development environment, install and initialize the\ngcloud CLI, and then set up Application Default Credentials with your user\ncredentials.\n\n1. [Install](/sdk/docs/install) the Google Cloud CLI.\n2. If you're using an external identity provider (IdP), you must first [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n3. If you're using a local shell, then create local authentication credentials for your user account: \n\n```bash\ngcloud auth application-default login\n```\n4. You don't need to do this if you're using Cloud Shell.\n5. 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](/iam/docs/workforce-log-in-gcloud).\n\n\nFor more information, see\n[Set up authentication for a local development environment](/spanner/docs/authentication#local-development).\n\n\nTo set up your local ADC file to use service account impersonation to authenticate to\nGoogle APIs, rather than your user credentials, run the following command: \n\n```bash\ngcloud auth application-default login --impersonate-service-account=SERVICE_ACCT_EMAIL\n```\n\n\nFor more information, see [Service account impersonation](/spanner/docs/authentication#sa-impersonation).\n\n### REST\n\n\nTo use the REST API in a local development environment, you use the credentials you provide to\nthe gcloud CLI.\n\n1. [Install](/sdk/docs/install) the Google Cloud CLI.\n2. If you're using an external identity provider (IdP), you must first [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n\nFor more information, see\n[Authenticate for using REST](/docs/authentication/rest)\nin the Google Cloud authentication documentation.\n\n\nYou can use service account impersonation to generate an access token for REST API requests.\nFor more information, see\n[Impersonated service account](/docs/authentication/rest#impersonated-sa).\n\nRun the Google Cloud CLI\n------------------------\n\nNow that you've set up your development environment and authentication, run the\n[`gcloud` command-line](/spanner/docs/gcloud-spanner) tool to interact with\nSpanner: \n\n gcloud spanner instance-configs list\n\nYou should see a list of the Spanner instance configurations that\nyour project can access, including regional, dual-region, and multi-region\nconfigurations. For more information, see the [Instances\noverview](/spanner/docs/instances).\n\nYou've completed the setup!\n\nWhat's next\n-----------\n\nLearn how to use the Cloud Client Libraries and drivers to create a\nSpanner instance, database, tables, and indexes. Then store,\nquery, and read data in Spanner.\n\n- [Getting started with Spanner in C++](/spanner/docs/getting-started/cpp)\n- [Getting started with Spanner in C#](/spanner/docs/getting-started/csharp)\n- [Getting started with Spanner in Go](/spanner/docs/getting-started/go)\n- [Getting started with Spanner in Java](/spanner/docs/getting-started/java)\n- [Getting started with Spanner in JDBC](/spanner/docs/getting-started/jdbc)\n- [Getting started with Spanner in Node.js](/spanner/docs/getting-started/nodejs)\n- [Getting started with Spanner in PHP](/spanner/docs/getting-started/php)\n- [Getting started with Spanner in Python](/spanner/docs/getting-started/python)\n- [Getting started with Spanner in Ruby](/spanner/docs/getting-started/ruby)\n- [Getting started with Spanner in REST](/spanner/docs/getting-started/rest)\n- [Getting started with Spanner in gcloud](/spanner/docs/getting-started/gcloud)"]]