使用 Firestore 文件觸發函式

本指南會列舉一些範例,說明在您對特定集合中的文件進行變更時,會觸發哪些函式。

事前準備

在本指南中執行範例程式碼前,您必須先執行下列操作:

範例

以下範例說明如何編寫可回應 Firestore 觸發事件的函式。

範例 1:Hello Firestore 函式

以下範例會列印觸發 Firestore 事件的欄位:

Node.js

/**
 * Cloud Event Function triggered by a change to a Firestore document.
 */
const functions = require('@google-cloud/functions-framework');
const protobuf = require('protobufjs');

functions.cloudEvent('helloFirestore', async cloudEvent => {
  console.log(`Function triggered by event on: ${cloudEvent.source}`);
  console.log(`Event type: ${cloudEvent.type}`);

  console.log('Loading protos...');
  const root = await protobuf.load('data.proto');
  const DocumentEventData = root.lookupType(
    'google.events.cloud.firestore.v1.DocumentEventData'
  );

  console.log('Decoding data...');
  const firestoreReceived = DocumentEventData.decode(cloudEvent.data);

  console.log('\nOld value:');
  console.log(JSON.stringify(firestoreReceived.oldValue, null, 2));

  console.log('\nNew value:');
  console.log(JSON.stringify(firestoreReceived.value, null, 2));
});

Python

from cloudevents.http import CloudEvent
import functions_framework
from google.events.cloud import firestore


@functions_framework.cloud_event
def hello_firestore(cloud_event: CloudEvent) -> None:
    """Triggers by a change to a Firestore document.

    Args:
        cloud_event: cloud event with information on the firestore event trigger
    """
    firestore_payload = firestore.DocumentEventData()
    firestore_payload._pb.ParseFromString(cloud_event.data)

    print(f"Function triggered by change to: {cloud_event['source']}")

    print("\nOld value:")
    print(firestore_payload.old_value)

    print("\nNew value:")
    print(firestore_payload.value)

Go


// Package hellofirestore contains a Cloud Event Function triggered by a Cloud Firestore event.
package hellofirestore

import (
	"context"
	"fmt"

	"github.com/GoogleCloudPlatform/functions-framework-go/functions"
	"github.com/cloudevents/sdk-go/v2/event"
	"github.com/googleapis/google-cloudevents-go/cloud/firestoredata"
	"google.golang.org/protobuf/proto"
)

func init() {
	functions.CloudEvent("helloFirestore", HelloFirestore)
}

// HelloFirestore is triggered by a change to a Firestore document.
func HelloFirestore(ctx context.Context, event event.Event) error {
	var data firestoredata.DocumentEventData

	// If you omit `DiscardUnknown`, protojson.Unmarshal returns an error
	// when encountering a new or unknown field.
	options := proto.UnmarshalOptions{
		DiscardUnknown: true,
	}
	err := options.Unmarshal(event.Data(), &data)

	if err != nil {
		return fmt.Errorf("proto.Unmarshal: %w", err)
	}

	fmt.Printf("Function triggered by change to: %v\n", event.Source())
	fmt.Printf("Old value: %+v\n", data.GetOldValue())
	fmt.Printf("New value: %+v\n", data.GetValue())
	return nil
}

Java

import com.google.cloud.functions.CloudEventsFunction;
import com.google.events.cloud.firestore.v1.DocumentEventData;
import com.google.protobuf.InvalidProtocolBufferException;
import io.cloudevents.CloudEvent;
import java.util.logging.Logger;

public class FirebaseFirestore implements CloudEventsFunction {
  private static final Logger logger = Logger.getLogger(FirebaseFirestore.class.getName());

  @Override
  public void accept(CloudEvent event) throws InvalidProtocolBufferException {
    DocumentEventData firestoreEventData = DocumentEventData
        .parseFrom(event.getData().toBytes());

    logger.info("Function triggered by event on: " + event.getSource());
    logger.info("Event type: " + event.getType());

    logger.info("Old value:");
    logger.info(firestoreEventData.getOldValue().toString());

    logger.info("New value:");
    logger.info(firestoreEventData.getValue().toString());
  }
}

