响应 Blob 存储事件Reacting to Blob storage events

Azure 存储事件允许应用程序响应事件,例如 Blob 的创建和删除。Azure Storage events allow applications to react to events, such as the creation and deletion of blobs. 为此,它无需复杂的代码或高价低效的轮询服务。It does so without the need for complicated code or expensive and inefficient polling services. 最大好处是,使用多少就付多少费用。The best part is you only pay for what you use.

通过 Azure 事件网格向订阅者(如 Azure Functions 或 Azure 逻辑应用,甚至是你自己的 HTTP 侦听器)推送 Blob 存储事件。Blob storage events are pushed using Azure Event Grid to subscribers such as Azure Functions, Azure Logic Apps, or even to your own http listener. 事件网格通过丰富的重试策略和死信向应用程序提供可靠的事件传递。Event Grid provides reliable event delivery to your applications through rich retry policies and dead-lettering.

若要查看 Blob 存储支持的事件的完整列表,请参阅 Blob 存储事件架构一文。See the Blob storage events schema article to view the full list of the events that Blob storage supports.

常见的 Blob 存储事件方案包括图像或视频处理、搜索索引或任何面向文件的工作流。Common Blob storage event scenarios include image or video processing, search indexing, or any file-oriented workflow. 异步文件上传十分适合事件。Asynchronous file uploads are a great fit for events. 基于事件的体系结构对于鲜少更改,但要求立即响应的情况尤为有效。When changes are infrequent, but your scenario requires immediate responsiveness, event-based architecture can be especially efficient.

若要尝试 blob 存储事件,请参阅以下任意快速入门文章:If you want to try blob storage events, see any of these quickstart articles:

若要使用此工具:If you want to use this tool: 请参阅此文:See this article:
Azure 门户Azure portal 快速入门:利用 Azure 门户将 Blob 存储事件路由到 Web 终结点Quickstart: Route Blob storage events to web endpoint with the Azure portal
PowerShellPowerShell 快速入门:利用 PowerShell 将存储事件路由到 Web 终结点Quickstart: Route storage events to web endpoint with PowerShell
Azure CLIAzure CLI 快速入门:使用 Azure CLI 将存储事件路由到 Web 终结点Quickstart: Route storage events to web endpoint with Azure CLI

若要深入了解使用 Azure Functions 对 Blob 存储事件做出反应的示例,请参阅以下文章:To view in-depth examples of reacting to Blob storage events by using Azure functions, see these articles:

备注

只有种类为“StorageV2 (常规用途 v2)”、“BlockBlobStorage”和“BlobStorage”的存储帐户支持事件集成。 Only storage accounts of kind StorageV2 (general purpose v2), BlockBlobStorage, and BlobStorage support event integration. “存储(常规用途 v1)”不支持与事件网格集成。Storage (general purpose v1) does not support integration with Event Grid.

事件模型The event model

事件网格使用事件订阅将事件消息路由到订阅方。Event Grid uses event subscriptions to route event messages to subscribers. 此图展示了事件发布者、事件订阅和事件处理程序之间的关系。This image illustrates the relationship between event publishers, event subscriptions, and event handlers.

事件网格模型

首先,将终结点订阅到事件。First, subscribe an endpoint to an event. 然后,在触发某个事件时,事件网格服务会将有关该事件的数据发送到终结点。Then, when an event is triggered, the Event Grid service will send data about that event to the endpoint.

请参阅 Blob 存储事件架构一文,以便查看:See the Blob storage events schema article to view:

  • Blob 存储事件的完整列表,以及如何触发每个事件。A complete list of Blob storage events and how each event is triggered.
  • 事件网格会针对每个此类事件发送的数据的示例。An example of the data the Event Grid would send for each of these events.
  • 显示在数据中的每个键值对的用途。The purpose of each key value pair that appears in the data.

筛选事件Filtering events

可以按事件类型、容器名称或已创建/删除的对象的名称来筛选 Blob 事件Blob events can be filtered by the event type, container name, or name of the object that was created/deleted. 事件网格中的筛选器与主题的开头或结尾匹配,因此具有匹配的主题的事件会转到订阅服务器。Filters in Event Grid match the beginning or end of the subject so events with a matching subject go to the subscriber.

若要详细了解如何应用筛选器,请参阅筛选事件网格的事件To learn more about how to apply filters, see Filter events for Event Grid.

Blob 存储事件使用者使用的格式:The subject of Blob storage events uses the format:

/blobServices/default/containers/<containername>/blobs/<blobname>

要匹配存储帐户的所有事件,可将主题筛选器留空。To match all events for a storage account, you can leave the subject filters empty.

要匹配在一组共享前缀的容器中创建的 blob 的事件,请使用 subjectBeginsWith 筛选器,如下所示:To match events from blobs created in a set of containers sharing a prefix, use a subjectBeginsWith filter like:

/blobServices/default/containers/containerprefix

要匹配在特定容器中创建的 blob 的事件,请使用 subjectBeginsWith 筛选器,如下所示:To match events from blobs created in specific container, use a subjectBeginsWith filter like:

/blobServices/default/containers/containername/

要匹配在共享 blob 名称前缀的特定容器中创建的 blob 的事件,请使用 subjectBeginsWith 筛选器,如下所示:To match events from blobs created in specific container sharing a blob name prefix, use a subjectBeginsWith filter like:

/blobServices/default/containers/containername/blobs/blobprefix

要匹配在共享 blob 后缀的特定容器中创建的 blob 事件,请使用 subjectEndsWith 筛选器,例如“.log”或“.jpg”。To match events from blobs created in specific container sharing a blob suffix, use a subjectEndsWith filter like ".log" or ".jpg". 有关详细信息,请参阅 事件网格概念For more information, see Event Grid Concepts.

使用事件的做法Practices for consuming events

处理 Blob 存储事件的应用程序应遵循以下建议的做法:Applications that handle Blob storage events should follow a few recommended practices:

  • 由于可将多个订阅配置为将事件路由至相同的事件处理程序,因此请勿假定事件来自特定的源,而是应检查消息的主题,确保它来自所期望的存储帐户。As multiple subscriptions can be configured to route events to the same event handler, it is important not to assume events are from a particular source, but to check the topic of the message to ensure that it comes from the storage account you are expecting.
  • 同样,检查 eventType 是否为准备处理的项,并且不假定所接收的全部事件都是期望的类型。Similarly, check that the eventType is one you are prepared to process, and do not assume that all events you receive will be the types you expect.
  • 由于消息可能在一段延迟后到达,请使用 etag 字段了解对象的相关信息是否仍然是最新的。As messages can arrive after some delay, use the etag fields to understand if your information about objects is still up-to-date. 若要了解如何使用 etag 字段,请参阅在 Blob 存储中管理并发To learn how to use the etag field, see Managing concurrency in Blob storage.
  • 由于消息可能无序到达,请使用 sequencer 字段来了解任何特定对象的事件顺序。As messages can arrive out of order, use the sequencer fields to understand the order of events on any particular object. sequencer 字段是一个字符串值,表示任何特定 blob 名称的事件逻辑顺序。The sequencer field is a string value that represents the logical sequence of events for any particular blob name. 你可以使用标准字符串比较,了解同一个 blob 名称上两个事件的相对顺序。You can use standard string comparison to understand the relative sequence of two events on the same blob name.
  • 存储事件保证至少向订阅服务器传递一次,以确保输出所有消息。Storage events guarantees at-least-once delivery to subscribers, which ensures that all messages are outputted. 但是,由于订阅的重试或可用性,可能偶尔会出现重复消息。However due to retries or availability of subscriptions, duplicate messages may occasionally occur. 若要详细了解消息传递和重试,请参阅事件网格消息传递和重试To learn more about message delivery and retry, see Event Grid message delivery and retry.
  • 使用 blobType 字段可了解 blob 中允许何种类型的操作,以及应当使用哪种客户端库类型来访问该 blob。Use the blobType field to understand what type of operations are allowed on the blob, and which client library types you should use to access the blob. 有效值为 BlockBlobPageBlobValid values are either BlockBlob or PageBlob.
  • 将 URL 字段与 CloudBlockBlobCloudAppendBlob 构造函数配合使用,以访问 blob。Use the url field with the CloudBlockBlob and CloudAppendBlob constructors to access the blob.
  • 忽略不了解的字段。Ignore fields you don't understand. 此做法有助于适应将来可能添加的新功能。This practice will help keep you resilient to new features that might be added in the future.
  • 若要确保 Microsoft.Storage.BlobCreated 事件仅在块 Blob 完全提交后触发,请针对 CopyBlobPutBlobPutBlockListFlushWithClose REST API 调用筛选此事件。If you want to ensure that the Microsoft.Storage.BlobCreated event is triggered only when a Block Blob is completely committed, filter the event for the CopyBlob, PutBlob, PutBlockList or FlushWithClose REST API calls. 这些 API 调用仅在数据已完全提交到块 Blob 后才触发 Microsoft.Storage.BlobCreated 事件。These API calls trigger the Microsoft.Storage.BlobCreated event only after data is fully committed to a Block Blob. 若要了解如何创建筛选器,请参阅筛选事件网格的事件To learn how to create a filter, see Filter events for Event Grid.

后续步骤Next steps

了解关于事件网格的详细信息,尝试一下 Blob 存储事件:Learn more about Event Grid and give Blob storage events a try: