Skip to content

Commit a38c9c3

Browse files
authored
Merge pull request #19850 from cbrooksmsft/storage-blob-events
Storage blob events
2 parents 419ca3a + e26bef9 commit a38c9c3

File tree

5 files changed

+279
-0
lines changed

5 files changed

+279
-0
lines changed

articles/storage/blobs/TOC.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
href: storage-blob-storage-tiers.md
3939
- name: Designing HA Apps using RA-GRS
4040
href: ../common/storage-designing-ha-apps-with-ragrs.md?toc=%2fazure%2fstorage%2fblobs%2ftoc.json
41+
- name: Reacting to events
42+
href: storage-blob-event-overview.md
4143
- name: Scalability and performance targets
4244
href: ../common/storage-scalability-targets.md?toc=%2fazure%2fstorage%2fblobs%2ftoc.json
4345
- name: Performance and scalability checklist
@@ -74,6 +76,8 @@
7476
href: storage-properties-metadata.md
7577
- name: Use the Storage Emulator
7678
href: ../common/storage-use-emulator.md?toc=%2fazure%2fstorage%2fblobs%2ftoc.json
79+
- name: Route events to a custom endpoint
80+
href: storage-blob-event-quickstart.md
7781
- name: Custom domains
7882
href: storage-custom-domain-name.md
7983
- name: Searching Blob storage with Azure Search
85.9 KB
Loading
46.7 KB
Loading
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
---
2+
title: Reacting to Azure Blob Storage events (preview) | Microsoft Docs
3+
description: Use Azure Event Grid to subscribe to Blob Storage events.
4+
services: storage,event-grid
5+
keywords:
6+
author: cbrooksmsft
7+
ms.author: cbrooks
8+
ms.date: 08/25/2017
9+
ms.topic: article
10+
ms.service: storage
11+
---
12+
13+
# Reacting to Blob storage events (preview)
14+
15+
Azure Blob storage events allow applications to react to the creation and deletion of blobs using modern serverless architectures and without the need for complicated code or expensive and inefficient polling services. Instead, events are pushed through [Azure Event Grid](https://azure.microsoft.com/services/event-grid/) to subscribers such as [Azure Functions](https://azure.microsoft.com/services/functions/), [Azure Logic Apps](https://azure.microsoft.com/services/logic-apps/), or even to your own custom http listener, and you only pay for what you use.
16+
17+
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.
18+
19+
![Event Grid Model](./media/storage-blob-event-overview/event-grid-functional-model.png)
20+
21+
[!INCLUDE [cloud-shell-try-it.md](../../../includes/cloud-shell-try-it.md)]
22+
23+
## Join the preview
24+
Blob storage Events are available for preview. Users may request to join the preview by issuing the following commands against their subscription:
25+
```azurecli-interactive
26+
az provider register --namespace Microsoft.EventGrid
27+
az feature register --name storageEventSubscriptions --namespace Microsoft.EventGrid
28+
```
29+
Subscriptions are added to the Preview Program as capacity is available. Request status can be monitored by issuing the following command:
30+
```azurecli-interactive
31+
az feature show --name storageEventSubscriptions --namespace Microsoft.EventGrid
32+
```
33+
Once your registration state changes to “Registered”, you have been admitted to the preview program and you can subscribe to Blob storage events for accounts in the *West Central US* location. Take a look at [Route Blob storage events to a custom web endpoint](storage-blob-event-quickstart.md) for a quick example.
34+
35+
## Blob Storage accounts
36+
Blob storage events are available in [Blob storage accounts](../common/storage-create-storage-account.md?toc=%2fazure%2fstorage%2fblobs%2ftoc.json#blob-storage-accounts) (and not in General Purpose storage accounts). A Blob storage account is a specialized storage account for storing your unstructured data as blobs (objects) in Azure Storage. 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. For applications requiring only block or append blob storage, we recommend using Blob storage accounts.
37+
38+
## Available Blob storage events
39+
Event grid uses [event subscriptions](../../event-grid/concepts.md#event-subscriptions) to route event messages to subscribers. Blob storage event subscriptions can include two types of events:
40+
41+
> |Event Name|Description|
42+
> |----------|-----------|
43+
> |`Microsoft.Storage.BlobCreated`|Fired when a blob is created or replaced through the `PutBlob`, `PutBlockList`, or `CopyBlob` operations|
44+
> |`Microsoft.Storage.BlobDeleted`|Fired when a blob is deleted through a `DeleteBlob` operation|
45+
46+
## Event Schema
47+
Blob storage events contain all the information you need to respond to changes in your data. You can identify a Blob storage event because the eventType property starts with “Microsoft.Storage.”
48+
Additional information about the usage of Event Grid event properties is documented in [Event Grid event schema](../../event-grid/event-schema.md).
49+
50+
> |Property|Type|Description|
51+
> |-------------------|------------------------|-----------------------------------------------------------------------|
52+
> |topic|string|Full Azure Resource Manager id of the storage account that emits the event.|
53+
> |subject|string|The relative resource path to the object that is the subject of the event, using the same extended Azure Resource Manager format that we use to describe storage accounts, services, and containers for Azure RBAC. This format includes a case-preserving blob name.|
54+
> |eventTime|string|Date/time that the event was generated, in ISO 8601 format|
55+
> |eventType|string|“Microsoft.Storage.BlobCreated” or “Microsoft.Storage.BlobDeleted”|
56+
> |Id|string|Unique identifier if this event|
57+
> |data|object|Collection of blob storage-specific event data|
58+
> |data.contentType|string|The content type of the blob, as would be returned in the Content-Type header from the blob|
59+
> |data.contentLength|number|The size of the blob as in integer representing a number of bytes, as would be returned in the Content-Length header from the blob. Sent with BlobCreated event, but not with BlobDeleted.|
60+
> |data.url|string|The url of the object that is the subject of the event|
61+
> |data.eTag|string|The etag of the object when this event fired. Not available for the BlobDeleted event.|
62+
> |data.api|string|The name of the api operation that triggered this event. For BlobCreated events, this value is “PutBlob”, “PutBlockList”, or “CopyBlob”. For BlobDeleted events, this value is “DeleteBlob”. These values are the same api names that are present in the Azure Storage diagnostic logs. See [Logged Operations and Status Messages](https://docs.microsoft.com/rest/api/storageservices/storage-analytics-logged-operations-and-status-messages).|
63+
> |data.sequencer|string|An opaque string value representing the logical sequence of events for any particular blob name. Users can use standard string comparison to understand the relative sequence of two events on the same blob name.|
64+
> |data.requestId|string|Service-generated request id for the storage API operation. Can be used to correlate to Azure Storage diagnostic logs using the “request-id-header” field in the logs and is returned from initiating API call in the 'x-ms-request-id' header. See [Log Format](https://docs.microsoft.com/rest/api/storageservices/storage-analytics-log-format).|
65+
> |data.clientRequestId|string|Client-provided request id for the storage API operation. Can be used to correlate to Azure Storage diagnostic logs using the “client-request-id” field in the logs, and can be provided in client requests using the “x-ms-client-request-id” header. See [Log Format](https://docs.microsoft.com/rest/api/storageservices/storage-analytics-log-format).|
66+
> |data.storageDiagnostics|object|Diagnostic data occasionally included by the Azure Storage service. When present, should be ignored by event consumers.|
67+
68+
Here is an example of a BlobCreated event:
69+
```json
70+
[{
71+
"topic": "/subscriptions/319a9601-1ec0-0000-aebc-8fe82724c81e/resourceGroups/testrg/providers/Microsoft.Storage/storageAccounts/myaccount",
72+
"subject": "/blobServices/default/containers/testcontainer/blobs/file1.txt",
73+
"eventType": "Microsoft.Storage.BlobCreated",
74+
"eventTime": "2017-08-16T01:57:26.005121Z",
75+
"id": "602a88ef-0001-00e6-1233-1646070610ea",
76+
"data": {
77+
"api": "PutBlockList",
78+
"clientRequestId": "799304a4-bbc5-45b6-9849-ec2c66be800a",
79+
"requestId": "602a88ef-0001-00e6-1233-164607000000",
80+
"eTag": "0x8D4E44A24ABE7F1",
81+
"contentType": "text/plain",
82+
"contentLength": 447,
83+
"blobType": "BlockBlob",
84+
"url": "https://myaccount.blob.core.windows.net/testcontainer/file1.txt",
85+
"sequencer": "00000000000000EB000000000000C65A",
86+
}
87+
}]
88+
89+
```
90+
91+
For more information, see [Blob storage events schema](../../event-grid/event-schema.md#azure-blob-storage).
92+
93+
## Filtering events
94+
Blob event subscriptions can be filtered based on the event type and by the container name and blob name of the object that was created or deleted. Subject filters in Event Grid work based on “begins with” and “ends with” matches, so that events with a matching subject are delivered to the subscriber.
95+
The subject of Blob storage events uses the format:
96+
```json
97+
/blobServices/default/containers/<containername>/blobs/<blobname>
98+
```
99+
To match all events for a storage account, you can leave the subject filters empty.
100+
101+
To match events from blobs created in a set of containers sharing a prefix, use a `subjectBeginsWith` filter like:
102+
```json
103+
/blobServices/default/containers/containerprefix
104+
```
105+
To match events from blobs created in specific container, use a `subjectBeginsWith` filter like:
106+
```json
107+
/blobServices/default/containers/containername/
108+
```
109+
To match events from blobs created in specific container sharing a blob name prefix, use a `subjectBeginsWith` filter like:
110+
```json
111+
/blobServices/default/containers/containername/blobs/blobprefix
112+
```
113+
114+
To match events from blobs created in specific container sharing a blob suffix, use a `subjectEndsWith` filter like “.log” or “.jpg”
115+
116+
For more information, see [Event Grid Concepts](../../event-grid/concepts.md#filters).
117+
118+
## Practices for consuming events
119+
Applications that handle Blob storage events should follow a few recommended practices:
120+
> [!div class="checklist"]
121+
> * 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.
122+
> * 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.
123+
> * As messages can arrive out of order and after some delay, use the etag fields to understand if your information about objects is still up-to-date. Also, use the sequencer fields to understand the order of events on any particular object.
124+
> * 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.
125+
> * Use the url field with the `CloudBlockBlob` and `CloudAppendBlob` constructors to access the blob.
126+
> * Ignore fields you don’t understand. This practice will help keep you resilient to new features that might be added in the future.
127+
128+
129+
## Next steps
130+
131+
Learn more about Event Grid and give Blob storage events a try:
132+
133+
- [About Event Grid](../../event-grid/overview.md)
134+
- [Route Blob storage Events to a custom web endpoint](storage-blob-event-quickstart.md)
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
---
2+
title: Route Azure Blob storage events to a custom web endpoint (preview) | Microsoft Docs
3+
description: Use Azure Event Grid to subscribe to Blob storage events.
4+
services: storage,event-grid
5+
keywords:
6+
author: cbrooksmsft
7+
ms.author: cbrooks
8+
ms.date: 08/18/2017
9+
ms.topic: hero-article
10+
ms.service: storage
11+
---
12+
13+
# Route Blob storage events to a custom web endpoint (preview)
14+
15+
Azure Event Grid is an eventing service for the cloud. In this article, you use the Azure CLI to subscribe to Blob storage events, and trigger the event to view the result.
16+
17+
> [!IMPORTANT]
18+
> You must be registered for the Blob storage events preview to complete this tutorial. Learn more about the preview program [here](storage-blob-event-overview.md#join-the-preview).
19+
20+
Typically, you send events to an endpoint that responds to the event, such as a webhook or Azure Function. To simplify the example shown in this article, we send the events to a URL that merely collects the messages. You create this URL by using an open source, third-party tool called [RequestBin](https://requestb.in/).
21+
22+
> [!NOTE]
23+
> **RequestBin** is an open source tool that is not intended for high throughput usage. The use of the tool here is purely demonstrative. If you push more than one event at a time, you might not see all of your events in the tool.
24+
25+
When you complete the steps described in this article, you see that the event data has been sent to an endpoint.
26+
27+
![Event data](./media/storage-blob-event-quickstart/request-result.png)
28+
29+
[!INCLUDE [quickstarts-free-trial-note.md](../../../includes/quickstarts-free-trial-note.md)]
30+
31+
[!INCLUDE [cloud-shell-try-it.md](../../../includes/cloud-shell-try-it.md)]
32+
33+
If you choose to install and use the CLI locally, this article requires that you are running the latest version of Azure CLI (2.0.14 or later). To find the version, run `az --version`. If you need to install or upgrade, see [Install Azure CLI 2.0](/cli/azure/install-azure-cli).
34+
35+
## Create a resource group
36+
37+
Event Grid topics are Azure resources, and must be placed in an Azure resource group. The resource group is a logical collection into which Azure resources are deployed and managed.
38+
39+
Create a resource group with the [az group create](/cli/azure/group#create) command.
40+
41+
The following example creates a resource group named `<resource_group_name>` in the *westcentralus* location. Replace `<resource_group_name>` with a unique name for your resource group.
42+
43+
```azurecli-interactive
44+
az group create --name <resource_group_name> --location westcentralus
45+
```
46+
47+
## Create a Blob storage account
48+
49+
To use Azure Storage, you need a storage account. Blob storage events are currently available today only in Blob storage accounts.
50+
51+
A Blob storage account is a specialized storage account for storing your unstructured data as blobs (objects) in Azure Storage. Blob storage accounts are similar to your existing 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. For applications requiring only block or append blob storage, we recommend using Blob storage accounts.
52+
53+
> [!NOTE]
54+
> For the preview release, Blob storage events are available only for storage accounts in the **westcentralus** location.
55+
56+
Replace `<storage_account_name>` with a unique name for your storage account, and `<resource_group_name>` with the resource group you created earlier.
57+
58+
```azurecli-interactive
59+
az storage account create \
60+
--name <storage_account_name> \
61+
--location westcentralus \
62+
--resource-group <resource_group_name> \
63+
--sku Standard_LRS \
64+
--kind BlobStorage \
65+
--access-tier Hot
66+
```
67+
68+
## Create a message endpoint
69+
70+
Before subscribing to events from the Blob storage account, let's create the endpoint for the event message. Rather than write code to respond to the event, we will create an endpoint that collects the messages so you can view them. RequestBin is an open source, third-party tool that enables you to create an endpoint, and view requests that are sent to it. Go to [RequestBin](https://requestb.in/), and click **Create a RequestBin**. Copy the bin URL, because you need it when subscribing to the topic.
71+
72+
## Subscribe to your Blob storage account
73+
74+
You subscribe to a topic to tell Event Grid which events you want to track. The following example subscribes to the Blob storage account you created, and passes the URL from RequestBin as the endpoint for event notification. Replace `<event_subscription_name>` with a unique name for your event subscription, and `<URL_from_RequestBin>` with the value from the preceding section. By specifying an endpoint when subscribing, Event Grid handles the routing of events to that endpoint. For `<resource_group_name>` and `<storage_account_name>`, use the values you created earlier.
75+
76+
```azurecli-interactive
77+
az eventgrid resource event-subscription create \
78+
--endpoint <URL_from_RequestBin> \
79+
--name <event_subscription_name> \
80+
--provider-namespace Microsoft.Storage \
81+
--resource-type storageAccounts \
82+
--resource-group <resource_group_name> \
83+
--resource-name <storage_account_name>
84+
```
85+
86+
## Trigger an event from Blob storage
87+
88+
Now, let's trigger an event to see how Event Grid distributes the message to your endpoint. First,let's configure the name and key for the storage account, then we'll create a container, then create and upload a file. Again, use the values for `<storage_account_name>` and `<resource_group_name>` you created earlier.
89+
90+
```azurecli-interactive
91+
export AZURE_STORAGE_ACCOUNT=<storage_account_name>
92+
export AZURE_STORAGE_ACCESS_KEY="$(az storage account keys list --account-name <storage_account_name> --resource-group <resource_group_name> --query "[0].value" --output tsv)"
93+
94+
az storage container create --name testcontainer
95+
96+
touch testfile.txt
97+
az storage blob upload --file testfile.txt --container-name testcontainer --name testfile.txt
98+
```
99+
100+
You have triggered the event, and Event Grid sent the message to the endpoint you configured when subscribing. Browse to the RequestBin URL that you created earlier. Or, click refresh in your open RequestBin browser. You see the event you just sent.
101+
102+
```json
103+
[{
104+
"topic": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myrg/providers/Microsoft.Storage/storageAccounts/myblobstorageaccount",
105+
"subject": "/blobServices/default/containers/testcontainer/blobs/testfile.txt",
106+
"eventType": "Microsoft.Storage.BlobCreated",
107+
"eventTime": "2017-08-16T20:33:51.0595757Z",
108+
"id": "4d96b1d4-0001-00b3-58ce-16568c064fab",
109+
"data": {
110+
"api": "PutBlockList",
111+
"clientRequestId": "d65ca2e2-a168-4155-b7a4-2c925c18902f",
112+
"requestId": "4d96b1d4-0001-00b3-58ce-16568c000000",
113+
"eTag": "0x8D4E4E61AE038AD",
114+
"contentType": "text/plain",
115+
"contentLength": 0,
116+
"blobType": "BlockBlob",
117+
"url": "https://myblobstorageaccount.blob.core.windows.net/testcontainer/testblob1.txt",
118+
"sequencer": "00000000000000EB0000000000046199",
119+
"storageDiagnostics": {
120+
"batchId": "dffea416-b46e-4613-ac19-0371c0c5e352"
121+
}
122+
}
123+
}]
124+
125+
```
126+
127+
## Clean up resources
128+
If you plan to continue working with this storage account and event subscription, do not clean up the resources created in this article. If you do not plan to continue, use the following command to delete the resources you created in this article.
129+
130+
Replace `<resource_group_name>` with the resource group you created above.
131+
132+
```azurecli-interactive
133+
az group delete --name <resource_group_name>
134+
```
135+
136+
## Next steps
137+
138+
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:
139+
140+
- [Reacting to Blob storage events](storage-blob-event-overview.md)
141+
- [About Event Grid](../../event-grid/overview.md)

0 commit comments

Comments
 (0)