在身份和 API 访问权限部分,设置访问权限范围以提供 Datastore 的访问权限。点击允许所有 Cloud API 的全面访问权限,授予对所有 Google Cloud API 的访问权限;或者依次点击针对每个 API 设置访问权限、Datastore 的下拉列表,然后点击已启用来授予对 Datastore 的访问权限。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-19。"],[[["\u003cp\u003eThis document explains how to access a Firestore in Datastore mode database from various platforms, including App Engine, Compute Engine, and external applications.\u003c/p\u003e\n"],["\u003cp\u003eApp Engine apps can access a Datastore mode database in the same project by default, using the App Engine default service account which has Project Editor IAM role.\u003c/p\u003e\n"],["\u003cp\u003eCompute Engine apps also have default access to a Datastore mode database within the same project, leveraging the Compute Engine default service account and its Project Editor IAM role.\u003c/p\u003e\n"],["\u003cp\u003eAccessing the database from an external platform requires the creation of a service account with appropriate permissions, such as the Cloud Datastore User role, and a JSON key file for authentication.\u003c/p\u003e\n"],["\u003cp\u003eWhile a free quota is available for Firestore in Datastore mode, enabling billing is necessary for using more resources than provided by this free quota.\u003c/p\u003e\n"]]],[],null,["# Access your database\n\nThis page describes how to access a Firestore in Datastore mode database from different\nplatforms.\n\nBefore you begin\n----------------\n\nThis document assumes that you've already created a Datastore mode database.\nIf you haven't created a database, follow the instructions in the [Firestore in Datastore mode\nQuickstart](/datastore/docs/store-query-data).\n\nAccess your database from App Engine\n------------------------------------\n\nTo get started with Datastore mode and App Engine, see one of the\nfollowing language-specific pages:\n\n### Datastore mode permissions for App Engine\n\nApp Engine apps can access a Datastore mode database in the same\nproject by default. Each App Engine app uses an [App Engine default service\naccount](/appengine/docs/standard/python/service-account) to manage access to Google Cloud\nservices such as Firestore. By default, the App Engine default\nservice account has the Project Editor [IAM role](/iam/docs/overview#roles),\nwhich includes full read and write access to Datastore mode.\n\nYou can [modify the IAM permissions of your App Engine default service\naccount](/appengine/docs/standard/python/service-account#changing_service_account_permissions_),\nbut your app might lose access to Firestore unless you assign an IAM role\nwith the [required Firestore\npermissions](/datastore/docs/access/iam#required_permissions). The [Datastore Owner](/datastore/docs/access/iam#iam_roles) and [Datastore User](/datastore/docs/access/iam#iam_roles) IAM roles, for\nexample, grant read and write access to Firestore in Datastore mode.\n\nIf you disable or delete your App Engine default service account, your\nApp Engine app will lose access to your Datastore mode database.\nIf you disabled your App Engine service account, you can re-enable it,\nsee [enabling a service account](https://cloud.google.com/iam/docs/creating-managing-service-accounts#enabling).\nIf you deleted your App Engine service account within the last 30 days,\nyou can restore your service account, see\n[undeleting a service account](https://cloud.google.com/iam/docs/creating-managing-service-accounts#undeleting).\n\nAccess your database from a Compute Engine instance\n---------------------------------------------------\n\nThis section shows how to activate and access a Datastore mode\ndatabase from a [Compute Engine](/compute) VM instance in a new or existing project.\n\n### Datastore mode permissions for Compute Engine\n\nCompute Engine apps can access a Datastore mode database in the same\nproject by default. Each Compute Engine app uses an [Compute Engine default service\naccount](/compute/docs/access/service-accounts#default_service_account) to manage access to Google Cloud services such as\nFirestore. By default, the Compute Engine default service account has\nthe Project Editor [IAM role](/iam/docs/overview#roles), which\nincludes full read and write access to Datastore mode.\n\nTo access your database from a Compute Engine instance, complete the following steps:\n\n1. Enable the Google Compute Engine API for your project. \n [Enable the Compute Engine API](https://console.cloud.google.com/flows/enableapi?apiid=compute.googleapis.com)\n2.\n [Verify that billing is enabled for your Google Cloud project](/billing/docs/how-to/verify-billing-enabled#confirm_billing_is_enabled_on_a_project).\n\n3. Create a Compute Engine instance.\n\n### Console\n\n1. In the Google Cloud console, go to the [**VM Instances**](https://console.cloud.google.com/compute/instances) page.\n2. Click the **Create instance** button.\n3. In the **Identity and API access** section, set **Access scopes** to provide access for Datastore. Either click **Allow full access to all Cloud APIs** to grant access to all Google Cloud APIs, or click **Set access for each API** , click the dropdown for **Datastore** , and then click **Enabled** to grant access to Datastore.\n4. Click the **Create** button to create the VM instance.\n5. Confirm that your [instance is running](/compute/docs/instances/checking-instance-status).\n6. To use this new Compute Engine VM instance, [connect](/compute/docs/instances/connecting-to-instance) to it using your browser.\n\n### gcloud\n\n1. If you haven't already done this, [install](/sdk/docs/install) the Google Cloud CLI and [set up `gcloud compute`](/compute/docs/gcloud-compute).\n2. Add a Compute Engine VM instance and start it, following the instructions for\n [starting an instance](/compute/docs/instances/creating-and-starting-an-instance#startinstancegcloud)\n in the Compute Engine documentation. Specify the project ID, the\n VM instance name, and either the `cloud-platform` or the `datastore`\n [scope](/compute/docs/access/service-accounts#accesscopesiam)\n as shown in the following example.\n\n export PROJECT_ID=[YOUR_PROJECT_ID]\n export INSTANCE_NAME=[YOUR_INSTANCE_NAME]\n gcloud compute instances create $INSTANCE_NAME --project $PROJECT_ID --scopes datastore\n\n Replace `[YOUR_PROJECT_ID]` with the ID of the project you created\n previously and `[YOUR_INSTANCE_NAME]` with the name you want\n to use for your VM instance.\n3. Confirm that your [instance is running](/compute/docs/instances/checking-instance-status).\n\n4. To use this new VM instance, [connect](/compute/docs/instances/connecting-to-instance)\n to the VM.\n\nAt this point all services and authorizations are configured for your\nproject and you can start [writing code](/datastore/docs/datastore-api-tutorial) or [exploring the API](https://developers.google.com/apis-explorer/#search/datastore/).\n\nAccess your database from another platform\n------------------------------------------\n\nThis section shows how to access your Datastore mode database from\nan external application running on a platform outside of Google Cloud.\n\nFirst, create a service account:\n\n1. In the Google Cloud console, go to the **Create service account** page.\n\n [Go to Create service account](https://console.cloud.google.com/projectselector/iam-admin/serviceaccounts/create?supportedpurview=project)\n2. Select a project.\n\n3. In the **Service account name** field, enter a name. The\n Google Cloud console fills in the **Service account ID** field based on this\n name.\n\n4. Optional: In the **Service account description** field, enter a description.\n\n5. Click **Create**.\n\n6. Click the **Select a role** field.\n\n Under **All roles** , select a role that grants access to your database, such\n as **Datastore** \\\u003e **Cloud Datastore User**.\n7. Click **Continue**.\n\n8. Click **Done** to finish creating the service account.\n\n Do not close your browser window. You will use it in the next procedure.\n\nThen create a service account key:\n\n1. In the Google Cloud console, click the email address for the service account that you created.\n2. Click **Keys**.\n3. Click **Add key** , then **Create new key**.\n4. Click **Create**. A JSON key file is downloaded to your computer.\n5. Click **Close**.\n\nUse this service account to configure credentials for your application code as\ndescribed in [Providing service account credentials](/docs/authentication/production#providing_service_account_credentials).\n\nQuotas and billing\n------------------\n\nA certain amount of free quota is available, as described in\n[Pricing and Quota](/datastore/docs/pricing). This means you aren't required to\nenable billing to get started or to use Firestore in Datastore mode up to the free\nquota limits. However, if you need more resources than is provided by the free\nquota, you must enable billing.\n\nWhat's next\n-----------\n\n- Learn about [setting up authentication with client libraries](/datastore/docs/reference/libraries#setting_up_authentication).\n- Understand the [how your credentials are used by client libraries](/docs/authentication#adc).\n\nTo enable billing, see\n[Enable billing for a project](/billing/docs/how-to/modify-project#enable_billing_for_a_project)."]]