Azure Blob storage input binding for Azure Functions

The input binding allows you to read blob storage data as input to an Azure Function.

For information on setup and configuration details, see the overview.

Example

The following example is a C# function that uses a queue trigger and an input blob binding. The queue message contains the name of the blob, and the function logs the size of the blob.

[FunctionName("BlobInput")]
public static void Run(
    [QueueTrigger("myqueue-items")] string myQueueItem,
    [Blob("samples-workitems/{queueTrigger}", FileAccess.Read)] Stream myBlob,
    ILogger log)
{
    log.LogInformation($"BlobInput processed blob\n Name:{myQueueItem} \n Size: {myBlob.Length} bytes");
}

Attributes and annotations

In C# class libraries, use the BlobAttribute.

The attribute's constructor takes the path to the blob and a FileAccess parameter indicating read or write, as shown in the following example:

[FunctionName("BlobInput")]
public static void Run(
    [QueueTrigger("myqueue-items")] string myQueueItem,
    [Blob("samples-workitems/{queueTrigger}", FileAccess.Read)] Stream myBlob,
    ILogger log)
{
    log.LogInformation($"BlobInput processed blob\n Name:{myQueueItem} \n Size: {myBlob.Length} bytes");
}

You can set the Connection property to specify the storage account to use, as shown in the following example:

[FunctionName("BlobInput")]
public static void Run(
    [QueueTrigger("myqueue-items")] string myQueueItem,
    [Blob("samples-workitems/{queueTrigger}", FileAccess.Read, Connection = "StorageConnectionAppSetting")] Stream myBlob,
    ILogger log)
{
    log.LogInformation($"BlobInput processed blob\n Name:{myQueueItem} \n Size: {myBlob.Length} bytes");
}

You can use the StorageAccount attribute to specify the storage account at class, method, or parameter level. For more information, see Trigger - attributes and annotations.

Configuration

The following table explains the binding configuration properties that you set in the function.json file and the Blob attribute.

function.json property Attribute property Description
type n/a Must be set to blob.
direction n/a Must be set to in. Exceptions are noted in the usage section.
name n/a The name of the variable that represents the blob in function code.
path BlobPath The path to the blob.
connection Connection The name of an app setting that contains the Storage connection string to use for this binding. If the app setting name begins with "AzureWebJobs", you can specify only the remainder of the name here. For example, if you set connection to "MyStorage", the Functions runtime looks for an app setting that is named "AzureWebJobsMyStorage". If you leave connection empty, the Functions runtime uses the default Storage connection string in the app setting that is named AzureWebJobsStorage.

The connection string must be for a general-purpose storage account, not a blob-only storage account.

If you are using version 5.x or higher of the extension, instead of a connection string, you can provide a reference to a configuration section which defines the connection. See Connections.
dataType n/a For dynamically typed languages, specifies the underlying data type. Possible values are string, binary, or stream. For more more detail, refer to the triggers and bindings concepts.
n/a Access Indicates whether you will be reading or writing.

When you're developing locally, app settings go into the local.settings.json file.

Usage

Default

You can use the following parameter types for the blob input binding:

  • Stream
  • TextReader
  • string
  • Byte[]
  • CloudBlobContainer
  • CloudBlobDirectory
  • ICloudBlob1
  • CloudBlockBlob1
  • CloudPageBlob1
  • CloudAppendBlob1

1 Requires "inout" binding direction in function.json or FileAccess.ReadWrite in a C# class library.

If you try to bind to one of the Storage SDK types and get an error message, make sure that you have a reference to the correct Storage SDK version.

Binding to string or Byte[] is only recommended if the blob size is small, as the entire blob contents are loaded into memory. Generally, it is preferable to use a Stream or CloudBlockBlob type. For more information, see Concurrency and memory usage earlier in this article.

Additional types

Apps using the 5.0.0 or higher version of the Storage extension may also use types from the Azure SDK for .NET. This version drops support for the legacy CloudBlobContainer, CloudBlobDirectory, ICloudBlob, CloudBlockBlob, CloudPageBlob, and CloudAppendBlob types in favor of the following types:

1 Requires "inout" binding direction in function.json or FileAccess.ReadWrite in a C# class library.

For examples using these types, see the GitHub repository for the extension.

Next steps