通过输出绑定,可在 Azure Functions 中修改和删除 blob 存储数据。
若要了解设置和配置详细信息,请参阅概述。
示例
C#
以下示例演示使用一个 blob 触发器和两个 blob 输出绑定的 C# 函数。 在 sample-images 容器中创建映像 Blob 时,会触发该函数。 该函数创建该映像 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
{
[FunctionName("ResizeImage")]
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) }
};
}
C# 脚本
以下示例演示 function.json 文件中的 blob 输入和输出绑定,以及使用这些绑定的 C# 脚本 (.csx) 代码。 此函数创建文本 blob 的副本。 该函数由包含要复制的 Blob 名称的队列消息触发。 新 Blob 名为 {originalblobname}-Copy。
在 function.json 文件中,queueTrigger 元数据属性用于指定 path 属性中的 Blob 名称:
{
"bindings": [
{
"queueName": "myqueue-items",
"connection": "MyStorageConnectionAppSetting",
"name": "myQueueItem",
"type": "queueTrigger",
"direction": "in"
},
{
"name": "myInputBlob",
"type": "blob",
"path": "samples-workitems/{queueTrigger}",
"connection": "MyStorageConnectionAppSetting",
"direction": "in"
},
{
"name": "myOutputBlob",
"type": "blob",
"path": "samples-workitems/{queueTrigger}-Copy",
"connection": "MyStorageConnectionAppSetting",
"direction": "out"
}
],
"disabled": false
}
配置部分解释了这些属性。
C# 脚本代码如下所示:
public static void Run(string myQueueItem, string myInputBlob, out string myOutputBlob, ILogger log)
{
log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
myOutputBlob = myInputBlob;
}
Java
本部分包含以下示例:
HTTP 触发器,使用 OutputBinding (Java)
下面的示例显示了一个 Java 函数,该函数使用 HttpTrigger 注释来接收包含 blob 存储容器中某个文件的名称的一个参数。 然后,BlobInput 注释读取该文件并将其作为 byte[] 传递给该函数。 BlobOutput 注释绑定到 OutputBinding outputItem,然后,函数使用后者来将输入 blob 的内容写入到所配置的存储容器中。
@FunctionName("copyBlobHttp")
@StorageAccount("Storage_Account_Connection_String")
public HttpResponseMessage copyBlobHttp(
@HttpTrigger(name = "req",
methods = {HttpMethod.GET},
authLevel = AuthorizationLevel.ANONYMOUS)
HttpRequestMessage<Optional<String>> request,
@BlobInput(
name = "file",
dataType = "binary",
path = "samples-workitems/{Query.file}")
byte[] content,
@BlobOutput(
name = "target",
path = "myblob/{Query.file}-CopyViaHttp")
OutputBinding<String> outputItem,
final ExecutionContext context) {
// Save blob to outputItem
outputItem.setValue(new String(content, StandardCharsets.UTF_8));
// build HTTP response with size of requested blob
return request.createResponseBuilder(HttpStatus.OK)
.body("The size of \"" + request.getQueryParameters().get("file") + "\" is: " + content.length + " bytes")
.build();
}
队列触发器,使用函数返回值 (Java)
下面的示例显示了一个 Java 函数,该函数使用 QueueTrigger 注释来接收包含 blob 存储容器中某个文件的名称的一个消息。 然后,BlobInput 注释读取该文件并将其作为 byte[] 传递给该函数。 BlobOutput 注释绑定到函数返回值,然后,运行时使用后者来将输入 blob 的内容写入到所配置的存储容器中。
@FunctionName("copyBlobQueueTrigger")
@StorageAccount("Storage_Account_Connection_String")
@BlobOutput(
name = "target",
path = "myblob/{queueTrigger}-Copy")
public String copyBlobQueue(
@QueueTrigger(
name = "filename",
dataType = "string",
queueName = "myqueue-items")
String filename,
@BlobInput(
name = "file",
path = "samples-workitems/{queueTrigger}")
String content,
final ExecutionContext context) {
context.getLogger().info("The content of \"" + filename + "\" is: " + content);
return content;
}
在 Java 函数运行时库中,对其值将写入 Blob 存储中对象的函数参数使用 @BlobOutput 注释。 参数类型应为 OutputBinding<T>,其中 T 是任何本机 Java 类型或者是一个 POJO。
JavaScript
下面的示例展示了 function.json 文件中的 blob 输入和输出绑定,以及使用这些绑定的 JavaScript 代码。 该函数创建 Blob 的副本。 该函数由包含要复制的 Blob 名称的队列消息触发。 新 Blob 名为 {originalblobname}-Copy。
在 function.json 文件中,queueTrigger 元数据属性用于指定 path 属性中的 Blob 名称:
{
"bindings": [
{
"queueName": "myqueue-items",
"connection": "MyStorageConnectionAppSetting",
"name": "myQueueItem",
"type": "queueTrigger",
"direction": "in"
},
{
"name": "myInputBlob",
"type": "blob",
"path": "samples-workitems/{queueTrigger}",
"connection": "MyStorageConnectionAppSetting",
"direction": "in"
},
{
"name": "myOutputBlob",
"type": "blob",
"path": "samples-workitems/{queueTrigger}-Copy",
"connection": "MyStorageConnectionAppSetting",
"direction": "out"
}
],
"disabled": false
}
配置部分解释了这些属性。
JavaScript 代码如下所示:
module.exports = function(context) {
context.log('Node.js Queue trigger function processed', context.bindings.myQueueItem);
context.bindings.myOutputBlob = context.bindings.myInputBlob;
context.done();
};
PowerShell
下面的示例演示如何创建传入 Blob 的副本作为 PowerShell 函数的输出。
在函数的配置文件 (function.json) 中,trigger 元数据属性用于指定 path 属性中的输出 Blob 名称。
Note: 为了避免无限循环,请确保输入和输出路径不同。
{
"bindings": [
{
"name": "myInputBlob",
"path": "data/{trigger}",
"connection": "MyStorageConnectionAppSetting",
"direction": "in",
"type": "blobTrigger"
},
{
"name": "myOutputBlob",
"type": "blob",
"path": "data/copy/{trigger}",
"connection": "MyStorageConnectionAppSetting",
"direction": "out"
}
],
"disabled": false
}
下面是 PowerShell 代码:
# Input bindings are passed in via param block.
param([byte[]] $myInputBlob, $TriggerMetadata)
Write-Host "PowerShell Blob trigger function Processed blob Name: $($TriggerMetadata.Name)"
Push-OutputBinding -Name myOutputBlob -Value $myInputBlob
Python
下面的示例展示了 function.json 文件中的 blob 输入和输出绑定,以及使用这些绑定的 Python 代码。 该函数创建 Blob 的副本。 该函数由包含要复制的 Blob 名称的队列消息触发。 新 Blob 名为 {originalblobname}-Copy。
在 function.json 文件中,queueTrigger 元数据属性用于指定 path 属性中的 Blob 名称:
{
"bindings": [
{
"queueName": "myqueue-items",
"connection": "MyStorageConnectionAppSetting",
"name": "queuemsg",
"type": "queueTrigger",
"direction": "in"
},
{
"name": "inputblob",
"type": "blob",
"path": "samples-workitems/{queueTrigger}",
"connection": "MyStorageConnectionAppSetting",
"direction": "in"
},
{
"name": "outputblob",
"type": "blob",
"path": "samples-workitems/{queueTrigger}-Copy",
"connection": "MyStorageConnectionAppSetting",
"direction": "out"
}
],
"disabled": false,
"scriptFile": "__init__.py"
}
配置部分解释了这些属性。
下面是 Python 代码:
import logging
import azure.functions as func
def main(queuemsg: func.QueueMessage, inputblob: func.InputStream,
outputblob: func.Out[func.InputStream]):
logging.info('Python Queue trigger function processed %s', inputblob.name)
outputblob.set(inputblob)
特性和注释
C#
在 C# 类库中,使用 BlobAttribute。
该特性的构造函数采用 Blob 的路径,以及一个表示读取或写入的 FileAccess 参数,如以下示例中所示:
[FunctionName("ResizeImage")]
public static void Run(
[BlobTrigger("sample-images/{name}")] Stream image,
[Blob("sample-images-md/{name}", FileAccess.Write)] Stream imageSmall)
{
...
}
可以设置 Connection 属性来指定要使用的存储帐户,如以下示例中所示:
[FunctionName("ResizeImage")]
public static void Run(
[BlobTrigger("sample-images/{name}")] Stream image,
[Blob("sample-images-md/{name}", FileAccess.Write, Connection = "StorageConnectionAppSetting")] Stream imageSmall)
{
...
}
C# 脚本
C# 脚本不支持特性。
Java
使用 @BlobOutput 特性可以访问触发函数的 blob。 如果将字节数组与特性一起使用,请将 dataType 设置为 binary。 有关详细信息,请参阅输出示例。
JavaScript
JavaScript 不支持特性。
PowerShell
PowerShell 不支持特性。
Python
Python 不支持特性。
有关完整示例,请参阅输出示例。
可以使用 StorageAccount 特性在类、方法或参数级别指定存储帐户。 有关详细信息,请参阅触发器 - 特性。
配置
下表解释了在 function.json 文件和 Blob 特性中设置的绑定配置属性。
| function.json 属性 | Attribute 属性 | 说明 |
|---|---|---|
| type | 不适用 | 必须设置为 blob。 |
| direction | 不适用 | 对于输出绑定,必须设置为 out。 用法部分中已阐述异常。 |
| name | 不适用 | 表示函数代码中的 Blob 的变量的名称。 设置为 $return 可引用函数返回值。 |
| 路径 | BlobPath | Blob 容器的路径。 |
| 连接 | Connection | 包含要用于此绑定的存储连接字符串的应用设置的名称。 如果应用设置名称以“AzureWebJobs”开始,则只能在此处指定该名称的余下部分。 例如,如果将 connection 设置为“MyStorage”,函数运行时将会查找名为“AzureWebJobsMyStorage”的应用设置。 如果将 connection 留空,函数运行时将使用名为 AzureWebJobsStorage 的应用设置中的默认存储连接字符串。连接字符串必须属于某个常规用途存储帐户,而不能属于仅限 Blob 的存储帐户。如果你使用的是 版本 5. x 或更高版本,而不是连接字符串,则可以提供对定义该连接的配置节的引用。 请参阅 连接。 |
| 不适用 | 访问 | 表示是要读取还是写入。 |
在本地进行开发时,应用设置将取 local.settings.json 文件的值。
使用情况
C#
默认
可绑定到以下类型以编写 blob:
TextWriterout stringout Byte[]CloudBlobStreamStreamCloudBlobContainer<sup>1</sup>CloudBlobDirectoryICloudBlob<sup>2</sup>CloudBlockBlob<sup>2</sup>CloudPageBlob<sup>2</sup>CloudAppendBlob<sup>pps-2</sup>
<sup>1</sup> function.json 中需有 "in" 绑定 direction 或 C# 类库中需有 FileAccess.Read。 但是,可以使用运行时提供的容器对象来执行写入操作,例如将 Blob 上传到容器。
<sup>2</sup> function.json 中需有 "inout" 绑定 direction 或 C# 类库中需有 FileAccess.ReadWrite。
如果尝试绑定到某个存储 SDK 类型并收到错误消息,请确保已引用正确的存储 SDK 版本。
由于整个 Blob 内容都会加载到内存中,因此,只有当 Blob 较小时才建议绑定到 string 或 Byte[]。 平时,最好使用 Stream 或 CloudBlockBlob 类型。 有关详细信息,请参阅本文前文中的并发和内存使用情况。
其他类型
使用 存储扩展插件的5.0.0 或更高版本 的应用程序也可以使用 Azure SDK for .net中的类型。 此版本支持旧的 CloudBlobContainer 、、、 CloudBlobDirectory 、 ICloudBlob CloudBlockBlob CloudPageBlob 和 CloudAppendBlob 类型,以支持以下类型:
- BlobContainerClient<sup>1</sup>
- BlobClient<sup>2</sup>
- BlockBlobClient<sup>2</sup>
- PageBlobClient<sup>2</sup>
- AppendBlobClient<sup>2</sup>
- BlobBaseClient<sup>2</sup>
<sup>1</sup> function.json 中需有 "in" 绑定 direction 或 C# 类库中需有 FileAccess.Read。 但是,可以使用运行时提供的容器对象来执行写入操作,例如将 Blob 上传到容器。
<sup>2</sup> function.json 中需有 "inout" 绑定 direction 或 C# 类库中需有 FileAccess.ReadWrite。
有关使用这些类型的示例,请参阅 GitHub 存储库中的扩展。
C# 脚本
默认
可绑定到以下类型以编写 blob:
TextWriterout stringout Byte[]CloudBlobStreamStreamCloudBlobContainer<sup>1</sup>CloudBlobDirectoryICloudBlob<sup>2</sup>CloudBlockBlob<sup>2</sup>CloudPageBlob<sup>2</sup>CloudAppendBlob<sup>pps-2</sup>
<sup>1</sup> function.json 中需有 "in" 绑定 direction 或 C# 类库中需有 FileAccess.Read。 但是,可以使用运行时提供的容器对象来执行写入操作,例如将 Blob 上传到容器。
<sup>2</sup> function.json 中需有 "inout" 绑定 direction 或 C# 类库中需有 FileAccess.ReadWrite。
如果尝试绑定到某个存储 SDK 类型并收到错误消息,请确保已引用正确的存储 SDK 版本。
由于整个 Blob 内容都会加载到内存中,因此,只有当 Blob 较小时才建议绑定到 string 或 Byte[]。 平时,最好使用 Stream 或 CloudBlockBlob 类型。 有关详细信息,请参阅本文前文中的并发和内存使用情况。
其他类型
使用 存储扩展插件的5.0.0 或更高版本 的应用程序也可以使用 Azure SDK for .net中的类型。 此版本支持旧的 CloudBlobContainer 、、、 CloudBlobDirectory 、 ICloudBlob CloudBlockBlob CloudPageBlob 和 CloudAppendBlob 类型,以支持以下类型:
- BlobContainerClient<sup>1</sup>
- BlobClient<sup>2</sup>
- BlockBlobClient<sup>2</sup>
- PageBlobClient<sup>2</sup>
- AppendBlobClient<sup>2</sup>
- BlobBaseClient<sup>2</sup>
<sup>1</sup> function.json 中需有 "in" 绑定 direction 或 C# 类库中需有 FileAccess.Read。 但是,可以使用运行时提供的容器对象来执行写入操作,例如将 Blob 上传到容器。
<sup>2</sup> function.json 中需有 "inout" 绑定 direction 或 C# 类库中需有 FileAccess.ReadWrite。
有关使用这些类型的示例,请参阅 GitHub 存储库中的扩展。
Java
使用 @BlobOutput 特性可以访问触发函数的 blob。 如果将字节数组与特性一起使用,请将 dataType 设置为 binary。 有关详细信息,请参阅输出示例。
JavaScript
使用 context.bindings.<BINDING_NAME> 访问 Blob 数据,其中绑定名称在 function.json 文件中定义。
PowerShell
通过与 function.json 文件中绑定名称参数指定的名称匹配的字符串参数访问 Blob 数据。
Python
可以将函数参数声明为以下类型以写出到 blob 存储:
- 字符串作为
func.Out(str) - 流为
func.Out(func.InputStream)
有关详细信息,请参阅输出示例。
异常和返回代码
| 绑定 | 参考 |
|---|---|
| Blob | Blob 错误代码 |
| Blob、表、队列 | 存储错误代码 |
| Blob、表、队列 | 故障排除 |
后续步骤
历史资料。此文对应 2022-08-12 的中文文档快照,适用产品版本和命令行为以原文为准。
来源与署名:Microsoft 和文档贡献者(原始作者元数据 craigshoemaker)。固定版本原文。文档采用 CC BY 4.0,示例代码采用 MIT 授权。本站将 Markdown 转为 HTML,规范化链接,移除外部媒体和导航标记。
