透過程式建立 HTTP 目標工作

Cloud Tasks 處理常式可在任何具有外部 IP 位址的 HTTP 端點上執行,例如 GKE、Compute Engine,甚至是地端部署網路伺服器。您可以在這些服務中,以可靠且可設定的方式執行工作。

本文說明如何使用 Cloud Tasks 用戶端程式庫,以程式輔助方式建立 HTTP 目標任務,並將其放入 Cloud Tasks 工作佇列。您也可以在 Google Cloud 控制台中使用 Google Cloud CLI,或直接向 Cloud Tasks API 傳送要求,建立 HTTP 目標任務。詳情請參閱「建立工作」。

如果工作有 HTTP 目標 (而非較不常見的明確 App Engine 目標),則有兩種建立工作的方式:

  • CreateTask 方法:您必須明確建立工作物件。如果佇列中的工作有不同的轉送設定,請使用這個方法。在這種情況下,您必須在任務層級指定轉送,無法使用佇列層級轉送。這個方法會使用 CreateTask 方法。

  • BufferTask 方法:如果佇列設定為在服務前緩衝工作,請使用這個方法。佇列必須具備佇列層級轉送功能。這個方法會使用 BufferTask 方法。

如要瞭解如何選擇建立 HTTP 任務的兩種方法,請參閱「選擇任務建立方法」。

使用 CreateTask 方法建立工作

本節說明如何建構工作物件來建立工作。您可以使用 CreateTask 方法。

使用 CreateTask 方法建立工作時,您會明確建立及定義工作物件。您必須指定處理工作的服務和處理常式。

您也可以選擇將特定工作的資料傳遞給處理常式。您也可以微調工作的設定,例如排定日後執行的時間,或限制工作失敗時的重試次數 (請參閱「進階設定」)。

下列範例會呼叫 CreateTask 方法,使用 Cloud Tasks 用戶端程式庫建立任務。

C#


using Google.Cloud.Tasks.V2;
using Google.Protobuf;
using Google.Protobuf.WellKnownTypes;
using System;

class CreateHttpTask
{
    public string CreateTask(
        string projectId = "YOUR-PROJECT-ID",
        string location = "us-central1",
        string queue = "my-queue",
        string url = "http://example.com/taskhandler",
        string payload = "Hello World!",
        int inSeconds = 0)
    {
        CloudTasksClient client = CloudTasksClient.Create();
        QueueName parent = new QueueName(projectId, location, queue);

        var response = client.CreateTask(new CreateTaskRequest
        {
            Parent = parent.ToString(),
            Task = new Task
            {
                HttpRequest = new HttpRequest
                {
                    HttpMethod = HttpMethod.Post,
                    Url = url,
                    Body = ByteString.CopyFromUtf8(payload)
                },
                ScheduleTime = Timestamp.FromDateTime(
                    DateTime.UtcNow.AddSeconds(inSeconds))
            }
        });

        Console.WriteLine($"Created Task {response.Name}");
        return response.Name;
    }
}

Go


// Command createHTTPtask constructs and adds a task to a Cloud Tasks Queue.
package main

import (
	"context"
	"fmt"

	cloudtasks "cloud.google.com/go/cloudtasks/apiv2"
	taskspb "cloud.google.com/go/cloudtasks/apiv2/cloudtaskspb"
)

// createHTTPTask creates a new task with a HTTP target then adds it to a Queue.
func createHTTPTask(projectID, locationID, queueID, url, message string) (*taskspb.Task, error) {

	// Create a new Cloud Tasks client instance.
	// See https://godoc.org/cloud.google.com/go/cloudtasks/apiv2
	ctx := context.Background()
	client, err := cloudtasks.NewClient(ctx)
	if err != nil {
		return nil, fmt.Errorf("NewClient: %w", err)
	}
	defer client.Close()

	// Build the Task queue path.
	queuePath := fmt.Sprintf("projects/%s/locations/%s/queues/%s", projectID, locationID, queueID)

	// Build the Task payload.
	// https://godoc.org/google.golang.org/genproto/googleapis/cloud/tasks/v2#CreateTaskRequest
	req := &taskspb.CreateTaskRequest{
		Parent: queuePath,
		Task: &taskspb.Task{
			// https://godoc.org/google.golang.org/genproto/googleapis/cloud/tasks/v2#HttpRequest
			MessageType: &taskspb.Task_HttpRequest{
				HttpRequest: &taskspb.HttpRequest{
					HttpMethod: taskspb.HttpMethod_POST,
					Url:        url,
				},
			},
		},
	}

	// Add a payload message if one is present.
	req.Task.GetHttpRequest().Body = []byte(message)

	createdTask, err := client.CreateTask(ctx, req)
	if err != nil {
		return nil, fmt.Errorf("cloudtasks.CreateTask: %w", err)
	}

	return createdTask, nil
}

