本文說明如何更新 Pub/Sub 訂閱的單一訊息轉換 (SMT)。對 SMT 所做的變更會在幾分鐘內生效。對於訂閱項目 SMT,變更會對更新 UDF 後傳送至訂閱項目的新訊息生效。
如要更新訂閱 SMT,您可以使用 Google Cloud 控制台、Google Cloud CLI、用戶端程式庫或 Pub/Sub API。
必要角色和權限
如要取得更新訂閱 SMT 所需的權限,請要求管理員為您授予專案的 Pub/Sub 編輯者 (roles/pubsub.editor
) 身分與存取權管理角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。
這個預先定義的角色具備更新訂閱項目 SMT 所需的權限。如要查看確切的必要權限,請展開「必要權限」部分:
所需權限
如要更新訂閱項目 SMT,您必須具備下列權限:
-
授予訂閱項目的更新訂閱權限:
projects.subscriptions.patch
-
授予查看專案訂閱權限。只有在使用 Google Cloud 控制台時,才需要這項權限:
pubsub.subscriptions.view
您可以在專案層級和個別資源層級設定存取權控管。
更新訂閱項目 SMT
如要更新訂閱單元,請按照下列步驟操作:
控制台
-
在 Google Cloud 控制台中,前往 Pub/Sub 的「Subscriptions」(訂閱項目)頁面。
-
按一下要編輯的訂閱項目。
-
在訂閱詳細資料頁面中,按一下「編輯」。
「轉換」分頁會列出已附加至訂閱項目的所有 SMT。
-
在「編輯訂閱項目」頁面中,您可以執行下列作業:
-
新增 SMT。按一下「新增轉換」。
-
編輯現有的 SMT。展開任何 SMT 即可編輯 SMT。
-
重新排列 SMT。使用向上鍵和向下鍵。
-
刪除 SMT。按一下刪除按鈕。
-
-
按一下「更新」。
gcloud
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
-
執行
gcloud pubsub subscriptions update
指令:gcloud pubsub subscriptions update SUBSCRIPTION_ID \ --message-transforms-file=TRANSFORMS_FILE
更改下列內容:
-
SUBSCRIPTION_ID:要更新的訂閱項目 ID 或名稱。
-
TRANSFORMS_FILE:包含更新後的 SMT 的 YAML 或 JSON 檔案路徑。
以下是 YAML 轉換檔案範例:
- javascriptUdf: code: > function redactSSN(message, metadata) { const data = JSON.parse(message.data); delete data['ssn']; message.data = JSON.stringify(data); return message; } functionName: redactSSN - javascriptUdf: code: > function filterHighAmount(message, metadata) { const data = JSON.parse(message.data); if (data['amount'] > 100 ) { return null; } return message; } functionName: filterHighAmount
如要清除訂閱項目中的所有 SMT,請使用下列指令:
gcloud pubsub subscriptions update SUBSCRIPTION_ID --clear-message-transforms
將 SUBSCRIPTION_ID 替換為您要更新的訂閱項目 ID 或名稱。
-