使用 Namespaces API,您只需为每个租户指定唯一的命名空间字符串,即可轻松在租户之间对数据进行分区。您只需使用命名空间管理器为每个租户全局设置命名空间(而不是针对特定请求明确设置命名空间)。默认情况下,支持命名空间的 API 始终使用此当前命名空间。
Namespaces API 与 Google Workspace 集成,这样您就可以将 Google Workspace 网域用作当前命名空间。由于 Google Workspace 可让您将应用部署到您拥有的任何网域,因此您可以轻松为与 Google Workspace 账号关联的所有网域设置独一无二的命名空间。
[[["易于理解","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-20。"],[[["\u003cp\u003eThe Namespaces API in Google App Engine allows for data compartmentalization, primarily used to manage multitenant applications where a single application instance serves multiple client organizations.\u003c/p\u003e\n"],["\u003cp\u003eThis API uses the \u003ccode\u003eappengine.Namespace\u003c/code\u003e function and is integrated into several namespace-enabled APIs like Datastore, Memcache, Task Queue, and Search.\u003c/p\u003e\n"],["\u003cp\u003eBy setting a unique namespace string for each tenant via the namespace manager, data can be partitioned across tenants, enabling easier management and segregation of data.\u003c/p\u003e\n"],["\u003cp\u003eThe Namespaces API is integrated with Google Workspace, allowing for the use of a Google Workspace domain as the current namespace, enabling easy management across multiple domains.\u003c/p\u003e\n"],["\u003cp\u003eBeyond multitenancy, the Namespaces API can also be used to compartmentalize user information, separate admin and application data, create distinct datastore instances for testing and production, and run multiple apps within a single App Engine instance.\u003c/p\u003e\n"]]],[],null,["# Namespaces API for legacy bundled services\n\nThe Namespaces API in Google App Engine makes it easy to compartmentalize your Google App Engine data. This API is implemented via the [`appengine.Namespace` function](/appengine/docs/legacy/standard/go111/reference/latest#google_golang_org_appengine_Namespace), and is incorporated in certain [namespace-enabled APIs](#App_Engine_APIs_that_use_namespaces).\n| This API is supported for first-generation runtimes and can be used when [upgrading to corresponding second-generation runtimes](/appengine/docs/standard/\n| go\n| /services/access). If you are updating to the App Engine Go 1.12+ runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/go-differences) to learn about your migration options for legacy bundled services.\n\nWhen you set a namespace in the namespace manager, these APIs get the current namespace and use it globally. You can explicitly declare a namespace locally, but you need to exercise caution when explicitly declaring namespaces, because you could inadvertently create data leaks and other bugs. Any App Engine request can access any namespace, leaving the application to enforce an access control policy across namespaces.\n\nYou can use the Namespaces API to build a wide range of applications. One of the most compelling uses of this API is for multitenant applications, as described below.\n\nAbout multitenancy\n------------------\n\n*Multitenancy* is the name given to a software architecture in which one instance of an application, running on a remote server, serves many client organizations (also known as *tenants*).\n\nUsing a multitenant architecture simplifies administration and provisioning of tenants. You can provide a more streamlined, customized user experience, and also aggregate different silos of data under a single database schema. As a result, your applications become more scalable as well as more cost-effective as you scale. Data becomes easier to segregate and analyze across tenants because all tenants share the same database schema. Different user groups see custom content wrapped within a more efficient application.\n\nBuilding a multitenant application with the Namespaces API\n----------------------------------------------------------\n\nUsing the Namespaces API, you can easily partition data across tenants simply by specifying a unique namespace string for each tenant. You simply [set the namespace](/appengine/docs/legacy/standard/go111/multitenancy/multitenancy#Setting_the_current_namespace) for each tenant globally using the namespace manager (as opposed to setting it explicitly for a specific request). The [namespace-enabled APIs](#App_Engine_APIs_that_use_namespaces) always use this current namespace by default.\n\nThe Namespaces API is integrated with Google Workspace, allowing you to use your Google Workspace domain as the current namespace. Because Google Workspace lets you deploy your app to any domain that you own, you can easily [set unique namespaces](/appengine/docs/legacy/standard/go111/multitenancy/multitenancy#Setting_the_current_namespace) for all domains linked to your Google Workspace account.\n\nWhen designing multitenant applications, you need to prevent data from leaking across namespaces. For more information, please see [Avoiding Data Leaks](/appengine/docs/legacy/standard/go111/multitenancy/multitenancy#Avoiding_data_leaks).\n\nApp Engine APIs that use namespaces\n-----------------------------------\n\nApp Engine currently supports namespaces in the following APIs:\n\n- [Datastore](/appengine/docs/legacy/standard/go111/multitenancy/multitenancy#Using_namespaces_with_the_Datastore)\n- [Memcache](/appengine/docs/legacy/standard/go111/multitenancy/multitenancy#Using_namespaces_with_the_Memcache)\n- [Task Queue](/appengine/docs/legacy/standard/go111/multitenancy/multitenancy#Using_namespaces_with_the_Task_Queue)\n- [Search](/appengine/docs/legacy/standard/go111/multitenancy/multitenancy#Using_namespaces_with_Search)\n\nSample projects using namespaces\n--------------------------------\n\nTwo sample guestbook applications using namespaces are provided:\n\n- Python App Engine: [appengine-multitenancy](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/appengine/standard/multitenancy) -- A namespace-aware sample guestbook application.\n- Java App Engine: [appengine-gwtguestbook-namespaces-java](https://github.com/GoogleCloudPlatform/appengine-gwtguestbook-namespaces-java) -- A namespace-aware sample guestbook application using GWT.\n\nOther uses for the Namespace API\n--------------------------------\n\nWhile the Namespaces API enables multitenancy on App Engine, it has a number of other uses, including:\n\n- Compartmentalizing user information\n- Separating admin data from application data\n- Creating separate datastore instances for testing and production\n- Running multiple apps on a single App Engine instance"]]