Java

import com.google.cloud.tasks.v2.CloudTasksClient;
import com.google.cloud.tasks.v2.HttpMethod;
import com.google.cloud.tasks.v2.HttpRequest;
import com.google.cloud.tasks.v2.QueueName;
import com.google.cloud.tasks.v2.Task;
import com.google.protobuf.ByteString;
import java.io.IOException;
import java.nio.charset.Charset;

public class CreateHttpTask {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "my-project-id";
    String locationId = "us-central1";
    String queueId = "my-queue";
    createTask(projectId, locationId, queueId);
  }

  // Create a task with a HTTP target using the Cloud Tasks client.
  public static void createTask(String projectId, String locationId, String queueId)
      throws IOException {

    // Instantiates a client.
    try (CloudTasksClient client = CloudTasksClient.create()) {
      String url = "https://example.com/taskhandler";
      String payload = "Hello, World!";

      // Construct the fully qualified queue name.
      String queuePath = QueueName.of(projectId, locationId, queueId).toString();

      // Construct the task body.
      Task.Builder taskBuilder =
          Task.newBuilder()
              .setHttpRequest(
                  HttpRequest.newBuilder()
                      .setBody(ByteString.copyFrom(payload, Charset.defaultCharset()))
                      .setUrl(url)
                      .setHttpMethod(HttpMethod.POST)
                      .build());

      // Send create task request.
      Task task = client.createTask(queuePath, taskBuilder.build());
      System.out.println("Task created: " + task.getName());
    }
  }
}

請注意 pom.xml 檔案:

<?xml version='1.0' encoding='UTF-8'?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example.tasks</groupId>
  <artifactId>cloudtasks-snippets</artifactId>
  <packaging>jar</packaging>
  <name>Google Cloud Tasks Snippets</name>

  <!--
    The parent pom defines common style checks and testing strategies for our samples.
    Removing or replacing it should not affect the execution of the samples in anyway.
  -->
  <parent>
    <groupId>com.google.cloud.samples</groupId>
    <artifactId>shared-configuration</artifactId>
    <version>1.2.0</version>
  </parent>

  <properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>


  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>libraries-bom</artifactId>
        <version>26.32.0</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>google-cloud-tasks</artifactId>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.google.truth</groupId>
      <artifactId>truth</artifactId>
      <version>1.4.0</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Node.js

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

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

async function createHttpTask() {
  // TODO(developer): Uncomment these lines and replace with your values.
  // const project = 'my-project-id';
  // const queue = 'my-queue';
  // const location = 'us-central1';
  // const url = 'https://example.com/taskhandler';
  // const payload = 'Hello, World!';
  // const inSeconds = 180;

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

  const task = {
    httpRequest: {
      headers: {
        'Content-Type': 'text/plain', // Set content type to ensure compatibility your application's request parsing
      },
      httpMethod: 'POST',
      url,
    },
  };

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

  if (inSeconds) {
    // The time when the task is scheduled to be attempted.
    task.scheduleTime = {
      seconds: parseInt(inSeconds) + Date.now() / 1000,
    };
  }

  // Send create task request.
  console.log('Sending task:');
  console.log(task);
  const request = {parent: parent, task: task};
  const [response] = await client.createTask(request);
  console.log(`Created task ${response.name}`);
}
createHttpTask();

請注意 package.json 檔案:

{
  "name": "appengine-cloudtasks",
  "description": "Google App Engine Cloud Tasks example.",
  "license": "Apache-2.0",
  "author": "Google Inc.",
  "private": true,
  "engines": {
    "node": ">=20.0.0"
  },
  "files": [
    "*.js"
  ],
  "scripts": {
    "test": "c8 mocha -p -j 2 --timeout 30000",
    "start": "node server.js"
  },
  "dependencies": {
    "@google-cloud/tasks": "^6.0.0",
    "express": "^5.0.0"
  },
  "devDependencies": {
    "c8": "^12.0.0",
    "chai": "^6.0.0",
    "mocha": "^10.0.0",
    "uuid": "^11.1.1"
  }
}

