Azure Functions 的 Azure Blob 存储输入绑定Azure Blob storage input binding for Azure Functions

通过输入绑定,可读取 blob 存储数据作为 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

以下示例演示使用一个队列触发器和一个 blob 输入绑定的 C# 函数The following example is a C# function that uses a queue trigger and an input blob binding. 队列消息包含该 blob 的名称,函数记录该 blob 的大小。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

C# 类库中,使用 BlobAttributeIn C# class libraries, use the BlobAttribute.

该特性的构造函数采用 Blob 的路径,以及一个表示读取或写入的 FileAccess 参数,如以下示例中所示: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");
}

可以设置 Connection 属性来指定要使用的存储帐户,如以下示例中所示: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");
}

可以使用 StorageAccount 特性在类、方法或参数级别指定存储帐户。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

下表解释了在 function.json 文件和 Blob 特性中设置的绑定配置属性。The following table explains the binding configuration properties that you set in the function.json file and the Blob attribute.

function.json 属性function.json property Attribute 属性Attribute property 说明Description
typetype 不适用n/a 必须设置为 blobMust be set to blob.
directiondirection 不适用n/a 必须设置为 inMust be set to in. 用法部分中已阐述异常。Exceptions are noted in the usage section.
namename 不适用n/a 表示函数代码中的 Blob 的变量的名称。The name of the variable that represents the blob in function code.
路径path BlobPathBlobPath Blob 的路径。The path to the blob.
连接 connection ConnectionConnection 包含要用于此绑定的存储连接字符串的应用设置的名称。The name of an app setting that contains the Storage connection string to use for this binding. 如果应用设置名称以“AzureWebJobs”开始,则只能在此处指定该名称的余下部分。If the app setting name begins with "AzureWebJobs", you can specify only the remainder of the name here. 例如,如果将 connection 设置为“MyStorage”,则 Functions 运行时会查找名为“AzureWebJobsMyStorage”的应用设置。For example, if you set connection to "MyStorage", the Functions runtime looks for an app setting that is named "AzureWebJobsMyStorage". 如果将 connection 留空,函数运行时将使用名为 AzureWebJobsStorage 的应用设置中的默认存储连接字符串。If you leave connection empty, the Functions runtime uses the default Storage connection string in the app setting that is named AzureWebJobsStorage.

连接字符串必须属于某个常规用途存储帐户,而不能属于仅限 Blob 的存储帐户The connection string must be for a general-purpose storage account, not a blob-only storage account.
不适用n/a AccessAccess 表示是要读取还是写入。Indicates whether you will be reading or writing.

在本地进行开发时,应用设置将取 local.settings.json 文件的值。When you're developing locally, app settings go into the local.settings.json file.

使用情况Usage

可以将以下参数类型用于 blob 输入绑定:You can use the following parameter types for the blob input binding:

  • Stream
  • TextReader
  • string
  • Byte[]
  • CloudBlobContainer
  • CloudBlobDirectory
  • ICloudBlob1ICloudBlob1
  • CloudBlockBlob1CloudBlockBlob1
  • CloudPageBlob1CloudPageBlob1
  • CloudAppendBlob1CloudAppendBlob1

1 function.json 中需有 "inout" 绑定 direction 或 C# 类库中需有 FileAccess.ReadWrite1 Requires "inout" binding direction in function.json or FileAccess.ReadWrite in a C# class library.

如果在尝试绑定到某个存储 SDK 类型时出现错误消息,请确保引用正确的存储 SDK 版本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.

由于整个 Blob 内容都会加载到内存中,因此,只有当 Blob 较小时才建议绑定到 stringByte[]Binding to string or Byte[] is only recommended if the blob size is small, as the entire blob contents are loaded into memory. 平时,最好使用 StreamCloudBlockBlob 类型。Generally, it is preferable to use a Stream or CloudBlockBlob type. 有关详细信息,请参阅本文前文中的并发和内存使用情况For more information, see Concurrency and memory usage earlier in this article.

后续步骤Next steps