问题描述
使用 Azure 函数的绑定选项,可以根据从触发器派生的参数(例如触发函数的队列消息)指定要写入的 Blob 的名称;文档显示了一个例子.
Using the binding options for an Azure Function one can specify the name of a Blob to be written based on parameters derived from the trigger (e.g. the queue message that triggered the function); the documentation shows an example of that.
我的问题是:如果事先不知道 blob 名称,但实际上是 计算 作为函数执行的一部分,那么处理这种情况的最佳方法是什么?
My question is: what is the best way to handle the case where the blob name is not known in advance, but in fact is calculated as part of the function's execution?
相关:根据计算结果,如果函数可能产生或不产生输出 blob(或多个输出 blob!)该怎么办?
And related: what to do if the function may or may not produce an output blob (or multiple output blobs!), based on the outcome of its calculation?
据我现在所见,Azure Function 的绑定机制在这些情况下没有多大帮助,最简单的方法是引用一个执行 azure blob 编写经典方式"的程序集.但是还有更惯用的方式吗?
As far as I can see now Azure Function's binding mechanism doesn't help much in these cases and the easiest approach is to refer to an assembly that does the azure blob writing "the classical way". But is there a more idiomatic way?
推荐答案
你其实已经可以在 C# Azure Functions 中做到这一点了,我们有一个跟踪项 here in our repo 也为 Node.js 函数启用此功能.我们很快就会解决这个问题.
You can actually already to this in C# Azure Functions, and we have a tracking item here in our repo to enable this for Node.js Functions as well. We'll get to that soon.
下面是一个示例工作函数,它绑定到具有在运行时指定的路径的 blob.由于在幕后 Azure Functions 构建在 Azure WebJobs SDK,您会注意到这依赖于使用您可能不熟悉的 WebJobs SDK Binder
.有关 IBinder
/Binder
的更多文档,请参阅 WebJobs SDK.在 WebJobs SDK 中,声明性属性用于绑定(例如 QueueAttribute
/TableAttribute
/BlobAttribute
等).您可以在运行时通过 Binder
指定所有这些.在 Azure Functions 中,我们使用外部元数据来描述绑定,但在此高级方案中,您有一个混合.请注意,当使用 Binder
时,function.json
中没有对应的绑定.有关 Binder
动态绑定的更多详细信息,请参阅 this SO question/answer.
Below is an example working function that binds to a blob with the path specified at runtime. Since under the covers Azure Functions is built on the 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 this SO question/answer.
一般来说,您会发现许多很棒的 WebJobs SDK 功能都可以在 Azure Functions 中使用 - 我们的文档只需要赶上来让人们意识到这一点 :)
In general, you'll find that many awesome WebJobs SDK features are usable in Azure Functions - our doc just needs to catch up to make people aware of this :)
还有一点需要注意:为输出生成随机的新标识符有一些内置支持.例如.如果您将输出 blob 路径设置为 test-output/{rand-guid},系统将自动为您生成一个新 ID.如果满足您的需求,那么您不需要 Binder
.
One other thing to note: there is some inbuilt support for generating random new identifiers for outputs. E.g. if you were to set your output blob path to test-output/{rand-guid} the system will automatically generate a new ID for you. If that meets your needs, then you don't need 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);
}
对于第二个问题,如果您想有条件地写入输出绑定,请不要为该绑定分配任何值 - 不应产生任何输出.
For your second question, if you want to conditionally write to an output binding, just don't assign any value to the binding - no output should be produced.
这篇关于为 Azure 函数的输出 blob 生成名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!