【问题标题】:Azure Functions - Blob Stream Dynamic Input bindingsAzure Functions - Blob 流动态输入绑定
【发布时间】:2018-05-17 10:54:08
【问题描述】:

我在 azure 上运行 C# 函数,它需要从容器中获取文件。唯一的问题是输入文件的路径每次都会(可能)不同,输入文件的数量将在 1 到大约 4 或 5 个之间变化。因此我不能只使用默认的输入 blob 绑定据我所知。我的选择是给容器匿名访问,只需通过链接获取文件或弄清楚如何获取动态输入绑定。

有谁知道如何在运行时(在 C# 代码中)声明输入 blob 流的路径?

如果有帮助,我已经设法为动态输出绑定找到了这个

using (var writer = await binder.BindAsync<TextWriter>(
                  new BlobAttribute(containerPath + fileName)))
    {
        writer.Write(OutputVariable);
    }

在此先感谢,川

【问题讨论】:

    标签: c# function azure inputstream azure-blob-storage


    【解决方案1】:

    试试下面的代码:

        string filename = string.Format("{0}/{1}_{2}.json", blobname,                         DateTime.UtcNow.ToString("ddMMyyyy_hh.mm.ss.fff"), Guid.NewGuid().ToString("n"));
    
        using (var writer = await binder.BindAsync<TextWriter>(
                new BlobAttribute(filename, FileAccess.Write)))
                {
                    writer.Write(JsonConvert.SerializeObject(a_object));
                }
    

    【讨论】:

    • 请详细说明而不是简单的代码 sn-p。
    【解决方案2】:

    对于动态输出绑定,您可以利用以下代码 sn-p:

    var attributes = new Attribute[]
    {    
        new BlobAttribute("{container-name}/{blob-name}"),
        new StorageAccountAttribute("brucchStorage") //connection string name for storage connection
    };
    using (var writer = await binder.BindAsync<TextWriter>(attributes))
    {
        writer.Write(userBlobText);
    }
    

    注意: 如果不存在,上述代码将创建目标 blob,如果存在则覆盖现有 blob。此外,如果您未指定 StorageAccountAttribute,则您的目标 blob 将根据应用设置 AzureWebJobsStorage 创建到存储帐户中。

    另外,您可以关注Azure Functions imperative bindings了解更多详情。

    更新:

    对于动态输入绑定,您可以按如下方式更改绑定类型:

    var blobString = await binder.BindAsync<string>(attributes);
    

    或者您可以将绑定类型设置为 CloudBlockBlob 并为 azure 存储 blob 添加以下命名空间:

    #r "Microsoft.WindowsAzure.Storage"
    using Microsoft.WindowsAzure.Storage.Blob;
    
    CloudBlockBlob blob = await binder.BindAsync<CloudBlockBlob>(attributes);
    

    另外,更多CloudBlockBlob的操作详情,请关注here

    【讨论】:

    • 这对动态输出很有帮助,但我该如何为输入 blob 做类似的事情呢?我的函数接受一个队列项,它用于确定它将使用哪些输入文件,但我无法找到任何用于在运行时声明输入路径的文档
    • 我更新了我的答案并添加了一些代码示例,你可以参考他们。
    • cannot convert from Attribute[] to Attribute
    猜你喜欢
    • 2017-04-06
    • 2021-11-02
    • 1970-01-01
    • 2021-11-12
    • 2019-08-20
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多