PHP

use Google\Cloud\Tasks\V2\Client\CloudTasksClient;
use Google\Cloud\Tasks\V2\CreateTaskRequest;
use Google\Cloud\Tasks\V2\HttpMethod;
use Google\Cloud\Tasks\V2\HttpRequest;
use Google\Cloud\Tasks\V2\Task;

/** Uncomment and populate these variables in your code */
// $projectId = 'The Google project ID';
// $locationId = 'The Location ID';
// $queueId = 'The Cloud Tasks Queue ID';
// $url = 'The full url path that the task request will be sent to.'
// $payload = 'The payload your task should carry to the task handler. Optional';

// Instantiate the client and queue name.
$client = new CloudTasksClient();
$queueName = $client->queueName($projectId, $locationId, $queueId);

// Create an Http Request Object.
$httpRequest = new HttpRequest();
// The full url path that the task request will be sent to.
$httpRequest->setUrl($url);
// POST is the default HTTP method, but any HTTP method can be used.
$httpRequest->setHttpMethod(HttpMethod::POST);
// Setting a body value is only compatible with HTTP POST and PUT requests.
if (!empty($payload)) {
    $httpRequest->setBody($payload);
}

// Create a Cloud Task object.
$task = new Task();
$task->setHttpRequest($httpRequest);

// Send request and print the task name.
$request = (new CreateTaskRequest())
    ->setParent($queueName)
    ->setTask($task);
$response = $client->createTask($request);
printf('Created task %s' . PHP_EOL, $response->getName());

請注意 composer.json 檔案:

{
    "require": {
        "google/cloud-tasks": "^2.0"
    }
}

Python

import datetime
import json
from typing import Dict, Optional

from google.cloud import tasks_v2
from google.protobuf import duration_pb2, timestamp_pb2


def create_http_task(
    project: str,
    location: str,
    queue: str,
    url: str,
    json_payload: Dict,
    scheduled_seconds_from_now: Optional[int] = None,
    task_id: Optional[str] = None,
    deadline_in_seconds: Optional[int] = None,
) -> tasks_v2.Task:
    """Create an HTTP POST task with a JSON payload.
    Args:
        project: The project ID where the queue is located.
        location: The location where the queue is located.
        queue: The ID of the queue to add the task to.
        url: The target URL of the task.
        json_payload: The JSON payload to send.
        scheduled_seconds_from_now: Seconds from now to schedule the task for.
        task_id: ID to use for the newly created task.
        deadline_in_seconds: The deadline in seconds for task.
    Returns:
        The newly created task.
    """

    # Create a client.
    client = tasks_v2.CloudTasksClient()

    # Construct the task.
    task = tasks_v2.Task(
        http_request=tasks_v2.HttpRequest(
            http_method=tasks_v2.HttpMethod.POST,
            url=url,
            headers={"Content-type": "application/json"},
            body=json.dumps(json_payload).encode(),
        ),
        name=(
            client.task_path(project, location, queue, task_id)
            if task_id is not None
            else None
        ),
    )

    # Convert "seconds from now" to an absolute Protobuf Timestamp
    if scheduled_seconds_from_now is not None:
        timestamp = timestamp_pb2.Timestamp()
        timestamp.FromDatetime(
            datetime.datetime.utcnow()
            + datetime.timedelta(seconds=scheduled_seconds_from_now)
        )
        task.schedule_time = timestamp

    # Convert "deadline in seconds" to a Protobuf Duration
    if deadline_in_seconds is not None:
        duration = duration_pb2.Duration()
        duration.FromSeconds(deadline_in_seconds)
        task.dispatch_deadline = duration

    # Use the client to send a CreateTaskRequest.
    return client.create_task(
        tasks_v2.CreateTaskRequest(
            # The queue to add the task to
            parent=client.queue_path(project, location, queue),
            # The task itself
            task=task,
        )
    )

請注意 requirements.txt 檔案:

google-cloud-tasks==2.18.0

Ruby

require "google/cloud/tasks"

