首页 > 解决方案 > 读取 Blob 存储 Azure 函数 HttpTrigger

问题描述

尽管在这里或其他地方有很多帖子,但我仍然没有找到如何从 azure 函数读取 blob 存储。

我有如下

在此处输入图像描述

上述每个容器都有一个json文件“customer.json”</p>

现在我需要调用我的函数并传递一个参数,例如“london”来检索伦敦客户

Customer customer= await azureFunctionService.GetCustomer(“London”);

函数应该是什么样子,理想情况下我想使用输入绑定从函数中读取 json 文件,但任何其他方式也可以。

        [FunctionName("GetCustomer")]
        public static void Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            string inputBlobPath,
            [Blob("howDoIBuildPathIncludingtheparameter", 
                FileAccess.Read, Connection = "WhereDoIGetThis")] string json,
            ILogger log)
        {
            // Not sure if anything is required here apart from logging when using input binding
            //
        }

有什么建议么?

非常感谢

标签: azure-functions

解决方案


此 Microsoft 文档提供了一个简短示例,说明如何从 中提取数据HttpTrigger以填充 blob 的输入绑定路径:https ://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-expressions-模式#json-payloads

如上所述,单独定义有效负载对象,然后将此类型与HttpTrigger属性一起使用。然后可以在其他输入绑定表达式中引用对象属性。

public class BlobInfo
{
    public string CityName { get; set; }
}

public static class GetCustomer
{
    [FunctionName("GetCustomer")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] BlobInfo info,
        [Blob("{CityName}/customer.json", FileAccess.Read)] Stream blob,
        ILogger log)
    {

使用指定所需城市名称的 JSON 有效负载调用此函数,例如curl http://localhost:7071/api/GetCustomer -d "{'CityName':'manchester'}".

这将使用名为“manchester”、名为“customer.json”的容器中的 blob 内容初始化 blob 输入参数。


推荐阅读