我在azure上运行C#函数,需要从容器中获取文件。唯一的问题是输入文件的路径每次都会(可能)不同,输入文件的数量从1到4或5不等。因此我不能只使用默认输入据我所知,blob绑定。我的选择是给容器匿名访问,只需通过链接获取文件或找出如何获得动态输入绑定。
有没有人知道如何在运行时声明输入blob流的路径(在C#代码中)?
如果有帮助我设法找到动态输出绑定
using (var writer = await binder.BindAsync<TextWriter>(
new BlobAttribute(containerPath + fileName)))
{
writer.Write(OutputVariable);
}
提前致谢,Cuan
答案 0 :(得分: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));
}
答案 1 :(得分: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);
}
注意:上面的代码会创建目标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。