使用 ARM 模板将 Blob 存储事件路由到 Web 终结点Route Blob storage events to web endpoint by using an ARM template

Azure 事件网格是针对云的事件处理服务。Azure Event Grid is an eventing service for the cloud. 本文将使用 Azure 资源管理器模板(ARM 模板)创建 Blob 存储帐户、订阅该 Blob 存储的事件,并触发事件来查看结果。In this article, you use an Azure Resource Manager template (ARM template) to create a Blob storage account, subscribe to events for that blob storage, and trigger an event to view the result. 通常,你会将事件发送到处理事件数据并执行操作的终结点。Typically, you send events to an endpoint that processes the event data and takes actions. 但是,为了简化本文,你将事件发送到收集并显示消息的 Web 应用。However, to simplify this article, you send the events to a web app that collects and displays the messages.

ARM 模板是定义项目基础结构和配置的 JavaScript 对象表示法 (JSON) 文件。An ARM template is a JavaScript Object Notation (JSON) file that defines the infrastructure and configuration for your project. 该模板使用声明性语法,使你可以声明要部署的内容,而不需要编写一系列编程命令来进行创建。The template uses declarative syntax, which lets you state what you intend to deploy without having to write the sequence of programming commands to create it.

如果你的环境满足先决条件,并且你熟悉如何使用 ARM 模板,请选择“部署到 Azure”按钮。If your environment meets the prerequisites and you're familiar with using ARM templates, select the Deploy to Azure button. Azure 门户中会打开模板。The template will open in the Azure portal.

部署到 AzureDeploy to Azure

先决条件Prerequisites

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a Trial before you begin.

创建消息终结点Create a message endpoint

在订阅 Blob 存储的事件之前,让我们创建事件消息的终结点。Before subscribing to the events for the Blob storage, let's create the endpoint for the event message. 通常情况下,终结点基于事件数据执行操作。Typically, the endpoint takes actions based on the event data. 为了简化此快速入门,将部署用于显示事件消息的预建的 Web 应用To simplify this quickstart, you deploy a pre-built web app that displays the event messages. 所部署的解决方案包括应用服务计划、应用服务 Web 应用和 GitHub 中的源代码。The deployed solution includes an App Service plan, an App Service web app, and source code from GitHub.

  1. 选择“部署到 Azure”将解决方案部署到你的订阅。Select Deploy to Azure to deploy the solution to your subscription. 在 Azure 门户中,为参数提供值。In the Azure portal, provide values for the parameters.

    部署到 AzureDeploy to Azure

  2. 部署可能需要几分钟才能完成。The deployment may take a few minutes to complete. 部署成功后,请查看 Web 应用以确保它正在运行。After the deployment has succeeded, view your web app to make sure it's running. 在 Web 浏览器中导航到 https://<your-site-name>.chinacloudsites.cnIn a web browser, navigate to: https://<your-site-name>.chinacloudsites.cn

  3. 查看站点,但是尚未有事件发布到它。You see the site but no events have been posted to it yet.

    查看新站点

查看模板Review the template

本快速入门中使用的模板来自 Azure 快速入门模板The template used in this quickstart is from Azure Quickstart templates.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "storageName": {
            "type": "string",
            "defaultValue": "[concat('storage', 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": [
        {
            "name": "[parameters('storageName')]",
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2017-10-01",
            "sku": {
                "name": "Standard_LRS"
            },
            "kind": "StorageV2",
            "location": "[parameters('location')]",
            "tags": {},
            "properties": {
                "accessTier": "Hot"
            }
        },
        {
            "name": "[parameters('systemTopicName')]",
            "type": "Microsoft.EventGrid/systemTopics",
            "apiVersion": "2020-04-01-preview",
            "location": "[parameters('location')]",
            "dependsOn": [
                "[parameters('storageName')]"
            ],
            "properties": {
                "source": "[resourceId('Microsoft.Storage/storageAccounts', parameters('storageName'))]",
                "topicType": "Microsoft.Storage.StorageAccounts"
            }
        },
        {
            "type": "Microsoft.EventGrid/systemTopics/eventSubscriptions",
            "apiVersion": "2020-04-01-preview",
            "name": "[concat(parameters('systemTopicName'), '/', parameters('eventSubName'))]",
            "dependsOn": [
                "[resourceId('Microsoft.EventGrid/systemTopics', parameters('systemTopicName'))]"
            ],
            "properties": {
                "destination": {
                    "properties": {
                        "endpointUrl": "[parameters('endpoint')]"
                    },
                    "endpointType": "WebHook"
                },
                "filter": {
                    "includedEventTypes": [
                        "Microsoft.Storage.BlobCreated",
                        "Microsoft.Storage.BlobDeleted"
                    ]
                }
            }
        }
    ]
}

该模板中定义了两个 Azure 资源:Two Azure resources are defined in the template:

部署模板Deploy the template

  1. 选择以下链接登录到 Azure 并打开一个模板。Select the following link to sign in to Azure and open a template. 该模板将创建 Key Vault 和机密。The template creates a key vault and a secret.

    部署到 AzureDeploy to Azure

  2. 指定“终结点”:提供你的 Web 应用的 URL 并将 api/updates 添加到主页 URL。Specify the endpoint: provide the URL of your web app and add api/updates to the home page URL.

  3. 选择“购买”以部署模板。Select Purchase to deploy the template.

此处使用 Azure 门户来部署模板。The Azure portal is used here to deploy the template. 还可以使用 Azure PowerShell、Azure CLI 和 REST API。You can also use the Azure PowerShell, Azure CLI, and REST API. 若要了解其他部署方法,请参阅部署模板To learn other deployment methods, see Deploy templates.

备注

可在此处找到更多 Azure 事件网格模板示例。You can find more Azure Event Grid template samples here.

验证部署Validate the deployment

再次查看 Web 应用,并注意现已向该应用发送了订阅验证事件。View your web app again, and notice that a subscription validation event has been sent to it. 选择眼睛图标以展开事件数据。Select the eye icon to expand the event data. 事件网格发送验证事件,以便终结点可以验证它是否想要接收事件数据。Event Grid sends the validation event so the endpoint can verify that it wants to receive event data. Web 应用包含用于验证订阅的代码。The web app includes code to validate the subscription.

查看订阅事件

现在,让我们触发一个事件,看事件网格如何将消息分发到终结点。Now, let's trigger an event to see how Event Grid distributes the message to your endpoint.

通过上传一个文件来为 Blob 存储触发一个事件。You trigger an event for the Blob storage by uploading a file. 该文件不需要有任何特定内容。The file doesn't need any specific content. 本文假定你有一个名为 testfile.txt 的文件,但是可以使用任何文件。The articles assumes you have a file named testfile.txt, but you can use any file.

将文件上传到 Azure Blob 存储时,事件网格会向你在订阅时配置的终结点发送一条消息。When you upload the file to the Azure Blob storage, Event Grid sends a message to the endpoint you configured when subscribing. 消息采用 JSON 格式,它包含一个或多个事件的数组。The message is in the JSON format and it contains an array with one or more events. 在以下示例中,JSON 消息包含一个事件的数组。In the following example, the JSON message contains an array with one event. 查看你的 Web 应用,将会看到已收到了一个 blob 已创建事件。View your web app and notice that a blob created event was received.

查看结果

清理资源Clean up resources

不再需要资源组时,可将其删除When no longer needed, delete the resource group.

后续步骤Next steps

有关 Azure 资源管理器模板的详细信息,请参阅以下文章:For more information about Azure Resource Manager templates, see the following articles: