Azure Functions Blob Output Bindings (C#)
This document provides details on how to use output bindings for Azure Blob Storage with Azure Functions in C#.
Overview
Output bindings allow your function to write data to external services without requiring explicit SDK code. For Blob Storage, an output binding makes it easy to upload data to a blob container.
Configuration
You configure blob output bindings in your function's function.json file (for V1/V2 programming model) or using attributes in your C# code (for V3/V4 programming model).
function.json (V1/V2 programming model)
The following JSON defines a blob output binding:
{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "myBlob",
"type": "blob",
"direction": "in",
"path": "samples-workitems/{name}",
"connection": "AzureWebJobsStorage"
},
{
"name": "outputBlob",
"type": "blob",
"direction": "out",
"path": "samples-output/{name}.txt",
"connection": "AzureWebJobsStorage"
}
]
}
C# Attributes (V3/V4 programming model)
In your C# function, you can define the output binding using attributes:
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
namespace Company.Function
{
public static class BlobOutputExample
{
[Function("BlobOutputExample")]
public static void Run(
[BlobTrigger("samples-workitems/{name}")] string myBlob,
[Blob("samples-output/{name}.txt", FileAccess.Write)] out string outputBlob,
FunctionContext context)
{
var logger = context.GetLogger();
logger.LogInformation($"C# Blob trigger function Processed blob\n Name: {context.BindingContext.GetInput("name")}");
logger.LogInformation($"Value: {myBlob}");
outputBlob = $"Processed content: {myBlob.ToUpper()}";
}
}
}
Parameters
The following table lists the common properties for a blob output binding:
| Property | Type | Description |
|---|---|---|
name |
String | The name of the binding. This name is used in your code to refer to the blob output. |
type |
String | Must be set to blob. |
direction |
String | Must be set to out for an output binding. |
path |
String | The path to the blob. This can include wildcard parameters that are passed to your function. For example, samples-output/{name}.txt. |
connection |
String | The name of an app setting that contains the connection string for your Azure Storage account. Defaults to AzureWebJobsStorage if not specified. |
Data Types
The output binding can be bound to various C# types, including:
string: Writes the string content directly to the blob.byte[]: Writes the byte array content to the blob.System.IO.Stream: Allows you to write data stream to the blob.CloudAppendBlob,CloudPageBlob,CloudBlockBlob(from the Azure Storage SDK): Provides direct access to blob objects.
Example Usage
In this example, a function is triggered by a blob. It then writes a modified version of the blob's content to a new blob using an output binding.
C# Code
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
namespace Company.Function
{
public static class BlobToUpper
{
[Function("BlobToUpper")]
public static void Run(
[BlobTrigger("input-blobs/{name}")] Stream inputBlobStream,
[Blob("output-blobs/{name}", FileAccess.Write)] Stream outputBlobStream,
string name,
ILogger logger)
{
logger.LogInformation($"Processing blob: {name}");
using (var reader = new StreamReader(inputBlobStream))
using (var writer = new StreamWriter(outputBlobStream))
{
string content = reader.ReadToEnd();
writer.Write(content.ToUpper());
}
logger.LogInformation($"Finished processing blob: {name}");
}
}
}
function.json (if not using attributes)
{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "inputBlobStream",
"type": "blobTrigger",
"direction": "in",
"path": "input-blobs/{name}",
"connection": "AzureWebJobsStorage"
},
{
"name": "outputBlobStream",
"type": "blob",
"direction": "out",
"path": "output-blobs/{name}",
"connection": "AzureWebJobsStorage"
}
]
}
connection property in your binding configuration points to a valid app setting that holds your Azure Storage connection string.
Troubleshooting
- Verify the
pathandconnectionsettings are correct. - Check Azure Function logs for error messages.
- Ensure your function app has the necessary permissions to access the storage account.