Azure功能 - Blob Stream动态输入绑定

问题描述 投票:2回答:2

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

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

如果它有帮助我设法找到这个动态输出绑定

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

提前谢谢,Cuan

c# function azure inputstream azure-storage-blobs
2个回答
0
投票

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

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);
}

enter image description here

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

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

更新:

对于动态输入绑定,您只需更改绑定类型,如下所示:

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

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

#r "Microsoft.WindowsAzure.Storage"
using Microsoft.WindowsAzure.Storage.Blob;

CloudBlockBlob blob = await binder.BindAsync<CloudBlockBlob>(attributes);

此外,关于CloudBlockBlob操作的更多细节,你可以关注here


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));
            }
© www.soinside.com 2019 - 2024. All rights reserved.