本文說明如何更新 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
如要更新訂閱 SMT,請按照下列步驟操作:
控制台
- 
前往 Google Cloud 控制台的 Pub/Sub「Subscriptions」(訂閱項目) 頁面。 
- 
按一下要編輯 SMT 的訂閱項目。 
- 
在訂閱詳細資料頁面中,按一下「編輯」。 「轉換」分頁會列出附加至訂閱項目的所有 SMT。 
- 
在「編輯訂閱項目」頁面中,您可以執行下列工作: - 
新增 SMT。按一下「新增轉換作業」。 
- 
編輯現有的 SMT。展開任一 SMT 即可編輯。 
- 
重新排列 SMT。使用向上鍵和向下鍵。 
- 
刪除 SMT。按一下「刪除」按鈕。 
 
- 
- 
按一下「Update」。 
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 或名稱。 
-