如何将Azure函数的输入参数绑定到存储账户?

4
我将尝试创建一个Azure函数(.NET core),从存储账户中检索SaS,但我无法理解为什么无法访问该存储。
函数签名:
public static async Task<IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
[StorageAccount("StorageConnectionString")] StorageAccount storage,
ILogger log)

运行时出现以下错误:
``` Error indexing method 'GetBlobSaS' Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'storage' to type StorageAccount. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.). ```
如果我直接创建存储账户引用,则可以正常工作。
var storage = StorageAccount.NewFromConnectionString(System.Environment.GetEnvironmentVariable("StorageConnectionString"));

我哪里理解错误或做错了呢?
1个回答

5
您遇到的错误是函数运行时无法理解方法签名。[StorageAccount]属性需要CloudStorageAccount。因此,请将签名更改为:
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
    [StorageAccount("StorageConnectionString")] CloudStorageAccount storage,
    ILogger log)
{

不,那不是自定义属性。请参阅https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-queue#trigger---attributes和https://github.com/Azure/azure-webjobs-sdk/blob/master/src/Microsoft.Azure.WebJobs/StorageAccountAttribute.cs。 - Krumelur
好的,但看起来你将它添加为绑定属性。它并不是作为绑定而意图使用的,而是作为一种指定在使用存储绑定(如QueueTrigger或Table绑定)时要使用哪个存储帐户的方式。 - Marco van Kimmenade
也许我误解了文档。你的意思是我可以将 blob 用作输入绑定,但不能获得直接的存储引用? - Krumelur
不确定,可能我理解有误。根据 https://stackoverflow.com/questions/46879065/how-to-bind-cloudstorageaccount-input-to-azure-function ,应该能够工作。也许您应该将其更改为 [StorageAccount("StorageConnectionString")] CloudStorageAccount 存储? - Marco van Kimmenade
摇滚乐!就是这样!快更新你的答案! :-) - Krumelur

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接