在 firebase 函数中创建 Google Cloud Task [英] creating Google Cloud Task in a firebase function

查看:16
本文介绍了在 firebase 函数中创建 Google Cloud Task的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调用试图创建任务的 firebase HTTP 函数时,我在 firebase 控制台函数日志中遇到错误.

I'm getting an error in the firebase console functions log when calling a firebase HTTP function that tries to create a task.

错误:7 PERMISSION_DENIED:委托人(用户或服务帐户)缺少 IAM 权限cloudtasks.tasks.create"对于资源projects/my-gcloud-project-id/locations/us-central1/queues/myqueuename"(或者资源可能不存在).

Error: 7 PERMISSION_DENIED: The principal (user or service account) lacks IAM permission "cloudtasks.tasks.create" for the resource "projects/my-gcloud-project-id/locations/us-central1/queues/myqueuename" (or the resource may not exist).

也许我对 gcloud id 和location 与 firebase id &地点?

Maybe I'm confused between the gcloud id & location versus the firebase id & location?

我已通过运行 gcloud --project my-gcloud-project-id tasks locations list

或者也许我需要设置权限?

Or maybe somehow I need to set up permissions?

我的代码:



const functions = require('firebase-functions');
const { CloudTasksClient } = require('@google-cloud/tasks')

const projectId = 'my-firebase-project-id';
const location = 'us-central1'
const queue = 'myqueuename'

exports.onFormSubmit = functions.https.onRequest(async (req, res) => {
  const tasksClient = new CloudTasksClient()
  const queuePath = tasksClient.queuePath('my-gcloud-project-id', location, queue);

  const url = `https://google.com/` // edited for stack overflow
  const delaySeconds = 5;
  console.log('delaying for ', delaySeconds, ' seconds');

  const task = {
      httpRequest: {
          httpMethod: 'POST',
          url,
          body: '',
          headers: {
              'Content-Type': 'application/json',
          },
      },
      scheduleTime: {
          seconds: delaySeconds
      }
  }

  const [ response ] = await tasksClient.createTask({ parent: queuePath, task })

  console.log('task name', response.name);
});

推荐答案

为了创建 Google 任务,您必须添加正确的 permissions 在 IAM 上,在这种情况下,如错误消息所示,您必须向服务添加 cloudtasks.tasks.create 权限正在调用 Cloud Function 的帐户.

In order to create a Google Task you have to add the correct permissions on IAM, in this case as the error message is showing, you have to add the cloudtasks.tasks.create permission to the service account that is invoking the Cloud Function.

这可以通过进入 Cloud Console 然后进入 IAM 来完成,搜索服务帐户 通常类似于 service-project-number@gcf-admin-robot.iam.gserviceaccount.com (update: 它是 my-project-id@appspot.gserviceaccount.com)并添加所需的权限,如果您有基于角色的权限 Cloud Tasks Enqueuer 应该足以创建任务.

This can be done by going inside the Cloud Console and then into IAM, search for the service account usually is something like service-project-number@gcf-admin-robot.iam.gserviceaccount.com (update: it was my-project-id@appspot.gserviceaccount.com) and add the required permission, if you have a role based permissions Cloud Tasks Enqueuer should be enough to create the tasks.

这篇关于在 firebase 函数中创建 Google Cloud Task的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