azure-functions – 如何从Azure函数输出多个blob?

Out绑定有例子:

ICollector<T> (to output multiple blobs)

并且:

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.

默认情况下,“Integrate”UI默认为:

Path: outcontainer/{rand-guid}

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

最佳答案 有几种不同的方法可以实现这一目标.首先,如果您需要输出的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";
}

和相应的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"
    }
  ]
}

为了测试上述内容,我将测试JSON有效负载发送到该函数,并生成blob:

{
  Container: "test",
  First: "test1",
  Second: "test2"
}

上面的示例演示了如何从输入绑定blob容器/名称值(通过{Container} / {First} {Container} / {Second}路径表达式).您只需定义一个POCO捕获要绑定的值.为简单起见,我在这里使用了ManualTrigger,但这也适用于其他触发器类型.此外,当我选择绑定到字符串类型时,您可以绑定到任何其他支持的类型:TextWriter,Stream,CloudBlockBlob等.

如果您需要输出的blob数量是可变的,那么您可以使用Binder强制绑定并在函数代码中编写blob.有关详细信息,请参见here.要绑定到多个输出,您只需使用该技术执行多个命令性绑定.

仅供参考:我们的文档不正确,所以我记录了一个错误here来修复:)

点赞