C#

using CloudNative.CloudEvents;
using Google.Cloud.Functions.Framework;
using Google.Events.Protobuf.Cloud.Firestore.V1;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace FirebaseFirestore;

public class Function : ICloudEventFunction<DocumentEventData>
{
    private readonly ILogger _logger;

    public Function(ILogger<Function> logger) =>
        _logger = logger;

    public Task HandleAsync(CloudEvent cloudEvent, DocumentEventData data, CancellationToken cancellationToken)
    {
        _logger.LogInformation("Function triggered by event on {subject}", cloudEvent.Subject);
        _logger.LogInformation("Event type: {type}", cloudEvent.Type);
        MaybeLogDocument("Old value", data.OldValue);
        MaybeLogDocument("New value", data.Value);

        // In this example, we don't need to perform any asynchronous operations, so the
        // method doesn't need to be declared async.
        return Task.CompletedTask;
    }

    /// <summary>
    /// Logs the names and values of the fields in a document in a very simplistic way.
    /// </summary>
    private void MaybeLogDocument(string message, Document document)
    {
        if (document is null)
        {
            return;
        }

        // ConvertFields converts the Firestore representation into a .NET-friendly
        // representation.
        IReadOnlyDictionary<string, object> fields = document.ConvertFields();
        var fieldNamesAndTypes = fields
            .OrderBy(pair => pair.Key)
            .Select(pair => $"{pair.Key}: {pair.Value}");
        _logger.LogInformation(message + ": {fields}", string.Join(", ", fieldNamesAndTypes));
    }
}

部署 Hello Firestore 函式

如果您尚未設定,請先設定 Firestore 資料庫

按一下分頁標籤,瞭解如何使用自選工具。

主控台

使用 Google Cloud 控制台建立函式時,您也可以為函式新增觸發條件。請按照下列步驟為函式建立觸發條件:

  1. 前往 Google Cloud 控制台的 Cloud Run:

    前往 Cloud Run

  2. 按一下「編寫函式」,然後輸入函式詳細資料。如要進一步瞭解如何在部署期間設定函式,請參閱「部署函式」。

  3. 在「觸發條件」部分中,按一下「新增觸發條件」

  4. 選取「Firestore 觸發條件」

  5. 在「Eventarc trigger」窗格中,修改觸發條件詳細資料,如下所示:

    1. 在「Trigger name」欄位中輸入觸發事件名稱,或使用預設名稱。

    2. 從清單中選取「觸發條件類型」,指定下列其中一種觸發條件類型:

      • Google 來源:指定 Pub/Sub、Cloud Storage、Firestore 和其他 Google 事件供應器的觸發事件。

      • 第三方:整合提供 Eventarc 來源的非 Google 供應商。詳情請參閱「Eventarc 中的第三方事件」。

    3. 從「Event provider」清單中選取「Firestore」Firestore,即可選取提供事件類型以觸發函式的產品。如需事件提供者清單,請參閱「事件提供者和目的地」。

    4. 在「Event type」清單中,選取「type=google.cloud.firestore.document.v1.written」。觸發條件設定會因支援的事件類型而異。詳情請參閱「事件類型」。

    5. 在「篩選器」部分,選取資料庫、運算和屬性值,或使用預設選項。

    6. 如果已啟用「區域」欄位,請為 Eventarc 觸發條件選取「位置」。一般來說,Eventarc 觸發條件的位置應與您要監控事件的 Google Cloud 資源位置一致。在大多數情況下,您也應在相同的地區部署函式。如要進一步瞭解 Eventarc 觸發條件的所在位置,請參閱「瞭解 Eventarc 位置」。

    7. 在「服務帳戶」欄位中,選取服務帳戶。Eventarc 觸發事件會連結至服務帳戶,以便在叫用函式時做為身分使用。Eventarc 觸發事件的服務帳戶必須具備叫用函式的權限。根據預設,Cloud Run 會使用 Compute Engine 預設服務帳戶

    8. 您可以視需要指定要傳送傳入要求的服務網址路徑。這是目的地服務中的相對路徑,應將觸發事件傳送至此路徑。例如://routerouteroute/subroute

  6. 填妥必填欄位後,按一下「儲存觸發條件」

gcloud

使用 gcloud CLI 建立函式時,您必須先部署函式,然後再建立觸發條件。請按照下列步驟為函式建立觸發條件:

  1. 在包含程式碼範例的目錄中執行下列指令,即可部署函式:

    gcloud run deploy FUNCTION \
            --source . \
            --function FUNCTION_ENTRYPOINT \
            --base-image BASE_IMAGE_ID \
            --region REGION
    

    取代:

    • FUNCTION 替換為您要部署的函式名稱。您可以將這個參數完全省略,這樣系統會提示您輸入名稱。

    • FUNCTION_ENTRYPOINT 與原始碼中函式的進入點。這是 Cloud Run 在函式執行時執行的程式碼。這個標記的值必須是來源程式碼中存在的函式名稱或完全限定的類別名稱。

    • BASE_IMAGE_ID 與函式的基礎映像檔環境。如要進一步瞭解基礎映像檔和各個映像檔所包含的套件,請參閱「執行階段基礎映像檔」。

    • REGION 與您要部署函式的 Google Cloud 區域。例如:us-central1

  2. 執行下列指令,建立用於篩選事件的觸發條件:

    gcloud eventarc triggers create TRIGGER_NAME  \
        --location=EVENTARC_TRIGGER_LOCATION \
        --destination-run-service=FUNCTION  \
        --destination-run-region=REGION \
        --event-filters=type=google.cloud.firestore.document.v1.written \
        --event-filters=database='(default)' \
        --event-data-content-type=application/protobuf \
        --event-filters-path-pattern=document='users/{username}' \
        --service-account=PROJECT_NUMBER-compute@developer.gserviceaccount.com
    

    取代:

    • TRIGGER_NAME 替換為觸發條件的名稱。

    • EVENTARC_TRIGGER_LOCATION 與 Eventarc 觸發條件的所在位置。一般來說,Eventarc 觸發事件的位置應與您要監控事件的 Google Cloud 資源位置相符。在大多數情況下,您也應在相同的區域中部署函式。詳情請參閱「Eventarc 位置」。

    • FUNCTION 改為您要部署的函式名稱。

    • REGION 與函式的 Cloud Run region 搭配使用。

    • PROJECT_NUMBER 換成您的 Google Cloud 專案編號。Eventarc 觸發事件會連結至服務帳戶,以便在叫用函式時做為身分識別資訊使用。Eventarc 觸發事件的服務帳戶必須具備叫用函式的權限。根據預設,Cloud Run 會使用預設的 Compute 服務帳戶。

    • event-filters 標記可指定觸發事件監控的事件篩選器。符合所有 event-filters 的事件會觸發對函式的呼叫,每個觸發條件都必須有支援的事件類型。建立後即無法變更事件篩選器類型。如要變更事件篩選器類型,您必須建立新的觸發條件,並刪除舊的觸發條件。您可以選擇在 ATTRIBUTE=VALUE 表單中重複使用支援的篩選器,藉此新增更多篩選器。--event-filters

Terraform

如要為 Cloud Run 函式建立 Eventarc 觸發條件,請參閱「使用 Terraform 建立觸發條件」一文。

其他欄位則不必修改:

  • --event-filters=type=google.cloud.firestore.document.v1.written 會指定在建立、更新或刪除文件時,根據 google.cloud.firestore.document.v1.written 事件類型觸發函式。
  • --event-filters=database='(default)' 會指定 Firebase 資料庫。如要使用預設資料庫名稱,請使用 (default)
  • --event-filters-path-pattern=document='users/{username}' 會提供應監控相關變更的文件路徑模式。這個路徑模式表示應監控 users 集合中的所有文件。詳情請參閱「瞭解路徑模式」。

