Blob Storage trigger for Azure Functions (C#)

This article explains how to use the Blob Storage trigger in Azure Functions with C#.

Note: This article covers the Blob Storage trigger when used with C# functions. For other languages, please refer to the specific language documentation.

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

Binding Data

The trigger can pass data to your function in several ways:

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

Learn More