0
0
AzureHow-ToBeginner · 3 min read

How to Use Blob Trigger Azure Function: Simple Guide

Use a BlobTrigger attribute in your Azure Function to automatically run code when a blob is added or updated in an Azure Storage container. Define the blob path and connection string in the function parameters to link the trigger to your storage account.
📐

Syntax

The BlobTrigger attribute listens to changes in a specified blob container. You specify the container path and the storage connection string. The function parameter receives the blob content or metadata.

  • BlobTrigger("container/{name}"): Path to the blob container and blob name pattern.
  • string name: The name of the blob that triggered the function.
  • Stream myBlob: The content of the blob as a stream.
  • Connection = "AzureWebJobsStorage": The storage account connection string setting.
csharp
public static void Run([BlobTrigger("samples-workitems/{name}", Connection = "AzureWebJobsStorage")] Stream myBlob, string name, ILogger log)
{
    log.LogInformation($"Blob trigger function processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
}
💻

Example

This example shows a complete Azure Function in C# that triggers when a new blob is uploaded to the samples-workitems container. It logs the blob name and size.

csharp
using System.IO;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

public static class BlobTriggerFunction
{
    [FunctionName("BlobTriggerFunction")]
    public static void Run(
        [BlobTrigger("samples-workitems/{name}", Connection = "AzureWebJobsStorage")] Stream myBlob,
        string name,
        ILogger log)
    {
        log.LogInformation($"Blob trigger function processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
    }
}
Output
Blob trigger function processed blob Name:example.txt Size: 1024 Bytes
⚠️

Common Pitfalls

  • Not setting the correct connection string name in Connection property causes the function not to connect to storage.
  • Incorrect blob path or container name in the trigger attribute will prevent the function from firing.
  • Forgetting to add the storage account connection string in application settings under the specified name (e.g., AzureWebJobsStorage).
  • Using the wrong parameter types; the blob content should be Stream, byte[], or string.
csharp
/* Wrong: Missing connection string name */
public static void Run([BlobTrigger("samples-workitems/{name}")] Stream myBlob, string name, ILogger log)
{
    log.LogInformation($"Processed blob {name}");
}

/* Right: Specify connection string name */
public static void Run([BlobTrigger("samples-workitems/{name}", Connection = "AzureWebJobsStorage")] Stream myBlob, string name, ILogger log)
{
    log.LogInformation($"Processed blob {name}");
}
📊

Quick Reference

Remember these key points when using Blob Trigger Azure Functions:

  • Use BlobTrigger("container/{name}") to specify the blob path.
  • Set the storage connection string in application settings with the name used in Connection.
  • The function parameter can be Stream, string, or byte[] to access blob content.
  • Blob triggers only fire on new or updated blobs.

Key Takeaways

Use the BlobTrigger attribute with the correct container path and connection string to listen for blob changes.
Ensure your storage connection string is set in application settings under the name used in the function.
The function parameter can receive blob content as Stream, string, or byte array.
Blob triggers activate only when blobs are added or updated in the specified container.
Double-check the blob path and connection string to avoid common configuration errors.