# Create a Task with an HTTP Target
#
# @param [String] project_id Your Google Cloud Project ID.
# @param [String] location_id Your Google Cloud Project Location ID.
# @param [String] queue_id Your Google Cloud Tasks Queue ID.
# @param [String] url The full path to sent the task request to.
# @param [String] payload The request body of your task.
# @param [Integer] seconds The delay, in seconds, to process your task.
def create_http_task project_id, location_id, queue_id, url, payload: nil, seconds: nil
  # Instantiates a client.
  client = Google::Cloud::Tasks.cloud_tasks

  # Construct the fully qualified queue name.
  parent = client.queue_path project: project_id, location: location_id, queue: queue_id

  # Construct task.
  task = {
    http_request: {
      http_method: "POST",
      url:         url
    }
  }

  # Add payload to task body.
  task[:http_request][:body] = payload if payload

  # Add scheduled time to task.
  if seconds
    timestamp = Google::Protobuf::Timestamp.new
    timestamp.seconds = Time.now.to_i + seconds.to_i
    task[:schedule_time] = timestamp
  end

  # Send create task request.
  puts "Sending task #{task}"

  response = client.create_task parent: parent, task: task

  puts "Created task #{response.name}" if response.name
end

使用 BufferTask 方法建立工作

本節說明如何透過傳送 HTTP 要求來建立任務。您使用的方法稱為 BufferTask。

限制

BufferTask 方法有下列限制:

  • 用戶端程式庫:用戶端程式庫不支援 BufferTask 方法。

  • RPC API:RPC API 不支援 BufferTask 方法。

  • 工作層級轉送:這個方法不支援工作層級轉送。由於以這種方式建立任務時,無法新增轉送資訊,因此您必須使用佇列層級轉送 (否則任務不會有轉送資訊)。如果佇列尚未使用佇列層級轉送,請參閱「設定 HTTP 任務的佇列層級轉送」。

呼叫 BufferTask 方法

下列範例說明如何傳送 HTTP POST 要求至 Cloud Tasks API buffer 端點,以建立工作。

curl

下列程式碼片段範例說明如何使用 BufferTask 方法和 curl 建立工作:

curl -X HTTP_METHOD\
"https://cloudtasks.googleapis.com/v2/projects/PROJECT_ID/locations/LOCATION/queues/QUEUE_ID/tasks:buffer" \

更改下列內容:

  • HTTP_METHOD:要求使用的 HTTP 方法,例如 GET 或 POST。
  • PROJECT_ID: Google Cloud 專案的 ID。在終端機中執行下列指令,即可取得這項資訊:
    gcloud config get-value project
  • LOCATION:佇列的位置。
  • QUEUE_ID:佇列 ID。

Python

from google.cloud import tasks_v2beta3 as tasks

import requests


def send_task_to_http_queue(
    queue: tasks.Queue, body: str = "", token: str = "", headers: dict = {}
) -> int:
    """Send a task to an HTTP queue.
    Args:
        queue: The queue to send task to.
        body: The body of the task.
        auth_token: An authorization token for the queue.
        headers: Headers to set on the task.
    Returns:
        The matching queue, or None if it doesn't exist.
    """

    # Use application default credentials if not supplied in a header
    if token:
        headers["Authorization"] = f"Bearer {token}"

    endpoint = f"https://cloudtasks.googleapis.com/v2beta3/{queue.name}/tasks:buffer"
    response = requests.post(endpoint, body, headers=headers)

    return response.status_code

設定 HTTP 目標處理常式驗證的服務帳戶

如果您有具備適當憑證的服務帳戶,可存取處理常式,Cloud Tasks 就能呼叫需要驗證的 HTTP 目標處理常式。

