Pour obtenir les autorisations nécessaires pour utiliser les bases de données Spanner et interagir avec elles, demandez à votre administrateur de vous accorder les rôles IAM suivants :
Créez un projet Google Cloud ou ouvrez un projet existant en cliquant sur son nom.
...Afficher les consignes d'attribution de noms
Ouvrez une fenêtre de terminal et définissez votre projet comme projet par défaut pour la Google Cloud CLI. Pour ce faire, remplacez MY_PROJECT_ID par l'ID de votre projet (et non pas son nom) dans la commande suivante :
Remarque : Si vous utilisez un compte de service dans un autre projet pour accéder à votre instance Spanner, vous devez activer l'API Spanner dans les deux projets.
Configurer l'authentification
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
Installez la Google Cloud CLI.
Une fois que la Google Cloud CLI est installée, initialisez-la en exécutant la commande suivante :
Pour configurer la gcloud CLI afin qu'elle utilise l'emprunt d'identité d'un compte de service pour s'authentifier auprès des Google APIs plutôt que d'utiliser vos identifiants utilisateur, exécutez la commande suivante :
Pour utiliser le code Terraform dans un environnement de développement local, installez et initialisez la gcloud CLI, puis configurez le service Identifiants par défaut de l'application avec vos identifiants utilisateur.
Pour utiliser des bibliothèques clientes dans un environnement de développement local, installez et initialisez la gcloud CLI, puis configurez le service Identifiants par défaut de l'application avec vos identifiants utilisateur.
Pour configurer votre fichier ADC local afin d'utiliser l'emprunt d'identité d'un compte de service pour vous authentifier auprès des Google APIs plutôt que vos identifiants utilisateur, exécutez la commande suivante :
Pour en savoir plus, consultez la section S'authentifier pour utiliser REST dans la documentation sur l'authentification Google Cloud .
Vous pouvez utiliser l'emprunt d'identité d'un compte de service pour générer un jeton d'accès pour les requêtes API REST.
Pour en savoir plus, consultez la section Compte de service dont l'identité est empruntée.
Exécuter la Google Cloud CLI
Maintenant que vous avez configuré l'environnement de développement et l'authentification, exécutez l'outil de ligne de commande gcloud pour interagir avec Spanner :
gcloudspannerinstance-configslist
La liste des configurations d'instance Spanner auxquelles votre projet a accès doit s'afficher. Elle inclut les configurations régionales, birégionales et multirégionales. Pour en savoir plus, consultez la présentation des instances.
Vous avez terminé la configuration.
Étapes suivantes
Apprenez à utiliser les bibliothèques clientes et les pilotes Cloud pour créer une instance, une base de données, des tables et des index Spanner. Ensuite, stockez, interrogez et lisez des données dans Spanner.
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/05 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/09/05 (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)"]]