Azure Functions

Documentation

Blob Output Bindings

Output bindings allow your Azure Functions to send data to other services without writing explicit SDK code. This document details how to use blob output bindings to write data to Azure Blob Storage.

Overview

Blob output bindings simplify the process of writing data to Azure Blob Storage. You define the binding in your function's configuration, and then simply return a value or use a context object to specify the data to be written. The runtime handles the underlying storage operations.

Configuration

Output bindings are configured in the function.json file (for Node.js, Python, and PowerShell) or via attributes (for C#).

function.json Example

For a C# function using function.json:

{ "scriptFile": "__init__.py", "bindings": [ { "name": "req", "type": "httpTrigger", "direction": "in", "methods": [ "get", "post" ], "authLevel": "function" }, { "name": "outputBlob", "type": "blob", "direction": "out", "path": "samples-output/{name}.txt", "connection": "AzureWebJobsStorage" }, { "name": "res", "type": "http", "direction": "out" } ] }

C# Attributes Example

For a C# function using attributes:

using Microsoft.Azure.WebJobs; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; public static class BlobOutputFunction { [FunctionName("WriteToBlob")] public static IActionResult Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req, [Blob("samples-output/{Query.name}.txt", FileAccess.Write, Connection = "AzureWebJobsStorage")] out string outputBlobContent) { string name = req.Query["name"]; string content = $"Hello, {name}!"; outputBlobContent = content; // This writes the content to the blob return new OkObjectResult($"Successfully wrote to blob: samples-output/{name}.txt"); } }

Parameters

Parameter Description
name The name of the output binding parameter in your function code.
type Must be blob for blob output bindings.
direction Must be out.
path The container and blob name. You can use parameters from the trigger or other input bindings (e.g., {name}.txt).
connection The name of the application setting that contains the Azure Storage connection string. Defaults to AzureWebJobsStorage.

Writing Data

You can write data to the blob in several ways:

  • Returning a value (for simple cases): If your function's primary purpose is to produce blob content, you can configure the function to return a string or byte array.
  • Using the output parameter: Assign a value to the output binding parameter (e.g., outputBlobContent = myData; in C# or context.bindings.outputBlob = myData; in Node.js).

Example: Python

import logging import azure.functions as func def main(req: func.HttpRequest, outputBlob: func.Out[str]) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.') name = req.params.get('name') if not name: try: req_body = req.get_json() except ValueError: pass else: name = req_body.get('name') if name: blob_content = f"Hello, {name} from Azure Functions!" outputBlob.set(blob_content) return func.HttpResponse( f"Successfully wrote to blob: samples-output/{name}.txt", status_code=200 ) else: return func.HttpResponse( "Please pass a name in the query string or request body", status_code=400 )

Blob Naming Conventions

The path property in the binding configuration can use tokens to dynamically generate blob names. Common tokens include:

  • {name}: From query parameters or route parameters.
  • {randguid}: A random GUID.
  • {datetime}: The current date and time in ISO 8601 format.

Note: If the specified blob path already exists, the existing blob will be overwritten.

Best Practices

  • Use environment variables or app settings for connection strings.
  • Keep blob names descriptive and organized.
  • Handle potential errors during blob writing, although the binding typically manages this.

Tip: For scenarios involving large amounts of data or streaming, consider using the Azure Storage SDK directly within your function for more granular control.