只要授予適當角色,即可使用目前的服務帳戶。這些操作說明涵蓋如何專為這項功能建立新的服務帳戶。用於 Cloud Tasks 驗證的現有或新服務帳戶,必須與 Cloud Tasks 佇列位於相同專案。

  1. 前往 Google Cloud 控制台的「Service accounts」(服務帳戶) 頁面。

    前往「Service Accounts」(服務帳戶)

  2. 視需要選取適當的專案。

  3. 按一下「 Create service account」(建立服務帳戶)。

  4. 為服務帳戶命名。記下控制台產生的相關電子郵件地址。您會在後續步驟中搜尋這封電子郵件。如要複製電子郵件地址,請按一下「複製到剪貼簿」圖示 。您也可以新增帳戶用途說明。

  5. 點按「Create and continue」(建立並繼續)。

  6. 在「Select a role」(選取角色) 清單中,搜尋並選取「Cloud Tasks Enqueuer (Beta)」(Cloud Tasks 工作加入佇列者 (Beta 版))。這個角色可讓服務帳戶將任務新增至佇列。

  7. 如果您使用 Google Cloud 處理常式,請按一下「+ Add another role」(+ 新增其他角色),將與存取處理常式執行所在服務相關聯的角色授予服務帳戶。 Google Cloud中的每項服務都需要不同的角色。舉例來說,如要存取 Cloud Run 上的處理常式,請授予 Cloud Run 叫用者角色。

  8. 按一下「Done」(完成),完成服務帳戶建立作業。

  9. 如要允許 Cloud Tasks 使用您剛建立的服務帳戶建立驗證權杖,您必須將服務帳戶使用者 (roles/iam.serviceAccountUser) 角色授予 Cloud Tasks 主要服務代理,並指定您剛建立的服務帳戶。

    1. 前往 Google Cloud 控制台的「Service accounts」(服務帳戶) 頁面。

      前往「Service accounts」(服務帳戶)

    2. 在剛建立的服務帳戶資料列中,勾選核取方塊,然後按一下 「Manage access」(管理存取權)。

    3. 在「Manage Access」(管理存取權) 窗格中,按一下 「Add principal」(新增主體)。

    4. 在「New principals」(新增主體) 清單中,搜尋並選取 Cloud Tasks 服務代理,其電子郵件地址格式為 service-PROJECT_NUMBER@gcp-sa-cloudtasks.iam.gserviceaccount.com。

    5. 在「Select a role」(選取角色) 清單中,搜尋並選取「Service Account User」(服務帳戶使用者) 角色。

    6. 按一下「儲存」。

使用 HTTP 目標任務和驗證權杖

如要在 Cloud Tasks 和需要驗證的 HTTP 目標處理常式之間進行驗證,Cloud Tasks 會建立標頭權杖。這個權杖是以 Cloud Tasks Enqueuer 服務帳戶中的憑證為基礎,並以其電子郵件地址識別。用於驗證的服務帳戶必須與 Cloud Tasks 佇列位於同一個專案。系統會使用 HTTPS,將含有標頭權杖的要求從佇列傳送至處理常式。您可以使用 ID 權杖或存取權杖。ID 權杖通常應用於在 Google Cloud上執行的任何處理常式,例如 Cloud Run 函式或 Cloud Run。主要例外狀況是託管於 *.googleapis.com 的 Google API,這類 API 需要存取權杖。

您可以設定 httpTarget 覆寫 (特別是 oauthToken 或 oidcToken 欄位),在佇列層級設定驗證。如果在佇列層級設定驗證,這項設定會覆寫在任務層級設定的任何驗證。舉例來說,如果佇列已設定 OIDC 權杖覆寫,則使用 OAuth 權杖新增至該佇列的工作,會使用佇列層級的 OIDC 權杖調度。

如要在工作層級設定驗證,請在工作本身指定 ID 權杖或存取權杖。

CreateTask 方法

下列範例使用 CreateTask 方法和 Cloud Tasks 用戶端程式庫,建立工作並建立標頭權杖。範例中使用的是 ID 權杖。如要使用存取權杖,請在建構要求時,將 OIDC 參數替換為適當語言的 OAuth 參數。

Go

import (
	"context"
	"fmt"

	cloudtasks "cloud.google.com/go/cloudtasks/apiv2"
	taskspb "cloud.google.com/go/cloudtasks/apiv2/cloudtaskspb"
)

// createHTTPTaskWithToken constructs a task with a authorization token
// and HTTP target then adds it to a Queue.
func createHTTPTaskWithToken(projectID, locationID, queueID, url, email, message string) (*taskspb.Task, error) {
	// Create a new Cloud Tasks client instance.
	// See https://godoc.org/cloud.google.com/go/cloudtasks/apiv2
	ctx := context.Background()
	client, err := cloudtasks.NewClient(ctx)
	if err != nil {
		return nil, fmt.Errorf("NewClient: %w", err)
	}
	defer client.Close()

	// Build the Task queue path.
	queuePath := fmt.Sprintf("projects/%s/locations/%s/queues/%s", projectID, locationID, queueID)

	// Build the Task payload.
	// https://godoc.org/google.golang.org/genproto/googleapis/cloud/tasks/v2#CreateTaskRequest
	req := &taskspb.CreateTaskRequest{
		Parent: queuePath,
		Task: &taskspb.Task{
			// https://godoc.org/google.golang.org/genproto/googleapis/cloud/tasks/v2#HttpRequest
			MessageType: &taskspb.Task_HttpRequest{
				HttpRequest: &taskspb.HttpRequest{
					HttpMethod: taskspb.HttpMethod_POST,
					Url:        url,
					AuthorizationHeader: &taskspb.HttpRequest_OidcToken{
						OidcToken: &taskspb.OidcToken{
							ServiceAccountEmail: email,
						},
					},
				},
			},
		},
	}

	// Add a payload message if one is present.
	req.Task.GetHttpRequest().Body = []byte(message)

	createdTask, err := client.CreateTask(ctx, req)
	if err != nil {
		return nil, fmt.Errorf("cloudtasks.CreateTask: %w", err)
	}

	return createdTask, nil
}

