Blob Input Binding

This document explains how to use the Azure Blob Storage input binding for Azure Functions.

Introduction

The blob input binding allows your function to read data from a blob in Azure Blob Storage. When the function is triggered, the binding automatically retrieves the blob's content and makes it available to your function code as a parameter.

Configuration

The blob input binding is configured in your function's function.json file. You need to specify the binding type, direction, path to the blob, and the storage connection string.

function.json Example


{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "blobContent",
      "type": "blob",
      "direction": "in",
      "path": "mycontainer/{name}.txt",
      "connection": "AzureWebJobsStorage"
    },
    {
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}
            

Binding Properties

Property Description Required
name The name of the parameter in your function code that will hold the blob content. Yes
type Must be set to blob. Yes
direction Must be set to in for an input binding. Yes
path The path to the blob, including the container name and blob name. You can use tokens like {name} to bind to parts of the blob name. Yes
connection The name of an app setting that contains the connection string for your Azure Blob Storage account. Common names include AzureWebJobsStorage. Yes

Usage in Function Code

The content of the blob is passed to your function as a parameter. The type of the parameter depends on the content of the blob and your function's language.

Python Example

Assuming the function.json above, your Python function might look like this:


import logging
import azure.functions as func

def main(req: func.HttpRequest, blobContent: str) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    if blobContent:
        logging.info(f"Blob content: {blobContent}")
        return func.HttpResponse(
             f"Successfully read blob content. Content: {blobContent[:100]}...",
             status_code=200
        )
    else:
        return func.HttpResponse(
             "Blob content could not be retrieved or was empty.",
             status_code=400
        )
            

C# Example

For C#, you might use a Stream or a strongly-typed object:


using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

public static class BlobTriggerFunction
{
    [FunctionName("BlobInputExample")]
    public static IActionResult Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        [Blob("mycontainer/{Query.name}.txt", Connection = "AzureWebJobsStorage")] Stream myBlob,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");

        string blobContent = "";
        if (myBlob != null)
        {
            using (var reader = new StreamReader(myBlob))
            {
                blobContent = reader.ReadToEnd();
                log.LogInformation($"Blob content: {blobContent}");
            }
            return new OkObjectResult($"Blob content read successfully. Content: {blobContent.Substring(0, Math.Min(blobContent.Length, 100))}...");
        }
        else
        {
            return new BadRequestObjectResult("Blob content could not be retrieved.");
        }
    }
}
            

Blob Path Tokens

The path property in function.json supports tokens to dynamically specify the blob to retrieve. These tokens are typically extracted from the trigger event or route parameters.

Tip: Use descriptive names for your blob containers and files to make your binding paths easier to manage.

Supported Data Types

The blob input binding can bind to various data types depending on your function's language:

Important: Ensure that the value of the connection property in your function.json correctly references an app setting that holds your Azure Blob Storage connection string.

Troubleshooting

Note: For more advanced scenarios, consider using the Azure Storage SDK directly within your function code for greater control.