Subscribing to Azure Blob Storage events
TypeScriptTo subscribe to Azure Blob Storage events, you'll need to use Azure Event Grid. Event Grid is a fully-managed event routing service that allows for uniform event consumption using a publish-subscribe model. It can handle events raised by Azure services or third-party resources.
When it comes to Blob Storage, Event Grid can send you events about changes to blobs so you can respond to them using a custom application, automation, or Azure Functions, for example.
Here's how you would generally set this up using Pulumi with TypeScript:
- An Azure Blob Storage account and a container where your blobs are stored.
- An Event Grid Topic that will handle the events.
- An Event Grid Subscription that filters the events you're interested in and directs them to an endpoint, like an Azure Function, for handling.
Below is a basic Pulumi program that creates these resources in Azure.
import * as storage from "@pulumi/azure-native/storage"; import * as resources from "@pulumi/azure-native/resources"; import * as eventgrid from "@pulumi/azure-native/eventgrid"; // Create an Azure Resource Group const resourceGroup = new resources.ResourceGroup("my-resourcegroup"); // Create an Azure Storage Account const storageAccount = new storage.StorageAccount("mystorageaccount", { resourceGroupName: resourceGroup.name, kind: "StorageV2", sku: { name: "Standard_LRS", }, }); // Create a Blob Container in the Storage Account const container = new storage.BlobContainer("mycontainer", { accountName: storageAccount.name, resourceGroupName: resourceGroup.name, }); // Create an Event Grid Topic const topic = new eventgrid.Topic("myeventgridtopic", { resourceGroupName: resourceGroup.name, location: resourceGroup.location, }); // Create an Event Subscription const eventSubscription = new eventgrid.EventSubscription("mysubscription", { scope: storageAccount.id, eventDeliverySchema: "EventGridSchema", destination: { endpointType: "WebHook", properties: { endpointUrl: "https://myeventhandler.example.com/eventhandler", // Replace with your actual endpoint URL }, }, filter: { includedEventTypes: ["Microsoft.Storage.BlobCreated"], // Replace with any other event types you're interested in isSubjectCaseSensitive: false, }, }); // Export the primary endpoint of the topic export const endpoint = topic.endpoint;
Let's go step by step through the code:
- You first create a new resource group to hold all the resources. It's a logical container that groups your Azure resources.
- Next, you create a storage account with standard locally-redundant storage (LRS).
- Within the storage account, you create a blob container, which is where your actual blob data will reside.
- An Event Grid Topic is set up to route the events from your storage account. Topics are used to categorize the events sent to Event Grid.
- Finally, you create an Event Grid Subscription. This subscription listens to events from the storage account and sends them to your webhook handler. The
filter
includes only blob-created events, but this can be changed to match other types of blob-related events.
Note that in the event subscription, you need to specify your actual endpoint and any other event types you want to filter for. Typically, this endpoint would be an Azure Function app or another service that can process the incoming events.
Lastly, by exporting the
endpoint
, you get the URL of the event grid topic, which you might need for further configuration, such as to manually subscribe other services or configure additional event subscriptions.