Java

import com.google.cloud.tasks.v2.CloudTasksClient;
import com.google.cloud.tasks.v2.HttpMethod;
import com.google.cloud.tasks.v2.HttpRequest;
import com.google.cloud.tasks.v2.OidcToken;
import com.google.cloud.tasks.v2.QueueName;
import com.google.cloud.tasks.v2.Task;
import com.google.protobuf.ByteString;
import java.io.IOException;
import java.nio.charset.Charset;

public class CreateHttpTaskWithToken {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "my-project-id";
    String locationId = "us-central1";
    String queueId = "my-queue";
    String serviceAccountEmail =
        "java-docs-samples-testing@java-docs-samples-testing.iam.gserviceaccount.com";
    createTask(projectId, locationId, queueId, serviceAccountEmail);
  }

  // Create a task with a HTTP target and authorization token using the Cloud Tasks client.
  public static void createTask(
      String projectId, String locationId, String queueId, String serviceAccountEmail)
      throws IOException {

    // Instantiates a client.
    try (CloudTasksClient client = CloudTasksClient.create()) {
      String url =
          "https://example.com/taskhandler"; // The full url path that the request will be sent to
      String payload = "Hello, World!"; // The task HTTP request body

      // Construct the fully qualified queue name.
      String queuePath = QueueName.of(projectId, locationId, queueId).toString();

      // Add your service account email to construct the OIDC token.
      // in order to add an authentication header to the request.
      OidcToken.Builder oidcTokenBuilder =
          OidcToken.newBuilder().setServiceAccountEmail(serviceAccountEmail);

      // Construct the task body.
      Task.Builder taskBuilder =
          Task.newBuilder()
              .setHttpRequest(
                  HttpRequest.newBuilder()
                      .setBody(ByteString.copyFrom(payload, Charset.defaultCharset()))
                      .setHttpMethod(HttpMethod.POST)
                      .setUrl(url)
                      .setOidcToken(oidcTokenBuilder)
                      .build());

      // Send create task request.
      Task task = client.createTask(queuePath, taskBuilder.build());
      System.out.println("Task created: " + task.getName());
    }
  }
}

請注意 pom.xml 檔案:

<?xml version='1.0' encoding='UTF-8'?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example.tasks</groupId>
  <artifactId>cloudtasks-snippets</artifactId>
  <packaging>jar</packaging>
  <name>Google Cloud Tasks Snippets</name>

  <!--
    The parent pom defines common style checks and testing strategies for our samples.
    Removing or replacing it should not affect the execution of the samples in anyway.
  -->
  <parent>
    <groupId>com.google.cloud.samples</groupId>
    <artifactId>shared-configuration</artifactId>
    <version>1.2.0</version>
  </parent>

  <properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>


  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>libraries-bom</artifactId>
        <version>26.32.0</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>google-cloud-tasks</artifactId>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.google.truth</groupId>
      <artifactId>truth</artifactId>
      <version>1.4.0</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Node.js

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

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

async function createHttpTaskWithToken() {
  // TODO(developer): Uncomment these lines and replace with your values.
  // const project = 'my-project-id';
  // const queue = 'my-queue';
  // const location = 'us-central1';
  // const url = 'https://example.com/taskhandler';
  // const serviceAccountEmail = 'client@<project-id>.iam.gserviceaccount.com';
  // const payload = 'Hello, World!';

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

  const task = {
    httpRequest: {
      headers: {
        'Content-Type': 'text/plain', // Set content type to ensure compatibility your application's request parsing
      },
      httpMethod: 'POST',
      url,
      oidcToken: {
        serviceAccountEmail,
      },
    },
  };

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

  console.log('Sending task:');
  console.log(task);
  // Send create task request.
  const request = {parent: parent, task: task};
  const [response] = await client.createTask(request);
  const name = response.name;
  console.log(`Created task ${name}`);
}
createHttpTaskWithToken();

