快速入门:使用 Azure CLI 将存储事件路由到 Web 终结点

Azure 事件网格是针对云的事件处理服务。 在本文中,请使用 Azure CLI 订阅 Blob 存储事件,然后触发可查看结果的事件。

通常,你会将事件发送到处理事件数据并执行操作的终结点。 但是,为了简化本文,你将事件发送到收集并显示消息的 Web 应用。

完成本文所述步骤后,即可看到事件数据已发送到 Web 应用。

Screenshot of the Azure Event Grid Viewer that shows event data that has been sent to the web app.

如果没有 Azure 试用版订阅,请在开始前创建一个试用版订阅

  • 如需在本地运行 CLI 参考命令,请安装 Azure CLI。 如果在 Windows 或 macOS 上运行,请考虑在 Docker 容器中运行 Azure CLI。 有关详细信息,请参阅如何在 Docker 容器中运行 Azure CLI

    • 如果使用的是本地安装,请使用 az login 命令登录到 Azure CLI。 若要完成身份验证过程,请遵循终端中显示的步骤。 有关其他登录选项,请参阅使用 Azure CLI 登录

    • 出现提示时,请在首次使用时安装 Azure CLI 扩展。 有关扩展详细信息,请参阅使用 Azure CLI 的扩展

    • 运行 az version 以查找安装的版本和依赖库。 若要升级到最新版本,请运行 az upgrade

  • 本文需要 Azure CLI 版本 2.0.70 或更高版本。

创建资源组

事件网格主题是 Azure 资源,必须放置在 Azure 资源组中。 该资源组是在其中部署和管理 Azure 资源的逻辑集合。

使用“az group create”命令创建资源组。

以下示例在 chinaeast2 位置创建名为 <resource_group_name> 的资源组。 将 <resource_group_name> 替换为资源组的唯一名称。

az group create --name <resource_group_name> --location chinaeast2

创建存储帐户

可在常规用途 v2 存储帐户和 Blob 存储帐户中使用 Blob 存储事件。 常规用途 v2 存储帐户支持所有存储服务(包括 Blob、文件、队列和表)的所有功能。 Blob 存储帐户是一个专用存储帐户,用于将非结构化数据作为 Blob(对象)存储到 Azure 存储中 。 Blob 存储帐户类似于常规用途存储帐户,并且具有现在使用的所有卓越的耐用性、可用性、伸缩性和性能功能,包括用于块 blob 和追加 blob 的 100% API 一致性。 有关详细信息,请参阅 Azure 存储帐户概述

<storage_account_name> 替换为存储帐户的唯一名称,将 <resource_group_name> 替换为此前创建的资源组。

az storage account create \
  --name <storage_account_name> \
  --location chinaeast2 \
  --resource-group <resource_group_name> \
  --sku Standard_LRS \
  --kind BlobStorage \
  --access-tier Hot

创建消息终结点

在订阅主题之前,让我们创建事件消息的终结点。 通常情况下,终结点基于事件数据执行操作。 为了简化此快速入门,将部署用于显示事件消息的预建的 Web 应用。 所部署的解决方案包括应用服务计划、应用服务 Web 应用和 GitHub 中的源代码。

<your-site-name> 替换为 Web 应用的唯一名称。 Web 应用名称必须唯一,因为它是 DNS 条目的一部分。

sitename=<your-site-name>

az deployment group create \
  --resource-group <resource_group_name> \
  --template-uri "https://raw.githubusercontent.com/Azure-Samples/azure-event-grid-viewer/master/azuredeploy.json" \
  --parameters siteName=$sitename hostingPlanName=viewerhost

部署可能需要几分钟才能完成。 部署成功后,请查看 Web 应用以确保它正在运行。 在 Web 浏览器中导航到 https://<your-site-name>.chinacloudsites.cn

应会看到站点上当前未显示任何消息。

启用事件网格资源提供程序

  1. 如果以前未在 Azure 订阅中使用过事件网格,则可能需要注册事件网格资源提供程序。 运行以下命令,注册提供程序:

    az provider register --namespace Microsoft.EventGrid
    
  2. 完成注册可能需要一些时间。 若要查看状态,请运行以下命令:

    az provider show --namespace Microsoft.EventGrid --query "registrationState"
    

    registrationStateRegistered 后,即可继续。

订阅存储帐户

订阅主题,以告知事件网格要跟踪哪些事件以及要将这些事件发送到哪个位置。 以下示例订阅所创建的存储帐户,并将 Web 应用中的 URL 作为事件通知的终结点传递。 将 <event_subscription_name> 替换为事件订阅的名称。 对于 <resource_group_name><storage_account_name>,请使用此前创建的值。

Web 应用的终结点必须包括后缀 /api/updates/

storageid=$(az storage account show --name <storage_account_name> --resource-group <resource_group_name> --query id --output tsv)
endpoint=https://$sitename.chinacloudsites.cn/api/updates

az eventgrid event-subscription create \
  --source-resource-id $storageid \
  --name <event_subscription_name> \
  --endpoint $endpoint

再次查看 Web 应用,并注意现已向该应用发送了订阅验证事件。 选择眼睛图标以展开事件数据。 事件网格发送验证事件,以便终结点可以验证它是否想要接收事件数据。 Web 应用包含用于验证订阅的代码。

View subscription event

触发 Blob 存储中的事件

现在,让我们触发一个事件,看事件网格如何将消息分发到终结点。 首先,我们来配置存储帐户的名称和密钥,再创建一个容器,然后创建和上传文件。 同样,对于 <storage_account_name><resource_group_name>,请使用此前创建的值。

export AZURE_STORAGE_ACCOUNT=<storage_account_name>
export AZURE_STORAGE_KEY="$(az storage account keys list --account-name <storage_account_name> --resource-group <resource_group_name> --query "[0].value" --output tsv)"

az storage container create --name testcontainer

touch testfile.txt
az storage blob upload --file testfile.txt --container-name testcontainer --name testfile.txt

现已触发事件,并且事件网格已将消息发送到订阅时配置的终结点。 查看 Web 应用以查看刚刚发送的事件。

[{
  "topic": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myrg/providers/Microsoft.Storage/storageAccounts/myblobstorageaccount",
  "subject": "/blobServices/default/containers/testcontainer/blobs/testfile.txt",
  "eventType": "Microsoft.Storage.BlobCreated",
  "eventTime": "2017-08-16T20:33:51.0595757Z",
  "id": "4d96b1d4-0001-00b3-58ce-16568c064fab",
  "data": {
    "api": "PutBlockList",
    "clientRequestId": "d65ca2e2-a168-4155-b7a4-2c925c18902f",
    "requestId": "4d96b1d4-0001-00b3-58ce-16568c000000",
    "eTag": "0x8D4E4E61AE038AD",
    "contentType": "text/plain",
    "contentLength": 0,
    "blobType": "BlockBlob",
    "url": "https://myblobstorageaccount.blob.core.chinacloudapi.cn/testcontainer/testblob1.txt",
    "sequencer": "00000000000000EB0000000000046199",
    "storageDiagnostics": {
      "batchId": "dffea416-b46e-4613-ac19-0371c0c5e352"
    }
  },
  "dataVersion": "",
  "metadataVersion": "1"
}]

清理资源

如果计划继续使用此存储帐户和事件订阅,请不要清理在本文中创建的资源。 如果不打算继续学习,请使用以下命令删除本文中创建的资源。

<resource_group_name> 替换为上面创建的资源组。

az group delete --name <resource_group_name>

后续步骤

了解如何创建主题和事件订阅以后,即可进一步学习 Blob 存储事件以及事件网格的功能: