如何从Azure函数输出多个Blob?

Chr*_*ton 5 azure-functions

输出绑定具有示例:

ICollector<T> (to output multiple blobs)
Run Code Online (Sandbox Code Playgroud)

并且:

Path must contain the container name and the blob name to write to. For example,
if you have a queue trigger in your function, you can use "path":
"samples-workitems/{queueTrigger}" to point to a blob in the samples-workitems
container with a name that matches the blob name specified in the trigger
message.
Run Code Online (Sandbox Code Playgroud)

在“集成” UI中,默认值为:

Path: outcontainer/{rand-guid}
Run Code Online (Sandbox Code Playgroud)

但这不足以让我取得进展。如果我使用C#编码,那么function.json和run.csx将多个blob输出到一个容器的语法是什么?

mat*_*ewc 3

您可以通过多种不同的方式来实现此目的。首先,如果需要输出的 blob 数量是固定的,则可以只使用多个输出绑定。

using System;

public class Input
{
    public string Container { get; set; }
    public string First { get; set; }
    public string Second { get; set; }
}

public static void Run(Input input, out string first, out string second, TraceWriter log)
{
    log.Info($"Writing 2 blobs to container {input.Container}");
    first = "Azure";
    second = "Functions";
}
Run Code Online (Sandbox Code Playgroud)

以及对应的function.json:

{
  "bindings": [
    {
      "type": "manualTrigger",
      "direction": "in",
      "name": "input"
    },
    {
      "type": "blob",
      "name": "first",
      "path": "{Container}/{First}",
      "connection": "functionfun_STORAGE",
      "direction": "out"
    },
    {
      "type": "blob",
      "name": "second",
      "path": "{Container}/{Second}",
      "connection": "functionfun_STORAGE",
      "direction": "out"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

为了测试上述内容,我向该函数发送了一个测试 JSON 负载,并生成了 blob:

{
  Container: "test",
  First: "test1",
  Second: "test2"
}
Run Code Online (Sandbox Code Playgroud)

上面的示例演示了如何从输入(通过路径表达式)绑定 blob 容器/名称值{Container}/{First} {Container}/{Second}。您只需定义一个 POCO 来捕获您想要绑定的值。为了简单起见,我在这里使用了 ManualTrigger,但这也适用于其他触发器类型。另外,虽然我选择绑定到out string类型,但您可以绑定到任何其他受支持的类型:TextWriterStreamCloudBlockBlob等。

如果您需要输出的 blob 数量是可变的,那么您可以使用Binder在函数代码中强制绑定和写入 blob。请参阅此处了解更多详细信息。要绑定到多个输出,您只需使用该技术执行多个命令式绑定。

仅供参考:我们的文档不正确,因此我在这里记录了一个错误以修复该问题:)