importcom.google.auth.oauth2.GoogleCredentials;importcom.google.cloud.firestore.Firestore;importcom.google.cloud.firestore.FirestoreOptions;/** * Demonstrate how to set a regional endpoint. */publicclassRegionalEndpointSnippets{/** * Create a client with a regional endpoint. **/publicFirestoreregionalEndpoint(StringprojectId,Stringendpoint)throwsException{FirestoreOptionsfirestoreOptions=FirestoreOptions.newBuilder().setProjectId(projectId).setCredentials(GoogleCredentials.getApplicationDefault())// set endpoint like nam5-firestore.googleapis.com:443.setHost(endpoint).build();FirestoredbWithEndpoint=firestoreOptions.getService();returndbWithEndpoint;}}
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[],[],null,["# Configure data locality with locational endpoints\n=================================================\n\nThis page describes how to configure the Firestore client libraries\nto use a locational endpoint.\n\nWhen you use Firestore client libraries, you can use either of the following endpoints:\n\n- **Global endpoint** : By default, the Firestore client libraries send API requests to a global\n service endpoint named `firestore.googleapis.com`. The global service endpoint\n routes the request to your database. During routing, a request\n might pass through a locational service endpoint in a location that's different\n from your database location.\n\n- **Locational endpoint** : A locational endpoint enforces regional restrictions, ensuring that data is stored and processed in a specified region.\n To guarantee that the service endpoint processes your app's Firestore\n requests in the same region as your database, specify a *locational endpoint*\n in the client library.\n\nSet a locational endpoint\n-------------------------\n\nThe following examples show how to set a locational endpoint when you initialize a\nFirestore client. Setting a locational endpoint other than where your data\nresides might result in a `PermissionDeniedError` error.\n**Note:** You can set locational endpoints in the server client libraries, namely C#, Go, Java, Node.js, PHP, Python, and Ruby. The mobile and web SDKs, namely, Android, IOS, and Web are not supported. \n\n### Java\n\n\nTo authenticate to Firestore, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import com.google.auth.oauth2.https://cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.GoogleCredentials.html;\n import com.google.cloud.firestore.https://cloud.google.com/java/docs/reference/google-cloud-firestore/latest/com.google.cloud.firestore.Firestore.html;\n import com.google.cloud.firestore.https://cloud.google.com/java/docs/reference/google-cloud-firestore/latest/com.google.cloud.firestore.FirestoreOptions.html;\n\n\n /**\n * Demonstrate how to set a regional endpoint.\n */\n public class RegionalEndpointSnippets {\n\n /**\n * Create a client with a regional endpoint.\n **/\n public https://cloud.google.com/java/docs/reference/google-cloud-firestore/latest/com.google.cloud.firestore.Firestore.html regionalEndpoint(String projectId, String endpoint) throws Exception {\n https://cloud.google.com/java/docs/reference/google-cloud-firestore/latest/com.google.cloud.firestore.FirestoreOptions.html firestoreOptions =\n https://cloud.google.com/java/docs/reference/google-cloud-firestore/latest/com.google.cloud.firestore.FirestoreOptions.html.newBuilder()\n .setProjectId(projectId)\n .setCredentials(https://cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.GoogleCredentials.html.https://cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.GoogleCredentials.html#com_google_auth_oauth2_GoogleCredentials_getApplicationDefault__())\n // set endpoint like nam5-firestore.googleapis.com:443\n .setHost(endpoint)\n .build();\n https://cloud.google.com/java/docs/reference/google-cloud-firestore/latest/com.google.cloud.firestore.Firestore.html dbWithEndpoint = firestoreOptions.getService();\n\n return dbWithEndpoint;\n }\n\n }\n\n### Python\n\n\nTo authenticate to Firestore, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n ENDPOINT = \"nam5-firestore.googleapis.com\"\n client_options = ClientOptions(api_endpoint=ENDPOINT)\n db = firestore.Client(client_options=client_options)\n\n cities_query = db.collection(\"cities\").limit(2).get()\n for r in cities_query:\n print(r)\n\n\u003cbr /\u003e\n\n### Locational endpoint semantics\n\nFirestore supports locational endpoints for both region and multi-region locations.\n\nUse the following format to define locational endpoints: \n\n### Java\n\n \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eREGION_NAME\u003c/span\u003e\u003c/var\u003e-firestore.googleapis.com:443\n\nMake sure that the port number is defined along with the endpoint.\n\n### Python\n\n \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eREGION_NAME\u003c/span\u003e\u003c/var\u003e-firestore.googleapis.com\n\n### Go\n\n \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-nx\"\u003eREGION_NAME\u003c/span\u003e\u003c/var\u003e-firestore.googleapis.com:443\n\nMake sure that the port number is defined along with the endpoint.\n\nReplace \u003cvar translate=\"no\"\u003eREGION_NAME\u003c/var\u003e with the name of a regional or multi-regional hostnames.\n\nSome examples of hostnames are:\n\n- `eur3-firestore.googleapis.com`\n- `nam5-firestore.googleapis.com`\n- `europe-west6-firestore.googleapis.com`\n- `asia-northeast2-firestore.googleapis.com`\n\nFor a complete list of multi-regional and regional hostnames, see [Firestore locations](/firestore/docs/locations).\n\nRestrict global API endpoint usage\n----------------------------------\n\nTo help enforce the use of regional endpoints, use the\n`constraints/gcp.restrictEndpointUsage` organization policy constraint to block\nrequests to the global API endpoint. For more information, see\n[Restricting endpoint usage](/assured-workloads/docs/restrict-endpoint-usage).\n\nWhat's next\n-----------\n\n- Learn about the Firestore [data model](/firestore/docs/data-model).\n- [Best practices](/firestore/docs/best-practices) for using Firestore."]]