Blob Storage trigger for Azure Functions (C#)
This article explains how to use the Blob Storage trigger in Azure Functions with C#.
Overview
The Blob Storage trigger allows your Azure Function to execute when a new or updated blob is detected in a specified Azure Blob Storage container. This is a powerful way to build event-driven architectures, reacting to file uploads or modifications.
Trigger Attributes
In C#, the Blob Storage trigger is configured using the BlobTrigger attribute.
Basic Usage
The BlobTrigger attribute requires a path that specifies the container and optionally a file pattern. The connection string to your Blob Storage account is typically defined in your application settings.
C# Function with Blob Trigger
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
public class BlobTriggerFunction
{
private readonly ILogger _logger;
public BlobTriggerFunction(ILogger logger)
{
_logger = logger;
}
[Function("BlobTriggerCSharp")]
public void Run(
[BlobTrigger("samples-workitems/{name}", Connection = "AzureWebJobsStorage")] Stream myBlob,
string name)
{
_logger.LogInformation($"C# Blob trigger function Processed blob\n Name: {name} \n Size: {myBlob.Length} Bytes");
// You can now read from the 'myBlob' stream
using (var reader = new StreamReader(myBlob))
{
string content = reader.ReadToEnd();
_logger.LogInformation($"Blob content:\n{content}");
}
}
}
Attribute Parameters
path: A string that defines the container and blob name pattern. For example,"samples-workitems/{name}". The{name}part is a placeholder that captures the blob's name.Connection: The name of the application setting that contains the Blob Storage connection string. If not specified, it defaults toAzureWebJobsStorage.
Binding Data
The trigger can pass data to your function in several ways:
- Blob Content: You can bind directly to the blob's content using types like
Stream,byte[], orstring. - Blob Metadata: You can bind to the blob's name, size, and other metadata using route parameters defined in the path (e.g.,
string name).
Configuration
Ensure your local.settings.json (for local development) or application settings (in Azure) includes the connection string for your Blob Storage account.
local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "AccountName=YOUR_ACCOUNT_NAME;AccountKey=YOUR_ACCOUNT_KEY;EndpointSuffix=core.windows.net",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
}
}
Example: Processing a CSV file
Here's an example of a function that processes a CSV file uploaded to Blob Storage.
C# Function to process CSV
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using System.IO;
using System.Threading.Tasks;
public class CsvBlobTrigger
{
private readonly ILogger _logger;
public CsvBlobTrigger(ILogger logger)
{
_logger = logger;
}
[Function("ProcessCsv")]
public async Task Run(
[BlobTrigger("csv-uploads/{name}.csv", Connection = "AzureWebJobsStorage")] Stream csvFile,
string name)
{
_logger.LogInformation($"Processing CSV file: {name}.csv");
using (var reader = new StreamReader(csvFile))
{
string line;
while ((line = await reader.ReadLineAsync()) != null)
{
// Process each line of the CSV
_logger.LogInformation($"CSV Line: {line}");
// Add your CSV parsing and processing logic here
}
}
_logger.LogInformation($"Finished processing CSV file: {name}.csv");
}
}
Common Scenarios
- Image Processing: Resize, watermark, or analyze images upon upload.
- Data Ingestion: Process data files (CSV, JSON, XML) as they arrive.
- Workflow Triggers: Initiate further processing steps based on file presence.