請注意 package.json 檔案:

{
  "name": "appengine-cloudtasks",
  "description": "Google App Engine Cloud Tasks example.",
  "license": "Apache-2.0",
  "author": "Google Inc.",
  "private": true,
  "engines": {
    "node": ">=20.0.0"
  },
  "files": [
    "*.js"
  ],
  "scripts": {
    "test": "c8 mocha -p -j 2 --timeout 30000",
    "start": "node server.js"
  },
  "dependencies": {
    "@google-cloud/tasks": "^6.0.0",
    "express": "^5.0.0"
  },
  "devDependencies": {
    "c8": "^12.0.0",
    "chai": "^6.0.0",
    "mocha": "^10.0.0",
    "uuid": "^11.1.1"
  }
}

Python

from typing import Optional

from google.cloud import tasks_v2


def create_http_task_with_token(
    project: str,
    location: str,
    queue: str,
    url: str,
    payload: bytes,
    service_account_email: str,
    audience: Optional[str] = None,
) -> tasks_v2.Task:
    """Create an HTTP POST task with an OIDC token and an arbitrary payload.
    Args:
        project: The project ID where the queue is located.
        location: The location where the queue is located.
        queue: The ID of the queue to add the task to.
        url: The target URL of the task.
        payload: The payload to send.
        service_account_email: The service account to use for generating the OIDC token.
        audience: Audience to use when generating the OIDC token.
    Returns:
        The newly created task.
    """

    # Create a client.
    client = tasks_v2.CloudTasksClient()

    # Construct the request body.
    task = tasks_v2.Task(
        http_request=tasks_v2.HttpRequest(
            http_method=tasks_v2.HttpMethod.POST,
            url=url,
            oidc_token=tasks_v2.OidcToken(
                service_account_email=service_account_email,
                audience=audience,
            ),
            body=payload,
        ),
    )

    # Use the client to build and send the task.
    return client.create_task(
        tasks_v2.CreateTaskRequest(
            parent=client.queue_path(project, location, queue),
            task=task,
        )
    )

請注意 requirements.txt 檔案:

google-cloud-tasks==2.18.0

BufferTask 方法

下列範例使用應用程式預設憑證,在使用 BufferTask 方法建立工作時進行驗證。

curl

curl -X HTTP_METHOD\
"https://cloudtasks.googleapis.com/v2/projects/PROJECT_ID/locations/LOCATION/queues/QUEUE_ID/tasks:buffer" \
    -H "Authorization: Bearer ACCESS_TOKEN"

更改下列內容:

  • HTTP_METHOD:要求使用的 HTTP 方法,例如 GET 或 POST。
  • PROJECT_ID: Google Cloud 專案的 ID。在終端機中執行下列指令,即可取得這項資訊:
    gcloud config get-value project
  • LOCATION:佇列的位置。
  • QUEUE_ID:佇列 ID。
  • ACCESS_TOKEN:您的存取權杖。在終端機執行下列指令,即可取得這項資訊:
  • gcloud auth application-default login
  • gcloud auth application-default print-access-token

Python

在下列程式碼範例中,提供驗證權杖值。

from google.cloud import tasks_v2beta3 as tasks

import requests


def send_task_to_http_queue(
    queue: tasks.Queue, body: str = "", token: str = "", headers: dict = {}
) -> int:
    """Send a task to an HTTP queue.
    Args:
        queue: The queue to send task to.
        body: The body of the task.
        auth_token: An authorization token for the queue.
        headers: Headers to set on the task.
    Returns:
        The matching queue, or None if it doesn't exist.
    """

    # Use application default credentials if not supplied in a header
    if token:
        headers["Authorization"] = f"Bearer {token}"

    endpoint = f"https://cloudtasks.googleapis.com/v2beta3/{queue.name}/tasks:buffer"
    response = requests.post(endpoint, body, headers=headers)

    return response.status_code

提供自己的 HTTP 目標任務處理常式

