Azure Functions 的 Azure Blob 存储输出绑定Azure Blob storage output binding for Azure Functions

通过输出绑定,可在 Azure Functions 中修改和删除 blob 存储数据。The output binding allows you to modify and delete blob storage data in an Azure Function.

若要了解设置和配置详细信息,请参阅概述For information on setup and configuration details, see the overview.


以下示例演示使用一个 blob 触发器和两个 blob 输出绑定的 C# 函数The following example is a C# function that uses a blob trigger and two output blob bindings. sample-images 容器中创建映像 Blob 时,会触发该函数。The function is triggered by the creation of an image blob in the sample-images container. 该函数创建该映像 Blob 的小型和中型副本。It creates small and medium size copies of the image blob.

using System.Collections.Generic;
using System.IO;
using Microsoft.Azure.WebJobs;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;

public class ResizeImages
    public static void Run([BlobTrigger("sample-images/{name}")] Stream image,
        [Blob("sample-images-sm/{name}", FileAccess.Write)] Stream imageSmall,
        [Blob("sample-images-md/{name}", FileAccess.Write)] Stream imageMedium)
        IImageFormat format;

        using (Image<Rgba32> input = Image.Load<Rgba32>(image, out format))
            ResizeImage(input, imageSmall, ImageSize.Small, format);

        image.Position = 0;
        using (Image<Rgba32> input = Image.Load<Rgba32>(image, out format))
            ResizeImage(input, imageMedium, ImageSize.Medium, format);

    public static void ResizeImage(Image<Rgba32> input, Stream output, ImageSize size, IImageFormat format)
        var dimensions = imageDimensionsTable[size];

        input.Mutate(x => x.Resize(dimensions.Item1, dimensions.Item2));
        input.Save(output, format);

    public enum ImageSize { ExtraSmall, Small, Medium }

    private static Dictionary<ImageSize, (int, int)> imageDimensionsTable = new Dictionary<ImageSize, (int, int)>() {
        { ImageSize.ExtraSmall, (320, 200) },
        { ImageSize.Small,      (640, 400) },
        { ImageSize.Medium,     (800, 600) }


特性和注释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:

public static void Run(
    [BlobTrigger("sample-images/{name}")] Stream image,
    [Blob("sample-images-md/{name}", FileAccess.Write)] Stream imageSmall)

可以设置 Connection 属性来指定要使用的存储帐户,如以下示例中所示:You can set the Connection property to specify the storage account to use, as shown in the following example:

public static void Run(
    [BlobTrigger("sample-images/{name}")] Stream image,
    [Blob("sample-images-md/{name}", FileAccess.Write, Connection = "StorageConnectionAppSetting")] Stream imageSmall)

有关完整示例,请参阅输出示例For a complete example, see Output example.

可以使用 StorageAccount 特性在类、方法或参数级别指定存储帐户。You can use the StorageAccount attribute to specify the storage account at class, method, or parameter level. 有关详细信息,请参阅触发器 - 特性For more information, see Trigger - attributes.


下表解释了在 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 对于输出绑定,必须设置为 outMust be set to out for an output binding. 用法部分中已阐述异常。Exceptions are noted in the usage section.
namename 不适用n/a 表示函数代码中的 Blob 的变量的名称。The name of the variable that represents the blob in function code. 设置为 $return 可引用函数返回值。Set to $return to reference the function return value.
路径path BlobPathBlobPath Blob 容器的路径。The path to the blob container.
连接 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”,函数运行时将会查找名为“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.


可绑定到以下类型以编写 blob:You can bind to the following types to write blobs:

  • TextWriter
  • out string
  • out Byte[]
  • CloudBlobStream
  • Stream
  • CloudBlobContainer1CloudBlobContainer1
  • CloudBlobDirectory
  • ICloudBlob2ICloudBlob2
  • CloudBlockBlob2CloudBlockBlob2
  • CloudPageBlob2CloudPageBlob2
  • CloudAppendBlob2CloudAppendBlob2

1 function.json 中需有 "in" 绑定 direction 或 C# 类库中需有 FileAccess.Read1 Requires "in" binding direction in function.json or FileAccess.Read in a C# class library. 但是,可以使用运行时提供的容器对象来执行写入操作,例如将 Blob 上传到容器。However, you can use the container object that the runtime provides to do write operations, such as uploading blobs to the container.

2 function.json 中需有 "inout" 绑定 direction 或 C# 类库中需有 FileAccess.ReadWrite2 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.

在异步函数中,请使用返回值或 IAsyncCollector 而非 out 参数。In async functions, use the return value or IAsyncCollector instead of an out parameter.

由于整个 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.

异常和返回代码Exceptions and return codes

绑定Binding 参考Reference
BlobBlob Blob 错误代码Blob Error Codes
Blob、表、队列Blob, Table, Queue 存储错误代码Storage Error Codes
Blob、表、队列Blob, Table, Queue 故障排除Troubleshooting

后续步骤Next steps