【问题标题】:Read Blob Storage Azure Function HttpTrigger读取 Blob 存储 Azure 函数 HttpTrigger
【发布时间】:2020-12-29 19:49:16
【问题描述】:

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

我有如下

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

现在我需要调用我的函数并传递一个参数,例如“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 Function 2 或更高版本
  • @maxspan 您好是版本 3。默认创建我使用 Visual Studio 2019 16.5 创建函数。非常感谢任何帮助。

标签: azure-functions


【解决方案1】:

此 Microsoft 文档提供了一个简短示例,说明如何从 HttpTrigger 中提取数据以填充 blob 的输入绑定路径:https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-expressions-patterns#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 输入参数。

【讨论】:

    【解决方案2】:

    您是否尝试过以下代码。 以下示例是使用队列触发器和输入 blob 绑定的 C# 函数。队列消息包含 blob 的名称,该函数记录 blob 的大小。

    [FunctionName("BlobInput")]
    public static void Run(
        [QueueTrigger("myqueue-items")] string myQueueItem,
        [Blob("samples-workitems/{queueTrigger}", FileAccess.Read)] Stream myBlob,
        ILogger log)
    {
        log.LogInformation($"BlobInput processed blob\n Name:{myQueueItem} \n Size: {myBlob.Length} bytes");
    

    }

    【讨论】:

    • 我不知道它使用的是“QueueTrigger”而不是 HttpTrigger,而且在我的情况下,我应该在此处放置“samples-workitems/{queueTrigger}”什么,您将如何更改它以适合我的情况。如果你喜欢,我需要理解语法
    • 是的,请将 QueueTrigger 替换为 Httptrigger
    • 好的,我在我的问题中做了,但是这里的语法是什么 [Blob("samples-workitems/{queueTrigger} 并且应该与 blob 存储有连接
    • 显然我的 blobstoragename =london 和 filename=customer.json 并且我不想硬编码,那么在函数中属性如何读取它?
    • 你需要在Route值中传递它
    猜你喜欢
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    • 2012-06-16
    • 2020-02-23
    • 2021-10-08
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多