HTTP 目標工作處理常式與 App Engine 工作處理常式非常相似,但有下列例外狀況:

  • 逾時:所有 HTTP 目標工作處理常式的預設逾時時間為 10 分鐘,最長為 30 分鐘。
  • 驗證邏輯:如果您在目標服務中自行編寫程式碼來驗證權杖,應使用 ID 權杖。如要進一步瞭解這項作業的相關資訊,請參閱 OpenID Connect,特別是驗證 ID 權杖。
  • 標頭:HTTP 目標要求具有由佇列設定的標頭,其中包含處理常式可使用的任務專屬資訊。這些標頭與 App Engine 任務要求上設定的標頭類似,但不完全相同。

    HTTP 目標要求一律包含下列標頭:

    標頭 說明
    X-CloudTasks-QueueName 佇列名稱。
    X-CloudTasks-TaskName 工作的「簡短」名稱;如果在建立工作時沒有指定名稱,則是由系統產生的唯一 ID 來代替。這是完整工作名稱中的 my-task-id 值,即 task_name = projects/my-project-id/locations/my-location/queues/my-queue-id/tasks/my-task-id。
    X-CloudTasks-TaskRetryCount 這個工作的已重試次數。若為第一次嘗試,此值為 0。這個數字包含因 5XX 錯誤代碼而失敗,且從未進入執行階段的嘗試。
    X-CloudTasks-TaskExecutionCount 工作收到處理常式回應的總次數。由於 Cloud Tasks 在收到成功回應後會刪除工作,所以先前的所有處理常式回應都是失敗。這個數字不包含因 5XX 錯誤代碼而失敗的次數。
    X-CloudTasks-TaskETA 一開始,系統會以 1970 年 1 月 1 日起算,以秒為單位指定任務的原訂時間。請注意,這代表預計派遣時間。重試時,這個值會更新為更接近目前時間的值,可用於評估傳送延遲時間。不應用於工作簡化。

    此外,Cloud Tasks 的要求可能包含下列標頭:

    標頭 說明
    X-CloudTasks-TaskPreviousResponse 上次重試的 HTTP 回應代碼。
    X-CloudTasks-TaskRetryReason 重試工作的原因。

手動新增 Cloud Tasks 服務代理角色

您可以手動將 Cloud Tasks 服務代理 (roles/cloudtasks.serviceAgent) 角色新增至 Cloud Tasks 服務帳戶,也就是 Cloud Tasks 的主要服務代理。

只有在 2019 年 3 月 19 日前啟用 Cloud Tasks API,才需要執行這項操作。

控制台

  1. 在 Google Cloud 控制台的「Welcome」(歡迎) 頁面,找到並複製 Google Cloud 專案的專案編號。
  2. 前往 Google Cloud 控制台的「IAM」(身分與存取權管理) 頁面。

    前往「身分與存取權管理」頁面

  3. 按一下「Grant access」(授予存取權)。「授予存取權」窗格隨即開啟。

  4. 在「新增主體」部分,按照下列格式新增電子郵件地址:

     service-PROJECT_NUMBER@gcp-sa-cloudtasks.iam.gserviceaccount.com
     

    將 PROJECT_NUMBER 替換為您的 Google Cloud 專案編號。

  5. 在「Assign roles」(指派角色) 部分,搜尋並選取「Cloud Tasks Service Agent」(Cloud Tasks 服務代理)。

  6. 按一下「儲存」。

gcloud

  1. 找出專案編號:

    gcloud projects describe PROJECT_ID --format='table(projectNumber)'

    將 PROJECT_ID 替換為專案 ID。

  2. 複製編號。

  3. 使用您複製的專案編號,將 Cloud Tasks Service Agent 角色授予 Cloud Tasks 服務代理:

    gcloud projects add-iam-policy-binding PROJECT_ID \
        --member serviceAccount:service-PROJECT_NUMBER@gcp-sa-cloudtasks.iam.gserviceaccount.com \
        --role roles/cloudtasks.serviceAgent

    更改下列內容:

    • PROJECT_ID:您的 Google Cloud 專案 ID。
    • PROJECT_NUMBER:您的 Google Cloud專案編號。

其他設定

您可以設定多項工作屬性。如需完整清單,請參閱工作資源定義。舉例來說,您可以自訂下列屬性:

  • 命名:如果您選擇為工作指定名稱,Cloud Tasks 可以使用該名稱確保工作簡化,但這項處理作業可能會增加延遲時間。
  • 排程:您可以排定在未來執行工作。這項功能僅支援 CreateTask,不支援 BufferTask。

您也可以設定佇列屬性,例如轉送覆寫、頻率限制和重試參數。這些設定會套用至佇列中的所有任務。詳情請參閱「設定佇列轉送、限制和重試」。

後續步驟