Read Blob Storage Azure Function HttpTrigger

Viewed 2266

Despite many posts in here or elsewhere I still have not found how to read a blob storage from an azure function.

I have as follows

enter image description here

Each of the above containers has a json file “customer.json”

Now I need to call my function and pass a parameter eg "london" to retrieve the london customer

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

What should the function look like, ideally I would like to use input binding to read the json file from a function but any other way is fine too.

        [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
            //
        }

Any suggestions?

many thanks

2 Answers

This Microsoft documentation gives a brief example of how to extract data from an HttpTrigger to populate the input binding path of a blob: https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-expressions-patterns#json-payloads

As described, define the payload object separately and then use this type with the HttpTrigger attribute. The object properties are then available for reference in other input binding expressions.

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)
    {

Call this function with a JSON payload specifying the required city name e.g. curl http://localhost:7071/api/GetCustomer -d "{'CityName':'manchester'}".

This initialises the blob input parameter with the contents of the blob in a container named 'manchester' entitled 'customer.json'.

have you tried the below code. The following example is a C# function that uses a queue trigger and an input blob binding. The queue message contains the name of the blob, and the function logs the size of the 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");

}

Related