測試 Hello Firestore 函式

如要測試 Hello Firestore 函式,請在 Firestore 資料庫中設定名為 users 的集合:

  1. 在 Google Cloud 控制台中,前往「Firestore 資料庫」頁面:

    前往 Firestore

  2. 按一下「啟動集合」

  3. users 指定為集合 ID。

  4. 如要開始新增集合的第一份文件,請在「新增第一份文件」下方接受系統自動產生的「文件 ID」

  5. 為文件新增至少一個欄位,並指定名稱和值。例如,在「欄位名稱」中輸入 username,並在「欄位值」中輸入 rowan

  6. 完成時,請按一下 [Save] (儲存)

    這項操作會建立新文件,進而觸發函式。

  7. 如要確認函式是否已觸發,請在 Google Cloud 控制台的 Cloud Run 總覽頁面中,按一下函式的連結名稱,開啟「Service details」(服務詳細資料) 頁面。

  8. 選取「Logs」分頁標籤,然後尋找以下字串:

Function triggered by change to: //firestore.googleapis.com/projects/your-project-id/databases/(default)'

範例 2:Convert to Uppercase 函式

以下範例會擷取使用者新增的值,將該位置的字串轉換為大寫,並以大寫字串取代該值:

Node.js

使用 protobufjs 解碼事件資料。在來源中加入 google.events.cloud.firestore.v1 data.proto

const functions = require('@google-cloud/functions-framework');
const Firestore = require('@google-cloud/firestore');
const protobuf = require('protobufjs');

const firestore = new Firestore({
  projectId: process.env.GOOGLE_CLOUD_PROJECT,
});

// Converts strings added to /messages/{pushId}/original to uppercase
functions.cloudEvent('makeUpperCase', async cloudEvent => {
  console.log('Loading protos...');
  const root = await protobuf.load('data.proto');
  const DocumentEventData = root.lookupType(
    'google.events.cloud.firestore.v1.DocumentEventData'
  );

  console.log('Decoding data...');
  const firestoreReceived = DocumentEventData.decode(cloudEvent.data);

  const resource = firestoreReceived.value.name;
  const affectedDoc = firestore.doc(resource.split('/documents/')[1]);

  const curValue = firestoreReceived.value.fields.original.stringValue;
  const newValue = curValue.toUpperCase();

  if (curValue === newValue) {
    // Value is already upper-case
    // Don't perform a(nother) write to avoid infinite loops
    console.log('Value is already upper-case.');
    return;
  }

  console.log(`Replacing value: ${curValue} --> ${newValue}`);
  affectedDoc.set({
    original: newValue,
  });
});

Python

from cloudevents.http import CloudEvent
import functions_framework
from google.cloud import firestore
from google.events.cloud import firestore as firestoredata

client = firestore.Client()


# Converts strings added to /messages/{pushId}/original to uppercase
@functions_framework.cloud_event
def make_upper_case(cloud_event: CloudEvent) -> None:
    firestore_payload = firestoredata.DocumentEventData()
    firestore_payload._pb.ParseFromString(cloud_event.data)

    path_parts = firestore_payload.value.name.split("/")
    separator_idx = path_parts.index("documents")
    collection_path = path_parts[separator_idx + 1]
    document_path = "/".join(path_parts[(separator_idx + 2) :])

    print(f"Collection path: {collection_path}")
    print(f"Document path: {document_path}")

    affected_doc = client.collection(collection_path).document(document_path)

    cur_value = firestore_payload.value.fields["original"].string_value
    new_value = cur_value.upper()

    if cur_value != new_value:
        print(f"Replacing value: {cur_value} --> {new_value}")
        affected_doc.set({"original": new_value})
    else:
        # Value is already upper-case
        # Don't perform a second write (which can trigger an infinite loop)
        print("Value is already upper-case.")

Go


// Package upper contains a Firestore Cloud Function.
package upper

import (
	"context"
	"errors"
	"fmt"
	"log"
	"os"
	"strings"

	"cloud.google.com/go/firestore"
	firebase "firebase.google.com/go/v4"
	"github.com/GoogleCloudPlatform/functions-framework-go/functions"
	"github.com/cloudevents/sdk-go/v2/event"
	"github.com/googleapis/google-cloudevents-go/cloud/firestoredata"
	"google.golang.org/protobuf/proto"
)

// set the GOOGLE_CLOUD_PROJECT environment variable when deploying.
var projectID = os.Getenv("GOOGLE_CLOUD_PROJECT")

// client is a Firestore client, reused between function invocations.
var client *firestore.Client

func init() {
	// Use the application default credentials.
	conf := &firebase.Config{ProjectID: projectID}

	// Use context.Background() because the app/client should persist across
	// invocations.
	ctx := context.Background()

	app, err := firebase.NewApp(ctx, conf)
	if err != nil {
		log.Fatalf("firebase.NewApp: %v", err)
	}

	client, err = app.Firestore(ctx)
	if err != nil {
		log.Fatalf("app.Firestore: %v", err)
	}

	// Register cloud event function
	functions.CloudEvent("MakeUpperCase", MakeUpperCase)
}

// MakeUpperCase is triggered by a change to a Firestore document. It updates
// the `original` value of the document to upper case.
func MakeUpperCase(ctx context.Context, e event.Event) error {
	var data firestoredata.DocumentEventData

	// If you omit `DiscardUnknown`, protojson.Unmarshal returns an error
	// when encountering a new or unknown field.
	options := proto.UnmarshalOptions{
		DiscardUnknown: true,
	}
	err := options.Unmarshal(e.Data(), &data)

	if err != nil {
		return fmt.Errorf("proto.Unmarshal: %w", err)
	}

	if data.GetValue() == nil {
		return errors.New("Invalid message: 'Value' not present")
	}

	fullPath := strings.Split(data.GetValue().GetName(), "/documents/")[1]
	pathParts := strings.Split(fullPath, "/")
	collection := pathParts[0]
	doc := strings.Join(pathParts[1:], "/")

	var originalStringValue string
	if v, ok := data.GetValue().GetFields()["original"]; ok {
		originalStringValue = v.GetStringValue()
	} else {
		return errors.New("Document did not contain field \"original\"")
	}

	newValue := strings.ToUpper(originalStringValue)
	if originalStringValue == newValue {
		log.Printf("%q is already upper case: skipping", originalStringValue)
		return nil
	}
	log.Printf("Replacing value: %q -> %q", originalStringValue, newValue)

	newDocumentEntry := map[string]string{"original": newValue}
	_, err = client.Collection(collection).Doc(doc).Set(ctx, newDocumentEntry)
	if err != nil {
		return fmt.Errorf("Set: %w", err)
	}
	return nil
}

Java

import com.google.cloud.firestore.Firestore;
import com.google.cloud.firestore.FirestoreOptions;
import com.google.cloud.firestore.SetOptions;
import com.google.cloud.functions.CloudEventsFunction;
import com.google.events.cloud.firestore.v1.DocumentEventData;
import com.google.events.cloud.firestore.v1.Value;
import com.google.protobuf.InvalidProtocolBufferException;
import io.cloudevents.CloudEvent;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.logging.Logger;

public class FirebaseFirestoreReactive implements CloudEventsFunction {
  private static final Logger logger = Logger.getLogger(FirebaseFirestoreReactive.class.getName());
  private final Firestore firestore;

  private static final String FIELD_KEY = "original";
  private static final String APPLICATION_PROTOBUF = "application/protobuf";

  public FirebaseFirestoreReactive() {
    this(FirestoreOptions.getDefaultInstance().getService());
  }

  public FirebaseFirestoreReactive(Firestore firestore) {
    this.firestore = firestore;
  }

  @Override
  public void accept(CloudEvent event)
      throws InvalidProtocolBufferException, InterruptedException, ExecutionException {
    if (event.getData() == null) {
      logger.warning("No data found in event!");
      return;
    }

    if (!event.getDataContentType().equals(APPLICATION_PROTOBUF)) {
      logger.warning(String.format("Found unexpected content type %s, expected %s",
          event.getDataContentType(),
          APPLICATION_PROTOBUF));
      return;
    }

    DocumentEventData firestoreEventData = DocumentEventData
        .parseFrom(event.getData().toBytes());

    // Get the fields from the post-operation document snapshot
    // https://firebase.google.com/docs/firestore/reference/rest/v1/projects.databases.documents#Document
    Map<String, Value> fields = firestoreEventData.getValue().getFieldsMap();
    if (!fields.containsKey(FIELD_KEY)) {
      logger.warning("Document does not contain original field");
      return;
    }
    String currValue = fields.get(FIELD_KEY).getStringValue();
    String newValue = currValue.toUpperCase();

    if (currValue.equals(newValue)) {
      logger.info("Value is already upper-case");
      return;
    }

    // Retrieve the document name from the resource path:
    // projects/{project_id}/databases/{database_id}/documents/{document_path}
    String affectedDoc = firestoreEventData.getValue()
        .getName()
        .split("/documents/")[1]
        .replace("\"", "");

    logger.info(String.format("Replacing values: %s --> %s", currValue, newValue));

    // Wait for the async call to complete
    this.firestore
        .document(affectedDoc)
        .set(Map.of(FIELD_KEY, newValue), SetOptions.merge())
        .get();
  }
}

C#

using CloudNative.CloudEvents;
using Google.Cloud.Firestore;
using Google.Cloud.Functions.Framework;
using Google.Cloud.Functions.Hosting;
using Google.Events.Protobuf.Cloud.Firestore.V1;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace FirestoreReactive;

public class Startup : FunctionsStartup
{
    public override void ConfigureServices(WebHostBuilderContext context, IServiceCollection services) =>
        services.AddSingleton(FirestoreDb.Create());
}

// Register the startup class to provide the Firestore dependency.
[FunctionsStartup(typeof(Startup))]
public class Function : ICloudEventFunction<DocumentEventData>
{
    private readonly ILogger _logger;
    private readonly FirestoreDb _firestoreDb;

    public Function(ILogger<Function> logger, FirestoreDb firestoreDb) =>
        (_logger, _firestoreDb) = (logger, firestoreDb);

    public async Task HandleAsync(CloudEvent cloudEvent, DocumentEventData data, CancellationToken cancellationToken)
    {
        // Get the recently-written value. This expression will result in a null value
        // if any of the following is true:
        // - The event doesn't contain a "new" document
        // - The value doesn't contain a field called "original"
        // - The "original" field isn't a string
        string currentValue = data.Value?.ConvertFields().GetValueOrDefault("original") as string;
        if (currentValue is null)
        {
            _logger.LogWarning($"Event did not contain a suitable document");
            return;
        }

        string newValue = currentValue.ToUpperInvariant();
        if (newValue == currentValue)
        {
            _logger.LogInformation("Value is already upper-cased; no replacement necessary");
            return;
        }

        // The CloudEvent subject is "documents/x/y/...".
        // The Firestore SDK FirestoreDb.Document method expects a reference relative to
        // "documents" (so just the "x/y/..." part). This may be simplified over time.
        if (cloudEvent.Subject is null || !cloudEvent.Subject.StartsWith("documents/"))
        {
            _logger.LogWarning("CloudEvent subject is not a document reference.");
            return;
        }
        string documentPath = cloudEvent.Subject.Substring("documents/".Length);

        _logger.LogInformation("Replacing '{current}' with '{new}' in '{path}'", currentValue, newValue, documentPath);
        await _firestoreDb.Document(documentPath).UpdateAsync("original", newValue, cancellationToken: cancellationToken);
    }
}

部署 Convert to Uppercase 函式

如果您尚未設定,請先設定 Firestore 資料庫

按一下分頁標籤,瞭解如何使用自選工具。

主控台

使用 Google Cloud 控制台建立函式時,您也可以為函式新增觸發條件。請按照下列步驟為函式建立觸發條件:

  1. 前往 Google Cloud 控制台的 Cloud Run:

    前往 Cloud Run

  2. 按一下「編寫函式」,然後輸入函式詳細資料。如要進一步瞭解如何在部署期間設定函式,請參閱「部署函式」。

  3. 在「觸發條件」部分中,按一下「新增觸發條件」

  4. 選取「Firestore 觸發條件」

  5. 在「Eventarc trigger」窗格中,修改觸發條件詳細資料,如下所示:

    1. 在「Trigger name」欄位中輸入觸發事件名稱,或使用預設名稱。

    2. 從清單中選取「觸發條件類型」,指定下列其中一種觸發條件類型:

      • Google 來源:指定 Pub/Sub、Cloud Storage、Firestore 和其他 Google 事件供應器的觸發事件。

      • 第三方:整合提供 Eventarc 來源的非 Google 供應商。詳情請參閱「Eventarc 中的第三方事件」。

    3. 從「Event provider」清單中選取「Firestore」Firestore,即可選取提供事件類型以觸發函式的產品。如需事件提供者清單,請參閱「事件提供者和目的地」。

    4. 在「Event type」清單中,選取「type=google.cloud.firestore.document.v1.written」。觸發條件設定會因支援的事件類型而異。詳情請參閱「事件類型」。

    5. 在「篩選器」部分,選取資料庫、運算和屬性值,或使用預設選項。

    6. 如果已啟用「區域」欄位,請為 Eventarc 觸發條件選取「位置」。一般來說,Eventarc 觸發條件的位置應與您要監控事件的 Google Cloud 資源位置一致。在大多數情況下,您也應在相同的地區部署函式。如要進一步瞭解 Eventarc 觸發條件的所在位置,請參閱「瞭解 Eventarc 位置」。

    7. 在「服務帳戶」欄位中,選取服務帳戶。Eventarc 觸發事件會連結至服務帳戶,以便在叫用函式時做為身分使用。Eventarc 觸發事件的服務帳戶必須具備叫用函式的權限。根據預設,Cloud Run 會使用 Compute Engine 預設服務帳戶

    8. 您可以視需要指定要傳送傳入要求的服務網址路徑。這是目的地服務中的相對路徑,應將觸發事件傳送至此路徑。例如://routerouteroute/subroute

  6. 填妥必填欄位後,按一下「儲存觸發條件」

gcloud

使用 gcloud CLI 建立函式時,您必須先部署函式,然後再建立觸發條件。請按照下列步驟為函式建立觸發條件:

  1. 在包含程式碼範例的目錄中執行下列指令,即可部署函式:

    gcloud run deploy FUNCTION \
            --source . \
            --function FUNCTION_ENTRYPOINT \
            --base-image BASE_IMAGE_ID \
            --region REGION
    

    取代:

    • FUNCTION 替換為您要部署的函式名稱。您可以將這個參數完全省略,這樣系統會提示您輸入名稱。

    • FUNCTION_ENTRYPOINT 與原始碼中函式的進入點。這是 Cloud Run 在函式執行時執行的程式碼。這個標記的值必須是來源程式碼中存在的函式名稱或完全限定的類別名稱。

    • BASE_IMAGE_ID 與函式的基礎映像檔環境。如要進一步瞭解基礎映像檔和各個映像檔所包含的套件,請參閱「執行階段基礎映像檔」。

    • REGION 與您要部署函式的 Google Cloud 區域。例如:us-central1

  2. 執行下列指令,建立用於篩選事件的觸發條件:

    gcloud eventarc triggers create TRIGGER_NAME  \
        --location=EVENTARC_TRIGGER_LOCATION \
        --destination-run-service=FUNCTION  \
        --destination-run-region=REGION \
        --event-filters=type=google.cloud.firestore.document.v1.written \
        --event-filters=database='(default)' \
        --event-data-content-type=application/protobuf \
        --event-filters-path-pattern=document='messages/{pushId}' \
        --service-account=PROJECT_NUMBER-compute@developer.gserviceaccount.com
    

    取代:

    • TRIGGER_NAME 替換為觸發條件的名稱。

    • EVENTARC_TRIGGER_LOCATION 與 Eventarc 觸發條件的所在位置。一般來說,Eventarc 觸發事件的位置應與您要監控事件的 Google Cloud 資源位置相符。在大多數情況下,您也應在相同的區域中部署函式。詳情請參閱「Eventarc 位置」。

    • FUNCTION 改為您要部署的函式名稱。

    • REGION 與函式的 Cloud Run region 搭配使用。

    • PROJECT_NUMBER 換成您的 Google Cloud 專案編號。Eventarc 觸發事件會連結至服務帳戶,以便在叫用函式時做為身分識別資訊使用。Eventarc 觸發事件的服務帳戶必須具備叫用函式的權限。根據預設,Cloud Run 會使用預設的 Compute 服務帳戶。

    • event-filters 標記可指定觸發事件監控的事件篩選器。符合所有 event-filters 的事件會觸發對函式的呼叫,每個觸發條件都必須有支援的事件類型。建立後即無法變更事件篩選器類型。如要變更事件篩選器類型,您必須建立新的觸發條件,並刪除舊的觸發條件。您可以選擇在 ATTRIBUTE=VALUE 表單中重複使用支援的篩選器,藉此新增更多篩選器。--event-filters

Terraform

如要為 Cloud Run 函式建立 Eventarc 觸發條件,請參閱「使用 Terraform 建立觸發條件」一文。

其他欄位則不需修改:

  • --event-filters=type=google.cloud.firestore.document.v1.written 會指定在建立、更新或刪除文件時,根據 google.cloud.firestore.document.v1.written 事件類型觸發函式。
  • --event-filters=database='(default)' 會指定 Firestore 資料庫。如要使用預設資料庫名稱,請使用 (default)
  • --event-filters-path-pattern=document='messages/{pushId}' 會提供應監控的文件路徑模式,以便監控相關變更。這個路徑模式表示應監控 messages 集合中的所有文件。詳情請參閱「瞭解路徑模式」。

測試 Convert to Uppercase 函式

如要測試剛部署的 Convert to Uppercase 函式,請在 Firestore 資料庫中設定名為 messages 的集合:

  1. 在 Google Cloud 控制台中,前往「Firestore 資料庫」頁面:

    前往 Firestore

  2. 按一下「啟動集合」

  3. messages 指定為集合 ID。

  4. 如要開始新增集合的第一份文件,請在「新增第一份文件」下方接受系統自動產生的「文件 ID」

  5. 如要觸發已部署的函式,請新增一個「欄位名稱」original 且「欄位值」minka 的文件。

  6. 儲存文件時,您會看到值欄中的小寫字詞轉換為大寫。

    如果您之後編輯欄位值,使其包含小寫字母,系統會再次觸發函式,將所有小寫字母轉換為大寫。

函式限制

  • 我們不保證排序。快速變更可能會以非預期的順序觸發函式叫用。
  • 事件至少會傳送一次,但單一事件可能會導致多次函式叫用。避免依賴「一次一律」機制,並編寫冪等函式
  • 觸發條件會與單一資料庫相關聯。您無法建立符合多個資料庫的觸發條件。
  • 刪除資料庫時,系統不會自動刪除該資料庫的任何觸發事件。觸發事件會停止傳送事件,但會持續存在,直到您刪除觸發事件為止。