Share via


快速入門:使用ARM範本將 Blob 記憶體事件路由至 Web 端點

Azure Event Grid 是一項雲端事件服務。 在本文中,您會使用 Azure Resource Manager 範本 (ARM 範本) 來建立 Blob 儲存器帳戶、訂閱該 Blob 記憶體的事件,並觸發事件來檢視結果。 通常,您會將事件傳送至可處理事件資料及採取行動的端點。 不過,若要簡化這篇文章,您可將事件傳送至可收集及顯示訊息的 Web 應用程式。

Azure Resource Manager 範本是 JavaScript 物件表示法 (JSON) 檔案,可定義專案的基礎結構和組態。 範本使用宣告式語法。 您不需要撰寫程式設計命令順序來建立部署,即可描述預定的部署。

如果您的環境符合必要條件,而且您很熟悉 ARM 範本,請選取 [部署至 Azure] 按鈕。 範本會在 Azure 入口網站中開啟。

Button to deploy the Resource Manager template to Azure.

必要條件

如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶

建立訊息端點

在訂閱 Blob 儲存體的事件之前,我們要先建立事件訊息的端點。 端點通常會根據事件資料來採取動作。 若要簡化此快速入門,請部署預先建置的 Web 應用程式以顯示事件訊息。 部署的解決方案包括 App Service 方案、App Service Web 應用程式,以及 GitHub 的原始程式碼。

  1. 選取 [部署至 Azure],將解決方案部署至您的訂用帳戶。 在 Azure 入口網站中,提供參數的值。

    部署至 Azure

  2. 部署需要幾分鐘的時間才能完成。 成功部署之後,檢視 Web 應用程式,確定它正在執行。 在網頁瀏覽器中,瀏覽至:https://<your-site-name>.azurewebsites.net

  3. 您看到網站,但其中尚未發佈任何事件。

    View new site

檢閱範本

本快速入門中使用的範本是來自 Azure 快速入門範本

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
      "name": "bicep",
      "version": "0.26.54.24096",
      "templateHash": "17805408638569592847"
    }
  },
  "parameters": {
    "storageAccountName": {
      "type": "string",
      "defaultValue": "[format('storage{0}', uniqueString(resourceGroup().id))]",
      "metadata": {
        "description": "Provide a unique name for the Blob Storage account."
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Provide a location for the Blob Storage account that supports Event Grid."
      }
    },
    "eventSubName": {
      "type": "string",
      "defaultValue": "subToStorage",
      "metadata": {
        "description": "Provide a name for the Event Grid subscription."
      }
    },
    "endpoint": {
      "type": "string",
      "metadata": {
        "description": "Provide the URL for the WebHook to receive events. Create your own endpoint for events."
      }
    },
    "systemTopicName": {
      "type": "string",
      "defaultValue": "mystoragesystemtopic",
      "metadata": {
        "description": "Provide a name for the system topic."
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2023-01-01",
      "name": "[parameters('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "StorageV2",
      "properties": {
        "accessTier": "Hot",
        "minimumTlsVersion": "TLS1_2",
        "supportsHttpsTrafficOnly": true,
        "allowBlobPublicAccess": false
      }
    },
    {
      "type": "Microsoft.EventGrid/systemTopics",
      "apiVersion": "2023-12-15-preview",
      "name": "[parameters('systemTopicName')]",
      "location": "[parameters('location')]",
      "identity": {
        "type": "SystemAssigned"
      },
      "properties": {
        "source": "[resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName'))]",
        "topicType": "Microsoft.Storage.StorageAccounts"
      },
      "dependsOn": [
        "[resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName'))]"
      ]
    },
    {
      "type": "Microsoft.EventGrid/systemTopics/eventSubscriptions",
      "apiVersion": "2023-12-15-preview",
      "name": "[format('{0}/{1}', parameters('systemTopicName'), parameters('eventSubName'))]",
      "properties": {
        "destination": {
          "properties": {
            "endpointUrl": "[parameters('endpoint')]"
          },
          "endpointType": "WebHook"
        },
        "filter": {
          "includedEventTypes": [
            "Microsoft.Storage.BlobCreated",
            "Microsoft.Storage.BlobDeleted"
          ]
        }
      },
      "dependsOn": [
        "[resourceId('Microsoft.EventGrid/systemTopics', parameters('systemTopicName'))]"
      ]
    }
  ],
  "outputs": {
    "name": {
      "type": "string",
      "value": "[parameters('eventSubName')]"
    },
    "resourceId": {
      "type": "string",
      "value": "[resourceId('Microsoft.EventGrid/systemTopics/eventSubscriptions', parameters('systemTopicName'), parameters('eventSubName'))]"
    },
    "resourceGroupName": {
      "type": "string",
      "value": "[resourceGroup().name]"
    },
    "location": {
      "type": "string",
      "value": "[parameters('location')]"
    }
  }
}

範本中定義了兩個 Azure 資源:

部署範本

  1. 選取下列連結以登入 Azure 並開啟範本。 此範本會建立金鑰保存庫和祕密。

    Button to deploy the Resource Manager template to Azure.

  2. 指定 端點:提供 Web 應用程式的 URL,並將 新增 api/updates 至首頁 URL。

  3. 選取 [ 購買 ] 以部署範本。

此處會使用 Azure 入口網站 來部署範本。 您也可以使用 Azure PowerShell、Azure CLI 和 REST API。 若要了解其他部署方法,請參閱<部署範本>。

注意

您可以在這裡找到更多 Azure 事件方格範本範例。

驗證部署

再次檢視您的 Web 應用程式,並注意訂閱驗證事件是否已傳送至其中。 選取眼睛圖示以展開事件資料。 事件方格會傳送驗證事件,以便端點確認接收事件資料。 Web 應用程式包括用於驗證訂閱的程式碼。

View subscription event

現在,讓我們觸發事件以了解 Event Grid 如何將訊息散發至您的端點。

您可以藉由上傳檔案來觸發 Blob 儲存體的事件。 此檔案不需要任何特定內容。 相關文章假設您具有名為 testfile.txt 的檔案,但是您可以使用任何檔案。

當您將檔案上傳至 Azure Blob 儲存體時,事件方格會將訊息傳送至您在訂閱時設定的端點。 訊息為 JSON 格式,且其包含具有一或多個事件的陣列。 在下列範例中,JSON 訊息會包含具有單一事件的陣列。 檢視您的 Web 應用程式,並留意已接收到 Blob 所建立的事件。

View results

清除資源

當不再需要時,請刪除資源群組

下一步

如需 Azure Resource Manager 範本的詳細資訊,請參閱下列文章: