1

我正在尝试使用 Cloud Function 运行 Google Cloud Tasks 任务,但我遇到了一个错误,我尝试使用的任何区域都是错误的。

代码是基本的。一切都很好,直到由于以下错误而停止:

错误:{"code":3,"message":"Location ' europe-west1' 不是有效位置。使用 ListLocations 列出有效位置。","details":[]}

例如,如果我尝试使用 us-central1,它将报告:

错误:{"code":3,"message":"位置必须等于 europe-west1,因为与此项目关联的 App Engine 应用位于 europe-west1 ","details":[]}

我正在使用带有 Node.js 的 Google Cloud Tasks API 来创建新任务:

const client = new CloudTasksClient({ fallback: true }); 
const parent = client.queuePath(PROJECT, 'europe-west1', QUEUE);

一个完整的例子可以在这里找到:https ://github.com/googleapis/nodejs-tasks/blob/master/samples/createHttpTaskWithToken.js

调用的 URL 是:“” https://cloudtasks.googleapis.com:443/$rpc/google.cloud.tasks.v2beta3.CloudTasks/CreateTask

如果我运行位置列表命令,这是输出:

$ gcloud tasks locations list
europe-west1    projects/[project-id]/locations/europe-west1

编辑:使用具有相同配置的 REST API ( https://cloud.google.com/tasks/docs/reference/rest/v2beta3/projects.locations.queues.tasks/create ) 可以工作。可能是客户端的bug?

我真的不确定我的设置有什么问题。

不确定哪些信息对调试有帮助,所以如果没有足够的信息,请提前道歉。

4

1 回答 1

0

我意识到您使用的示例适用于非 App Engine/Cloud 功能环境,请尝试npm 页面中的简单示例。

请检查您的 package.json 是否定义了最新版本的 google-cloud/tasks 库,此时它是 1.9.0

您无需在 App Engine/Cloud Functions 环境中使用 Ouath 令牌,因为已经配置了服务帐户。

// Imports the Google Cloud Tasks library.
  const {CloudTasksClient} = require('@google-cloud/tasks');

  // Instantiates a client.
  const client = new CloudTasksClient();

  // TODO(developer): Uncomment these lines and replace with your values.
  // const project = 'my-project-id';
  // const queue = 'my-appengine-queue';
  // const location = 'us-central1';
  // const payload = 'hello';

  // Construct the fully qualified queue name.
  const parent = client.queuePath(project, location, queue);

  const task = {
    appEngineHttpRequest: {
      httpMethod: 'POST',
      relativeUri: '/log_payload',
    },
  };

  if (payload) {
    task.appEngineHttpRequest.body = Buffer.from(payload).toString('base64');
  }

  if (inSeconds) {
    task.scheduleTime = {
      seconds: inSeconds + Date.now() / 1000,
    };
  }

  const request = {
    parent: parent,
    task: task,
  };

  console.log('Sending task:');
  console.log(task);
  // Send create task request.
  const [response] = await client.createTask(request);
  const name = response.name;
  console.log(`Created task ${name}`);
于 2020-03-30T14:47:19.603 回答