本文使用Azure Resource Manager模板(ARM 模板)创建 Blob 存储帐户、订阅该 Blob 存储的事件,并触发事件以查看结果。 通常,你会将事件发送到处理事件数据并执行操作的终结点。 但是,为了简化本文,你将事件发送到收集并显示消息的 Web 应用。
注意
如果你不熟悉 Azure Event Grid,请在学习本教程之前参阅 什么是 Azure Event Grid 来了解该服务。
Azure Resource Manager模板是一个 JavaScript 对象表示法(JSON)文件,用于定义项目的基础结构和配置。 模板使用声明性语法。 你可以在不编写用于创建部署的编程命令序列的情况下,描述预期部署。
如果你的环境满足先决条件,并且你熟悉使用 ARM 模板,请选择 部署到Azure 按钮。 模板将在Azure门户中打开。
先决条件
如果没有Azure订阅,请在开始前创建 Trial。
创建消息终结点
在订阅 Blob 存储的事件之前,让我们创建事件消息的终结点。 通常情况下,终结点基于事件数据执行操作。 为了简化本快速入门,请部署显示事件消息的 预生成的 Web 应用。 部署的解决方案包括应用服务计划、应用服务 Web 应用以及来自GitHub的源代码。
选择 部署到 Azure将解决方案部署到您的订阅。 在Azure门户中,提供参数的值。
部署可能需要几分钟才能完成。 部署成功后,请查看 Web 应用以确保它正在运行。 在 Web 浏览器中导航到
https://<your-site-name>.chinacloudsites.cn你可以看到站点,但尚未有任何事件发布到其中。
查看模板
本快速入门中使用的模板来自 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资源:
- Microsoft.Storage/storageAccounts:创建Azure Storage帐户。
- Microsoft.EventGrid/systemTopics:使用指定的存储帐户名称创建系统主题。
- Microsoft.EventGrid/systemTopics/eventSubscriptions:为系统主题创建Azure Event Grid订阅。
部署模板
此处使用Azure门户部署模板。 还可以使用 Azure PowerShell、Azure CLI 和 REST API。 若要了解其他部署方法,请参阅部署模板。
注意
可以在 here 找到更多Azure Event Grid模板示例。
验证部署
再次查看 Web 应用,并注意现已向该应用发送了订阅验证事件。 选择眼睛图标以展开事件数据。 事件网格发送验证事件,以便终结点可以验证它是否想要接收事件数据。 Web 应用包含用于验证订阅的代码。
现在,让我们触发一个事件,看事件网格如何将消息分发到终结点。
通过上传一个文件来为 Blob 存储触发一个事件。 该文件不需要有任何特定内容。 本文假定你有一个名为 testfile.txt 的文件,但是你可以使用任何文件。
将文件上传到 Azure Blob 存储时,事件网格会将消息发送到订阅时配置的终结点。 消息采用 JSON 格式,它包含一个或多个事件的数组。 在以下示例中,JSON 消息包含一个事件的数组。 查看你的 Web 应用,会看到已收到一个 blob 创建事件。
清理资源
不再需要资源组时,可将其删除。
后续步骤
有关Azure Resource Manager模板的详细信息,请参阅以下文章:
- Azure Resource Manager 文档
- Azure Resource Manager 模板中定义资源
- Azure 快速入门模板
- Azure Event Grid 模板。