您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

快速入门:利用 PowerShell 将存储事件路由到 Web 终结点Quickstart: Route storage events to web endpoint with PowerShell

Azure 事件网格是针对云的事件处理服务。Azure Event Grid is an eventing service for the cloud. 在本文中,请使用 Azure PowerShell 订阅 Blob 存储事件,然后触发事件并查看结果。In this article, you use Azure PowerShell to subscribe to Blob storage events, trigger an event, and 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.

完成后即可看到事件数据已发送到 Web 应用。When you're finished, you see that the event data has been sent to the web app.

查看结果

设置Setup

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

本文要求运行最新版本的 Azure PowerShell。This article requires that you're running the latest version of Azure PowerShell. 如需进行安装或升级,请参阅安装和配置 Azure PowerShellIf you need to install or upgrade, see Install and configure Azure PowerShell.

登录 AzureSign in to Azure

使用 Connect-AzAccount 命令登录到 Azure 订阅,然后按照屏幕上的说明进行身份验证。Sign in to your Azure subscription with the Connect-AzAccount command and follow the on-screen directions to authenticate.

Connect-AzAccount

此示例使用 westus2,并将所选内容存储在变量中以供整个过程使用 。This example uses westus2 and stores the selection in a variable for use throughout.

$location = "westus2"

创建资源组Create a resource group

事件网格主题是 Azure 资源,必须放置在 Azure 资源组中。Event Grid topics are Azure resources, and must be placed in an Azure resource group. 该资源组是在其中部署和管理 Azure 资源的逻辑集合。The resource group is a logical collection into which Azure resources are deployed and managed.

使用 New-AzResourceGroup 命令创建资源组。Create a resource group with the New-AzResourceGroup command.

以下示例在“westus2” 位置创建名为“gridResourceGroup” 的资源组。The following example creates a resource group named gridResourceGroup in the westus2 location.

$resourceGroup = "gridResourceGroup"
New-AzResourceGroup -Name $resourceGroup -Location $location

创建存储帐户Create a storage account

可在常规用途 v2 存储帐户和 Blob 存储帐户中使用 Blob 存储事件。Blob storage events are available in general-purpose v2 storage accounts and Blob storage accounts. 常规用途 v2 存储帐户支持所有存储服务(包括 Blob、文件、队列和表)的所有功能 。General-purpose v2 storage accounts support all features for all storage services, including Blobs, Files, Queues, and Tables. Blob 存储帐户是一个专用存储帐户,用于将非结构化数据作为 Blob(对象)存储到 Azure 存储中 。A Blob storage account is a specialized storage account for storing your unstructured data as blobs (objects) in Azure Storage. Blob 存储帐户类似于常规用途存储帐户,并且具有现在使用的所有卓越的耐用性、可用性、伸缩性和性能功能,包括用于块 blob 和追加 blob 的 100% API 一致性。Blob storage accounts are like general-purpose storage accounts and share all the great durability, availability, scalability, and performance features that you use today including 100% API consistency for block blobs and append blobs. 有关详细信息,请参阅 Azure 存储帐户概述For more information, see Azure storage account overview.

使用 New-AzStorageAccount 创建具有 LRS 复制的 Blob 存储帐户,然后检索定义要使用的存储帐户的存储帐户上下文。Create a Blob storage account with LRS replication using New-AzStorageAccount, then retrieve the storage account context that defines the storage account to be used. 对存储帐户执行操作时,引用上下文而不是重复提供凭据。When acting on a storage account, you reference the context instead of repeatedly providing the credentials. 本示例创建一个名为 gridstorage 的存储帐户,其中启用了本地冗余存储 (LRS) 。This example creates a storage account called gridstorage with locally redundant storage (LRS).

备注

存储帐户名称位于全局命名空间,因此需要向此脚本中提供的名称追加某些随机字符。Storage account names are in a global name space so you need to append some random characters to the name provided in this script.

$storageName = "gridstorage"
$storageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroup `
  -Name $storageName `
  -Location $location `
  -SkuName Standard_LRS `
  -Kind BlobStorage `
  -AccessTier Hot

$ctx = $storageAccount.Context

创建消息终结点Create a message endpoint

在订阅主题之前,让我们创建事件消息的终结点。Before subscribing to the topic, 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.

<your-site-name> 替换为 Web 应用的唯一名称。Replace <your-site-name> with a unique name for your web app. Web 应用名称必须唯一,因为它是 DNS 条目的一部分。The web app name must be unique because it's part of the DNS entry.

$sitename="<your-site-name>"

New-AzResourceGroupDeployment `
  -ResourceGroupName $resourceGroup `
  -TemplateUri "https://raw.githubusercontent.com/Azure-Samples/azure-event-grid-viewer/master/azuredeploy.json" `
  -siteName $sitename `
  -hostingPlanName viewerhost

部署可能需要几分钟才能完成。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>.azurewebsites.netIn a web browser, navigate to: https://<your-site-name>.azurewebsites.net

应会看到站点上当前未显示任何消息。You should see the site with no messages currently displayed.

启用事件网格资源提供程序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. 运行以下命令:Run the following command:

Register-AzResourceProvider -ProviderNamespace Microsoft.EventGrid

完成注册可能需要一些时间。It may take a moment for the registration to finish. 若要检查状态,请运行:To check the status, run:

Get-AzResourceProvider -ProviderNamespace Microsoft.EventGrid

RegistrationStatusRegistered 后,即可继续。When RegistrationStatus is Registered, you're ready to continue.

订阅存储帐户Subscribe to your storage account

订阅主题是为了告知事件网格要跟踪哪些事件。以下示例订阅所创建的存储帐户,并将 Web 应用中的 URL 作为事件通知的终结点传递。You subscribe to a topic to tell Event Grid which events you want to track. The following example subscribes to the storage account you created, and passes the URL from your web app as the endpoint for event notification. Web 应用的终结点必须包括后缀 /api/updates/The endpoint for your web app must include the suffix /api/updates/.

$storageId = (Get-AzStorageAccount -ResourceGroupName $resourceGroup -AccountName $storageName).Id
$endpoint="https://$sitename.azurewebsites.net/api/updates"

New-AzEventGridSubscription `
  -EventSubscriptionName gridBlobQuickStart `
  -Endpoint $endpoint `
  -ResourceId $storageId

再次查看 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.

查看订阅事件

触发 Blob 存储中的事件Trigger an event from Blob storage

现在,让我们触发一个事件,看事件网格如何将消息分发到终结点。Now, let's trigger an event to see how Event Grid distributes the message to your endpoint. 首先,让我们创建容器和对象。First, let's create a container and an object. 然后,让我们将该对象上传到容器。Then, let's upload the object into the container.

$containerName = "gridcontainer"
New-AzStorageContainer -Name $containerName -Context $ctx

echo $null >> gridTestFile.txt

Set-AzStorageBlobContent -File gridTestFile.txt -Container $containerName -Context $ctx -Blob gridTestFile.txt

现已触发事件,并且事件网格已将消息发送到订阅时配置的终结点。You've triggered the event, and Event Grid sent the message to the endpoint you configured when subscribing. 查看 Web 应用以查看刚刚发送的事件。View your web app to see the event you just sent.

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

清理资源Clean up resources

如果计划继续使用此存储帐户和事件订阅,请不要清理在本文中创建的资源。If you plan to continue working with this storage account and event subscription, don't clean up the resources created in this article. 如果不打算继续学习,请使用以下命令删除本文中创建的资源。If you don't plan to continue, use the following command to delete the resources you created in this article.

Remove-AzResourceGroup -Name $resourceGroup

后续步骤Next steps

了解如何创建主题和事件订阅以后,即可进一步学习 Blob 存储事件以及事件网格的功能:Now that you know how to create topics and event subscriptions, learn more about Blob storage Events and what Event Grid can help you do: