Azure Functions Blob Trigger Binding
The Blob Trigger allows an Azure Function to be executed automatically when a new or updated blob is detected in Azure Blob Storage.
Introduction
This binding is incredibly useful for event-driven architectures, enabling you to process files as they are uploaded to storage. Common use cases include image processing, data ingestion pipelines, and file validation.
Binding Attributes
When defining a blob trigger, you'll use specific attributes in your function's configuration (e.g., function.json or attributes in code).
Common Attributes:
path: Specifies the container and blob name pattern. You can use wildcards like*and curly braces for named parameters (e.g.,samples-workitems/{name}.txt).connection: The name of an app setting that contains the Azure Blob Storage connection string. Defaults toAzureWebJobsStorage.dataType: (Optional) Specifies the expected data type of the blob. Can bestream,string, orbinary. Defaults tostream.direction: Should be set toinfor triggers.
Trigger Behavior
The blob trigger monitors the specified path in Blob Storage. When a blob is created or modified that matches the defined path, the function is invoked.
Blob Processing:
- The function receives the blob content as an input.
- The trigger automatically handles the polling and invocation.
- If your function completes successfully, the blob is marked as processed. If it fails, the trigger might retry depending on configuration.
Configuration
Configuration can be done in two primary ways:
1. Using function.json (for JavaScript, Python, PowerShell, etc.):
{
"scriptFile": "../run.py",
"bindings": [
{
"name": "myBlob",
"type": "blobTrigger",
"direction": "in",
"path": "samples-workitems/{name}.csv",
"connection": "AzureBlobStorageConnectionString"
}
]
}
2. Using Attributes in Code (for C#, Java, TypeScript):
C# Example:
using Microsoft.Azure.WebJobs;
public static class BlobTriggerFunction
{
public static void Run(
[BlobTrigger("samples-workitems/{name}.csv", Connection = "AzureBlobStorageConnectionString")] Stream myBlob,
string name)
{
// Function logic here
// myBlob is the stream of the blob content
// name is the name of the blob (e.g., "myfile")
}
}
TypeScript Example:
import { AzureFunction, Context, BlobTrigger } from "@azure/functions";
const blobTrigger: AzureFunction = async function (
context: Context,
myBlob: Buffer // Or 'string' or 'NodeJS.ReadableStream'
): Promise {
context.log("Blob trigger function processed blob");
context.log(`Name: ${context.bindingData.name}`);
context.log(`Size: ${myBlob.length} Bytes`);
// Your processing logic here
};
export default blobTrigger;
name property in the path (e.g., {name}.csv) will be available in the function's context (context.bindingData.name in JavaScript/TypeScript) and can be used to access specific parts of the blob name.
Examples
Processing a CSV file and inserting data into a database:
When a .csv file lands in the data-input container, the function is triggered, reads the CSV content, and processes each row.
Resizing images upon upload:
A blob trigger on an images container can invoke a function that resizes the uploaded image and saves the new version to a different container or blob path.
Best Practices
- Use specific paths: Avoid overly broad paths (like
*) if possible to prevent unnecessary function invocations. - Handle errors gracefully: Implement robust error handling and logging. Consider using poison queue patterns for persistently failing blobs.
- Connection Strings: Store connection strings securely in application settings, not directly in code or configuration files.
- Batching (if applicable): For high-volume scenarios, explore output bindings that can batch operations to improve performance.
- Idempotency: Design your function to be idempotent, meaning running it multiple times with the same input produces the same result. This is crucial for retry mechanisms.
- Monitoring: Utilize Azure Monitor and Application Insights to track function execution, errors, and performance.
Stream or binary data type and processing the data in chunks to manage memory usage.