简体   繁体   English

GCP Cloud Functions Gen 2 - 无法通过 NodeJs 或 gcloud 调用

[英]GCP Cloud Functions Gen 2 - Cannot call through NodeJs or gcloud

I created a gen 2 cloud function on my project "project-x" and used everything default, with permission Allow unauthenticated :我在我的项目“project-x”上创建了一个 gen 2 cloud function 并使用默认的所有内容,并Allow unauthenticated身份验证:

const functions = require('@google-cloud/functions-framework');

functions.http('helloHttp', (req, res) => {
 res.send(`Hello ${req.query.name || req.body.name || 'World'}!`);
});

This generated a URL for this function, eg https://my-function-bvskvwq11c-uc.a.run.app , which when I call unauthenticated (or visit on the browser) it works.这为这个 function 生成了一个 URL,例如https://my-function-bvskvwq11c-uc.a.run.app ,当我调用未经身份验证(或在浏览器上访问)时它起作用。 I see the response.我看到了回应。

Now here's the problem...现在问题来了...


A. Using the npm package @google-cloud/functions I tried to call this endpoint with the following: A.使用 npm package @google-cloud/functions我尝试使用以下内容调用此端点:

await functionsClient.callFunction({
    name: 'my-function',
})

This gives me a weird error of the format:这给了我一个奇怪的格式错误:

7 PERMISSION_DENIED: Cloud Functions API has not been used in project ********* before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/cloudfunctions.googleapis.com/overview?project=********* then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.

I say this is a weird error, cause the project ID provided in this error is not the ID of my project on GCP and when I visit the link provided it says I do not have permissions to view this project.我说这是一个奇怪的错误,因为此错误中提供的项目 ID 不是我在 GCP 上的项目 ID,当我访问提供的链接时,它说我没有查看此项目的权限。

Then I tried to list my functions by doing the following, but I get back an empty list.然后我尝试通过执行以下操作列出我的功能,但我得到一个空列表。

const [functions] = await functionsClient.listFunctions({
    parent: `projects/project-x/locations/us-central1`,
});
// functions = [];

B. I thought to try gcloud this time, so first I listed all functions for my project: B.这次我想试试gcloud ,所以我首先列出了我项目的所有功能:

gcloud functions list

This actually returned the correct function:这实际上返回了正确的 function:

NAME           STATE   TRIGGER       REGION       ENVIRONMENT
my-function    ACTIVE  HTTP Trigger  us-central1  2nd gen

I'm like cool.我很酷。 Let's try to call it now: So I run:让我们现在尝试调用它:所以我运行:

gcloud functions call my-function

And I get the following back:我得到以下回复:

ResponseError: status=[404], code=[Ok], message=[Function my-function in region us-central1 in project project-x does not exist]

Can someone please shed some light in all of these?有人可以阐明所有这些吗? Listing through the npm package yields different results than the gcloud command and neither of them are able to call the function. One gives 404 and the other one permission denied.通过 npm package 列出与 gcloud 命令产生不同的结果,它们都无法调用 function。一个给出 404,另一个权限被拒绝。 What would be the best approach?最好的方法是什么?

Answering my own question and summarizing.回答我自己的问题并总结。


To call a gen2 function from the gcloud command, use the --gen2 option, as stated on the gcloud documentation :要从 gcloud 命令调用 gen2 function,请使用--gen2选项,如gcloud 文档中所述:

gcloud functions call my-function --gen2

To call gen2 functions from NodeJs, do not use the @google-cloud/functions package. As stated in Google's Rate Limits documentation:要从 NodeJs 调用 gen2 函数,请勿使用@google-cloud/functions package。如 Google 的速率限制文档中所述:

The CALL API only applies to Cloud Functions (1st gen)... Please keep in mind that this API is meant for testing via Cloud Console or gcloud functions call CLI, and it cannot handle heavy traffic. CALL API 仅适用于 Cloud Functions(第一代)...请记住,此 API 用于通过 Cloud Console 或 gcloud 函数调用 CLI 进行测试,它无法处理繁忙的流量。

Instead, use the google-auth-library package, where they also have an example on how to call a function :相反,请使用google-auth-library package,其中还有一个关于如何调用 function 的示例

const {GoogleAuth} = require('google-auth-library');

async function main() {
  const url = 'https://cloud-run-1234-uc.a.run.app';
  const auth = new GoogleAuth();
  const client = await auth.getIdTokenClient(url);
  const res = await client.request({url});
  console.log(res.data);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM