在您新增通知設定後,Storage 移轉服務會針對任何已啟動的 TransferOperations 傳送通知,並保證至少向 Pub/Sub 傳送一次。Pub/Sub 也會至少向接收者傳送一次,這表示您可以收到代表相同 Storage Transfer Service 事件、具有多個 ID 的多個訊息。
系統無法保證能以 Pub/Sub 接收通知的順序發布通知。
如果系統持續無法將通知傳送至 Pub/Sub 主題,Storage 移轉服務可能會在 7 天後刪除該項通知。以下的情況可能會導致傳送失敗:Pub/Sub 主題已不存在、Storage Transfer Service 不再具有發布主題的權限,或擁有主題的專案超過發布配額。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-03 (世界標準時間)。"],[],[],null,["# Pub/Sub notifications\n\nAfter you complete a Storage Transfer Service transfer, you might want to start another\ntask, handle an error encountered during a transfer, or log that a transfer\noccurred. Pub/Sub provides a queue that your programs subscribe to\nto receive messages when a transfer completes. This enables you to reuse code\nand to program next steps based on the state of a transfer.\n\nFor more information about Pub/Sub, see [What is Cloud\nPub/Sub](/pubsub/docs/overview)\n\nPrerequisites\n-------------\n\nBefore you can use this feature, do the following:\n\n1. [Enable the Pub/Sub API](/service-usage/docs/enable-disable)\n for the project receiving Pub/Sub notifications.\n\n [Enable\n the API](https://console.cloud.google.com/flows/enableapi?apiid=pubsub)\n2. [Have an existing Pub/Sub topic](/pubsub/docs/create-topic-console#create_a_topic)\n that you want to send notifications to.\n\n3. [Get the email address of the service account](/storage-transfer/docs/reference/rest/v1/googleServiceAccounts)\n associated with the project that contains your Storage Transfer Service\n bucket.\n\n 1. [Grant the service account the IAM role `roles.pubsub.publisher`](/pubsub/docs/access-control#console) for the desired Pub/Sub topic.\n\n | **Note:** There might be a delay of several seconds between assigning the role and having it applied to your service account. If you grant this permission programmatically, wait 30 seconds before configuring Storage Transfer Service.\n\nConfiguring notification settings\n---------------------------------\n\nTo configure Storage Transfer Service Pub/Sub notification settings,\nyou use the\n[Storage Transfer Service transferJobs API](/storage-transfer/docs/reference/rest/v1/transferJobs), to\ncreate `NotificationConfig` message. The topic will receive notifications on the\nconfigured Pub/Sub channel.\n\nSpecify the following in your `NotificationConfig` message:\n\n- The Pub/Sub topic to send notifications to\n- The format of the message, `\"JSON\"` or `\"NONE\"`\n- The desired event types, corresponding to completed\n [`TransferOperation.Statuses`](/storage-transfer/docs/reference/rest/v1/transferOperations#status):\n\n - `\"TRANSFER_OPERATION_SUCCESS\"`\n - `\"TRANSFER_OPERATION_FAILED\"`\n - `\"TRANSFER_OPERATION_ABORTED\"`\n\nThe following is an example `NotificationConfig` message: \n\n {\n ...\n \"notificationConfig\": {\n \"pubsubTopic\": \"projects/\u003cvar translate=\"no\"\u003eproject-id\u003c/var\u003e/topics/\u003cvar translate=\"no\"\u003etopic-id\u003c/var\u003e\",\n \"eventTypes\": [\"TRANSFER_OPERATION_SUCCESS\"],\n \"payloadFormat\": \"JSON\"\n },\n ...\n }\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eproject-id\u003c/var\u003e: The Google Cloud project ID of the transfer\n- \u003cvar translate=\"no\"\u003etopic-id\u003c/var\u003e: The Pub/Sub topic name\n\nFor more information, see [Storage Transfer Service Pub Sub REST specification](/storage-transfer/docs/reference/rest/v1/NotificationConfig).\n\nNotification format\n-------------------\n\nNotifications sent to the Pub/Sub topic consist of two parts:\n\n- **Attributes**: A set of key:value pairs describing the event.\n- **Payload**: A string that contains the metadata of the changed object.\n\n### Attributes\n\nAttributes are key:value pairs contained in all notifications sent by\nStorage Transfer Service to your Pub/Sub topic. Notifications always\ncontain the following set of key:value pairs, regardless of the notification's\npayload:\n\nSee [`PubsubMessage`](/pubsub/docs/reference/rest/v1/PubsubMessage) for more\ninformation about the Pub/Sub message format.\n\n### Payload\n\nThe payload contains the `TransferOperation` metadata. When you create a\nnotification configuration, you specify a type of payload to include in\nnotifications triggered by that configuration. You can specify the following\ntypes of payload:\n\nExample Pub/Sub notification configurations\n-------------------------------------------\n\n### Notifying for failed transfers only\n\nTo receive messages for failed transfers only, submit a `TransferJob` with a\n`NotificationConfig` that filters for failed transfers only: \n\n // REST JSON format:\n // https://cloud.google.com/storage-transfer/docs/create-manage-transfer-program\n {\n ...\n \"notificationConfig\": {\n \"pubsubTopic\": \"projects/\u003cvar translate=\"no\"\u003eproject-id\u003c/var\u003e/topics/\u003cvar translate=\"no\"\u003etopic-id\u003c/var\u003e\",\n \"eventTypes\": [\"TRANSFER_OPERATION_FAILED\"],\n \"payloadFormat: \"JSON\"\n },\n ...\n }\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eproject-id\u003c/var\u003e: The Google Cloud project ID of the transfer\n- \u003cvar translate=\"no\"\u003etopic-id\u003c/var\u003e: The Pub/Sub topic name\n\n### Notify on all completed transfers\n\nTo notify on all completed transfers, regardless of status, submit a\n`TransferJob` with a `NotificationConfig` without `EventType` filters: \n\n // REST JSON format:\n // https://cloud.google.com/storage-transfer/docs/create-manage-transfer-program\n {\n ...\n \"notificationConfig\": {\n \"pubsubTopic\": \"projects/\u003cvar translate=\"no\"\u003eproject-id\u003c/var\u003e/topics/\u003cvar translate=\"no\"\u003etopic-id\u003c/var\u003e\",\n \"payloadFormat: \"JSON\"\n },\n ...\n }\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eproject-id\u003c/var\u003e: The Google Cloud project ID of the transfer\n- \u003cvar translate=\"no\"\u003etopic-id\u003c/var\u003e: The Pub/Sub topic name\n\n### Performing arbitrary actions on a transfer\n\nTo perform an arbitrary action, you create a Pub/Sub notification\nusing a\n[Cloud function with a Pub/Sub trigger](/functions/docs/calling/pubsub)\npaired with a [background cloud function](/functions/docs/writing/background).\nFor more information, see the\n[Cloud Pub/Sub tutorial](/functions/docs/tutorials/pubsub).\n\nExamples of arbitrary actions include:\n\n- [Sending an email](/compute/docs/tutorials/sending-mail)\n- [Starting a Dataflow job](/dataflow/docs/quickstarts/create-pipeline-python)\n- [Recording metadata in Cloud SQL](/sql/docs/mysql/connect-functions)\n- [Recording metadata in Spanner](/spanner/docs/reference/libraries)\n\nDelivery guarantees\n-------------------\n\n| **Important:** There is currently no SLA for delivery time, but notifications are typically delivered within seconds. In some circumstances notifications might be delayed substantially longer.\n\nStorage Transfer Service sends notifications for any `TransferOperations` started\nafter you add a notification configuration, and guarantees at-least-once\ndelivery to Pub/Sub. Pub/Sub also offers\nat-least-once delivery to the recipient, which means that you could receive\nmultiple messages, with multiple IDs, that represent the same\nStorage Transfer Service event.\n\nNotifications are not guaranteed to be published in the order\nPub/Sub receives them.\n\nIf a notification consistently cannot be delivered to a Pub/Sub\ntopic, Storage Transfer Service might delete the notification after 7 days. Delivery\nfailure can occur when the Pub/Sub topic no longer exists, when\nStorage Transfer Service no longer has permission to publish to the topic, or when\nthe project that owns the topic exceeds its publishing quota."]]