如何指定Google Cloud功能的区域?

Dum*_*ius 4 latency region google-cloud-platform gcloud google-cloud-functions

我目前正在使用Google Cloud Function来构建我的restful API.但是,我发现它很慢,因为我的Google-Cloud-Function服务器位于"us-central",而我的服务是在亚洲.

我尝试将我的Google项目的默认区域更改为"asia-west-1"并重新启动云功能 - 我按照此处列出的步骤进行了操作- 但不幸的是,它仍处于"us-central"状态.如何更改功能区域?

小智 6

Google Cloud Functions目前似乎仅在us-central1区域提供.如果我转到"创建功能"(https://console.cloud.google.com/functions/add),"区域"下拉列表只有一个选项,它是us-central1.


jub*_*0bs 6

没有“asia-west-1”这样的区域

Google Cloud Platform 上没有“asia-west-1”这样的区域。在撰写本文时,GCP 在亚洲提供以下区域

  • asia-east1
  • asia-northeast1
  • asia-south1
  • asia-southeast1

但是,asia-northeast1目前是亚洲唯一可用于 Google Cloud Functions 的 GCP 区域:

$ gcloud functions regions list
NAME
projects/xxxxxxxx/locations/europe-west1
projects/xxxxxxxx/locations/us-east1
projects/xxxxxxxx/locations/us-central1
projects/xxxxxxxx/locations/asia-northeast1
Run Code Online (Sandbox Code Playgroud)

如何指定 Google Cloud Function 的区域

使用 Google Cloud Console

区域设置很容易错过。它实际上隐藏在名为“更多”的手风琴后面:

手风琴

单击它会显示区域下拉列表(以及其他高级设置):

下拉菜单

使用 gcloud

只需使用--region标志指定四个可用区域之一。

最小工作示例

在假设你有

$ tree .
.
??? index.js

0 directories, 1 file
$ cat index.js 
exports.hello = (req, res) => {

  res.send(`Hello World!`); 

}
Run Code Online (Sandbox Code Playgroud)

然后运行

gcloud functions deploy hello --region asia-northeast1 --trigger-http
Run Code Online (Sandbox Code Playgroud)

应该将功能部署hello到区域asia-northeast1

亚洲-东北1

  • 有没有办法通过节点js来做到这一点?当我尝试使用 'firebase deploy --region europe-west1' 进行部署时,给了我“未知选项 --region”。有什么建议? (2认同)
  • 显然有一种方法,刚刚在此链接上找到:https://firebase.google.com/docs/functions/manage-functions#modify 现在将尝试并让您知道。 (2认同)
  • 抱歉忘了告诉你,是的,效果很好。只需使用functions.region('europe-west1').https ...它将在所需的位置创建。塔克斯 (2认同)

Ada*_*olt 5

如果您使用的是 Firebase SDK:

取自文档 - https://firebase.google.com/docs/functions/manage-functions#modify

// before
const functions = require('firebase-functions');

exports.webhook = functions
    .https.onRequest((req, res) => {
            res.send("Hello");
    });

// after
const functions = require('firebase-functions');

exports.webhookAsia = functions
    .region('asia-northeast1')
    .https.onRequest((req, res) => {
            res.send("Hello");
    });
Run Code Online (Sandbox Code Playgroud)