使用 Azure CLI 创建并通过事件网格监视 Azure 媒体服务事件Create and monitor Media Services events with Event Grid using the Azure CLI

媒体服务徽标 v3media services logo v3

Azure 事件网格是针对云的事件处理服务。Azure Event Grid is an eventing service for the cloud. 此服务使用事件订阅将事件消息路由到订阅方。This service uses event subscriptions to route event messages to subscribers. 媒体服务事件包含响应数据中的更改所需的所有信息。Media Services events contain all the information you need to respond to changes in your data. 可以识别媒体服务事件,因为 eventType 属性以“Microsoft.Media”开头。You can identify a Media Services event because the eventType property starts with "Microsoft.Media.". 有关详细信息,请参阅媒体服务事件架构For more information, see Media Services event schemas.

本文将使用 Azure CLI 订阅 Azure 媒体服务帐户的事件。In this article, you use the Azure CLI to subscribe to events for your Azure Media Services account. 然后,触发事件以查看结果。Then, you trigger events to view the result. 通常,你会将事件发送到处理事件数据并执行操作的终结点。Typically, you send events to an endpoint that processes the event data and takes actions. 在本文中,将事件发送到收集并显示消息的 Web 应用。In this article, you send the events to a web app that collects and displays the messages.


  • 一个有效的 Azure 订阅。An active Azure subscription. 如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

  • 在本地安装并使用 CLI,本文要求使用 Azure CLI 2.0 或更高版本。Install and use the CLI locally, this article requires the Azure CLI version 2.0 or later. 运行 az --version 即可确定你拥有的版本。Run az --version to find the version you have. 如需进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install the Azure CLI.

  • 创建媒体服务帐户Create a Media Services account.

    请务必记住用于资源组名称和媒体服务帐户名称的值。Make sure to remember the values that you used for the resource group name and Media Services account name.

创建消息终结点Create a message endpoint

在订阅媒体服务帐户的事件之前,先创建事件消息的终结点。Before subscribing to the events for the Media Services account, let's create the endpoint for the event message. 通常情况下,终结点基于事件数据执行操作。Typically, the endpoint takes actions based on the event data. 在本文中,将部署用于显示事件消息的预建 Web 应用In this article, 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.

    图像显示标记为“部署到 Azure”的按钮。Image showing a button labeled "Deploy 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

如果切换到“Azure 事件网格查看器”站点,将看到其中尚不具有任何事件。If you switch to the "Azure Event Grid Viewer" site, you see it has no events yet.

启用事件网格资源提供程序Enable Event Grid resource provider

如果以前未在 Azure 订阅中使用过事件网格,则可能需要注册事件网格资源提供程序。If you haven't previously used Event Grid in your Azure subscription, you may need to register the Event Grid resource provider.

在 Azure 门户中:In the Azure portal:

  1. 选择 订阅Select Subscriptions.
  2. 选择要用于事件网格的订阅。Select the subscription you're using for Event Grid.
  3. 在“设置”下,选择“资源提供程序”。Under Settings, select Resource providers.
  4. 找到 Microsoft.EventGridFind Microsoft.EventGrid.
  5. 如果尚未注册,请选择“注册”。If not registered, select Register.

完成注册可能需要一些时间。It may take a moment for the registration to finish. 选择“刷新”可更新状态。Select Refresh to update the status. 当“状态”为“已注册”后,即可继续。When Status is Registered, you're ready to continue.

设置 Azure 订阅Set the Azure subscription

在以下命令中,为媒体服务帐户提供想要使用的 Azure 订阅 ID。In the following command, provide the Azure subscription ID that you want to use for the Media Services account. 导航到订阅即可查看有权访问的订阅列表。You can see a list of subscriptions that you have access to by navigating to Subscriptions.

az account set --subscription mySubscriptionId

订阅媒体服务事件Subscribe to Media Services events

订阅文章是为了告知事件网格要跟踪哪些事件。以下示例订阅所创建的媒体服务帐户,并将所创建网站中的 URL 作为事件通知的终结点进行传递。You subscribe to an article to tell Event Grid which events you want to track. The following example subscribes to the Media Services account you created, and passes the URL from the website you created as the endpoint for event notification.

<event_subscription_name> 替换为事件订阅的唯一名称。Replace <event_subscription_name> with a unique name for your event subscription. 对于 <resource_group_name><ams_account_name>,使用在创建媒体服务帐户时使用的值。For <resource_group_name> and <ams_account_name>, use the values you used when creating the Media Services account. 对于 <endpoint_URL>,请提供你的 Web 应用的 URL,并将 api/updates 添加到主页 URL。For the <endpoint_URL>, provide the URL of your web app and add api/updates to the home page URL. 在订阅时指定终结点,然后事件网格就会负责将事件路由到该终结点。By specifying the endpoint when subscribing, Event Grid handles the routing of events to that endpoint.

  1. 获取资源 IDGet the resource id

    amsResourceId=$(az ams account show --name <ams_account_name> --resource-group <resource_group_name> --query id --output tsv)

    例如:For example:

    amsResourceId=$(az ams account show --name amsaccount --resource-group amsResourceGroup --query id --output tsv)
  2. 订阅事件Subscribe to the events

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

    例如:For example:

    az eventgrid event-subscription create --source-resource-id $amsResourceId --name amsTestEventSubscription --endpoint https://amstesteventgrid.chinacloudsites.cn/api/updates/


    可能会收到验证握手警告。You might get validation handshake warning. 等待几分钟后握手验证应会完成。Give it a few minutes and the handshake should validate.

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

向终结点发送事件Send an event to your endpoint

可通过运行编码作业来触发媒体服务帐户的事件。You can trigger events for the Media Services account by running an encoding job. 可遵照本快速入门来对文件进行编码并开始发送事件。You can follow this quickstart to encode a file and start sending events.

再次查看 Web 应用,并注意现已向该应用发送了订阅验证事件。View your web app again, and notice that a subscription validation event has been sent to it. 事件网格发送验证事件,以便终结点可以验证它是否想要接收事件数据。Event Grid sends the validation event so the endpoint can verify that it wants to receive event data. 终结点必须将 validationResponse 设置为 validationCodeThe endpoint has to set validationResponse to validationCode. 有关详细信息,请参阅事件网格安全性和身份验证For more information, see Event Grid security and authentication. 可以查看 Web 应用代码以了解它如何验证订阅。You can view the web app code to see how it validates the subscription.


选择眼睛图标以展开事件数据。Select the eye icon to expand the event data. 如果要查看所有事件,请勿刷新页面。Do not refresh the page, if you want to view all the events.


后续步骤Next steps

上传、编码和流式处理Upload, encode, and stream