为Azure函数生成输出Blob的名称 [英] Generating names for output blobs for an Azure Function

查看:88
本文介绍了为Azure函数生成输出Blob的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用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的绑定机制无济于事,最简单的方法是引用一个以经典方式"编写天蓝色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中做到这一点,并且我们有一个跟踪项

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的功能是基于 Azure WebJobs SDK ,您会注意到这依赖于您可能不熟悉的WebJobs SDK Binder的使用.请参阅WebJobs SDK,以获取有关IBinder/Binder的更多文档.在WebJobs SDK中,声明性属性用于绑定(例如QueueAttribute/TableAttribute/BlobAttribute等).您可以在运行时通过Binder指定所有这些.在Azure Functions中,我们使用外部元数据来描述绑定,但是在此高级方案中,您需要使用混合功能.请注意,使用Binder时,function.json中没有相应的绑定.有关Binder动态绑定的更多详细信息,请参见

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的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