为 Azure 函数生成输出 blob 的名称

Generating names for output blobs for an Azure Function

使用 Azure 函数的绑定选项,可以根据从触发器派生的参数(例如触发函数的队列消息)指定要写入的 Blob 的名称;文档显示了一个例子。

我的问题是:如果事先不知道 blob 名称,但实际上 计算 作为函数执行的一部分,那么处理这种情况的最佳方法是什么?

相关:如果函数可能会或可能不会根据其计算结果产生输出 blob(或多个输出 blob!),该怎么办?

据我所知,Azure Functions 的绑定机制在这些情况下并没有太大帮助,最简单的方法是引用执行 azure blob 写入的程序集 "the classical way"。但是有没有更地道的方法呢?

你实际上已经可以在 C# Azure Functions 中做到这一点,我们有一个跟踪项 here in our repo 可以为 Node.js Functions 启用它。我们很快就会解决这个问题。

下面是一个示例工作函数,它在运行时绑定到具有指定路径的 blob。由于 Azure Functions 是建立在 Azure WebJobs SDK, you'll notice that this relies on using the WebJobs SDK Binder something that you might not be familiar with. Please see the WebJobs SDK for more documentation on IBinder/Binder. In the WebJobs SDK, declarative attributes are used for bindings (e.g. QueueAttribute/TableAttribute/BlobAttribute, etc.). You can specify all of these at runtime via Binder. In Azure Functions, we use external metadata to describe bindings, but in this advanced scenario you have a hybrid. Note that when using Binder there is no corresponding binding in function.json. For more details on Binder dynamic bindings see SO question/answer.

之上的

一般来说,您会发现许多很棒的 WebJobs SDK 功能都可以在 Azure Functions 中使用 - 我们的文档只需要跟上进度让人们意识到这一点 :)

另一件需要注意的事情:有一些内置支持为输出生成随机的新标识符。例如。如果您将输出 blob 路径设置为 test-output/{rand-guid},系统将自动为您生成一个新 ID。如果满足您的需求,那么您就不需要 Binder.

using System;
using System.IO;
using System.Net;
using Microsoft.Azure.WebJobs;

public static async Task<HttpResponseMessage> 
       Run(HttpRequestMessage req, Binder binder, TraceWriter log)
{
    log.Verbose($"C# HTTP function processed RequestUri={req.RequestUri}");

    using (var writer = await binder.BindAsync<TextWriter>(
                  new BlobAttribute("test-output/result")))
    {
        writer.Write("Hello World!!");
    }

    return new HttpResponseMessage(HttpStatusCode.OK);
}

对于你的第二个问题,如果你想有条件地写入输出绑定,只是不要为绑定分配任何值 - 不应产生任何输出。