Azure Functions provide a powerful and flexible way to react to changes in Azure Blob Storage. The blob trigger allows your function to execute automatically when a new or updated blob is detected in a specified container.
A blob trigger binds to a blob in Azure Blob Storage. When a new or updated blob is created in the storage account, the function is invoked. The function receives the blob content as input, allowing you to process it directly.
samples/{name}.txt).Blob triggers are configured using the function.json file or through attributes in code (for C#, Java, etc.).
function.json Example:{
"scriptFile": "../run.py",
"bindings": [
{
"name": "myBlob",
"type": "blobTrigger",
"direction": "in",
"path": "samples-workitems/{name}.json",
"connection": "AzureWebJobsStorage"
}
]
}
In this example:
name: Refers to the parameter name in your function code that will receive the blob data.type: Set to blobTrigger.direction: Set to in, indicating the blob is an input to the function.path: Specifies the container and blob name pattern. samples-workitems is the container, and {name}.json means any blob ending in .json will trigger the function, with name capturing the blob's base name.connection: The name of the app setting that contains the connection string to your Azure Storage account.Blob triggers are a type of blob binding. You can also have blob input and output bindings within the same function.
Here's how you might define a blob trigger in C#:
using Microsoft.Azure.WebJobs;
public static class BlobTriggerFunction
{
[FunctionName("ProcessBlob")]
public static void Run(
[BlobTrigger("samples-workitems/{name}.json", Connection = "AzureWebJobsStorage")] Stream myBlob,
string name,
ILogger log)
{
log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
// Process the blob content from 'myBlob' stream
}
}
| Parameter Name | Type | Description |
|---|---|---|
| Blob Content | Various (Stream, byte[], model classes) |
The content of the triggered blob. |
name (or similar) |
string |
Captures the blob name or parts of it based on the path pattern (e.g., {name}). |
cloudBlob |
Microsoft.Azure.Storage.Blob.CloudBlockBlob (or Azure SDK equivalent) |
Provides access to the blob metadata and operations. |
Stream binding to avoid loading the entire blob into memory.
You can use wildcard characters and curly braces to define flexible blob paths:
samples/{name}.txt: Triggers for any .txt file in the samples container.data/{year}/{month}/{day}/{name}.csv: Triggers for .csv files organized by date.logs/{*path}: Triggers for any blob within the logs container, capturing the full path in {path}.Ensure the connection property in your binding configuration correctly points to an app setting containing the connection string for your Azure Storage account. This is crucial for security and manageability.