Reacting to new files: Blob and Event Grid
Functions can react when files land in Blob Storage, and to events from almost any Azure service through Event Grid. The modern way to trigger on blobs uses Event Grid underneath for speed and reliability.
- StartUser uploads a photoThe file lands in the uploads container.
- Step 2Storage raises an eventBlobCreated goes to Event Grid.
- Step 3Event Grid calls the appThe blob extension endpoint receives the event.
- Step 4Function runsThe blob is streamed in and resized.
- EndOutput savedThe thumbnail is written to another container.
Writing to a different container avoids an endless trigger loop.
Blob trigger with the Event Grid source
The classic blob trigger polls the container, which can take minutes on busy accounts. Setting Source to EventGrid makes Storage push an event the moment a blob is created. Flex Consumption apps support only the Event Grid source, and Microsoft recommends it for new apps. You also create an Event Grid subscription that points at the function's blob extension endpoint.
[Function("MakeThumbnail")]
public async Task Run(
[BlobTrigger("uploads/{name}", Source = BlobTriggerSource.EventGrid, Connection = "Storage")]
Stream image,
string name,
CancellationToken ct)
{
using var thumb = await _images.ResizeAsync(image, width: 320, ct);
await _thumbnails.UploadAsync($"thumbs/{name}", thumb, ct);
}Event Grid triggers
Event Grid routes events from Azure services (Storage, Resource groups, Key Vault, IoT Hub, your own topics) to handlers. An Event Grid trigger receives one event at a time, in the Event Grid schema or the CloudEvents schema.
[Function("OnSecretChanged")]
public void Run([EventGridTrigger] CloudEvent ev, ILogger<OnSecretChanged> logger)
{
logger.LogInformation("Event {Type} for {Subject}", ev.Type, ev.Subject);
// e.g. Microsoft.KeyVault.SecretNewVersionCreated: refresh a cached secret
}Working with files safely
- Bind to Stream for big files instead of byte[] or string, so the whole file is not loaded into memory.
- Writing to the same container that triggers the function causes an endless loop; write output to a different container or path.
- Blob triggers can fire more than once for the same blob. Check whether the output already exists before doing expensive work.
- Use the blob name binding ({name}) to build output paths and log which file you processed.
Interview question
What is the difference between a polling blob trigger and an Event Grid based blob trigger?
The polling trigger scans the container and its logs to find new blobs, so it can be slow, from seconds to minutes, on busy or large containers. With Source set to EventGrid, Storage pushes a BlobCreated event through Event Grid, so the function runs almost immediately and scales better. Flex Consumption supports only the Event Grid source, so I use it for new apps.
Want the full details? Read the official Microsoft documentation for this topic.