Azure Functions Documentation

Azure Functions Storage Bindings

Azure Functions provide input and output bindings to interact with various Azure storage services. These bindings simplify data integration by abstracting the complexities of connecting to and manipulating storage data. This allows you to focus on your business logic rather than storage implementation details.

Supported Storage Services

Azure Functions offer bindings for the following storage services:

  • Azure Blob Storage: For storing and retrieving large amounts of unstructured data like files, images, and documents.
  • Azure Queue Storage: For reliable message queuing between application components.
  • Azure Table Storage: For NoSQL key-attribute data storage, suitable for structured, non-relational data.
  • Azure Cosmos DB: A multi-model database service that supports document, key-value, graph, and column-family data. (While not strictly "storage" in the traditional sense, it's a common data store for Functions and has dedicated bindings).

Key Concepts

Understanding these concepts is crucial when working with storage bindings:

  • Triggers: An event that starts a function execution. For storage, this could be a new blob created, a message added to a queue, or a document modification in Cosmos DB.
  • Input Bindings: Used to retrieve data from a storage service and make it available as a parameter in your function.
  • Output Bindings: Used to send data from your function to a storage service.
  • Connection Strings: Stored in application settings, these define how your function connects to a specific storage account.

Common Storage Binding Patterns

1. Blob Storage Bindings

Interact with Azure Blob Storage for file operations.

Blob Trigger: Reading a new blob

This function is triggered when a new blob is created in a specified container.

C# Example (Blob Trigger)


using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

public static class BlobTriggerFunction
{
    [FunctionName("BlobTriggerCSharp")]
    public static void Run(
        [BlobTrigger("samples-workitems/{name}", Connection = "AzureWebJobsStorage")] Stream myBlob,
        string name,
        ILogger log)
    {
        log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
        // Your logic to process the blob content
    }
}
                    

Node.js Example (Blob Trigger)


module.exports = async function (context, myBlob) {
    context.log("JavaScript blob trigger function processed blob");
    context.log("Name:", context.bindingData.name);
    context.log("Size:", myBlob.length, "Bytes");
    // Your logic to process the blob content
};
                    
Blob Output Binding: Writing a blob

This function writes content to a blob in a specified container.

C# Example (Blob Output)


using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

public static class BlobOutputFunction
{
    [FunctionName("BlobOutputCSharp")]
    [return: Blob("output-container/{name}.txt")]
    public static string Run(
        [QueueTrigger("myqueue-items")] string queueItem,
        string name,
        ILogger log)
    {
        log.LogInformation($"C# Queue trigger function processed: {queueItem}");
        return $"Processed item: {queueItem}";
    }
}
                    

2. Queue Storage Bindings

Use Queue Storage for asynchronous message processing.

Queue Trigger: Processing a queue message

This function executes when a new message arrives in a queue.

C# Example (Queue Trigger)


using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

public static class QueueTriggerFunction
{
    [FunctionName("QueueTriggerCSharp")]
    public static void Run(
        [QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string myQueueItem,
        ILogger log)
    {
        log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
        // Your logic to process the queue message
    }
}
                    
Queue Output Binding: Sending a queue message

This function sends a message to a specified queue.

C# Example (Queue Output)


using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

public static class QueueOutputFunction
{
    [FunctionName("QueueOutputCSharp")]
    public static void Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        [Queue("outputqueue", Connection = "AzureWebJobsStorage")] out string queueMessage,
        ILogger log)
    {
        string requestBody = new StreamReader(req.Body).ReadToEnd();
        queueMessage = $"Received request: {requestBody}";
        log.LogInformation("Message sent to output queue.");
    }
}
                    

3. Table Storage Bindings

Store and retrieve structured data in Azure Table Storage.

Table Input Binding: Reading table entities

This function retrieves entities from a table based on query parameters.

C# Example (Table Input)


using Microsoft.Azure.WebJobs;
using Microsoft.Azure.Cosmos.Table;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;

public static class TableInputFunction
{
    [FunctionName("TableInputCSharp")]
    public static void Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", Route = "entities/{partitionKey}/{rowKey}")] HttpRequest req,
        string partitionKey,
        string rowKey,
        [Table("MyTable", "{partitionKey}", "{rowKey}", Connection = "AzureWebJobsStorage")] CloudTableEntity tableEntity,
        ILogger log)
    {
        if (tableEntity != null)
        {
            log.LogInformation($"Entity found: PartitionKey={tableEntity.PartitionKey}, RowKey={tableEntity.RowKey}");
            // Process the tableEntity
        }
        else
        {
            log.LogInformation("Entity not found.");
        }
    }
}
                    
Table Output Binding: Writing table entities

This function inserts or merges entities into a table.

C# Example (Table Output)


using Microsoft.Azure.WebJobs;
using Microsoft.Azure.Cosmos.Table;
using Microsoft.Extensions.Logging;

public static class TableOutputFunction
{
    [FunctionName("TableOutputCSharp")]
    public static void Run(
        [QueueTrigger("table-updates", Connection = "AzureWebJobsStorage")] string queueItem,
        [Table("MyTable", Connection = "AzureWebJobsStorage")] out CloudTableEntity[] newEntities,
        ILogger log)
    {
        log.LogInformation($"Processing item for table update: {queueItem}");
        // Example: Parse queueItem and create new table entities
        var entities = new List();
        // ... logic to create entities ...
        newEntities = entities.ToArray();
    }
}
                    

Configuration

Storage bindings are configured in your function.json file (for JavaScript, Python, PowerShell) or via attributes in your code (for C#, Java).

The Connection property in the binding configuration refers to an application setting that holds the connection string to your storage account. For example, an app setting named AzureWebJobsStorage is the default for connecting to the storage account where the Function App is deployed.

Best Practices

  • Use Connection Strings Securely: Store connection strings in application settings, not directly in your code.
  • Choose the Right Binding: Select the binding that best suits your data access needs (e.g., Blob for files, Queue for messages, Table for structured data).
  • Handle Errors Gracefully: Implement robust error handling for storage operations.
  • Optimize for Performance: Understand the throughput limits and best practices for each storage service.

Explore the official Azure Functions documentation for detailed examples and advanced configurations for each storage binding.