developer9969
developer9969

Reputation: 5226

Read Blob Storage Azure Function HttpTrigger

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

Upvotes: 2

Views: 3752

Answers (3)

Vasudev
Vasudev

Reputation: 1007

If you want to avoid passing the parameter from request body, instead read the mapping parameter from query param, below snippet should help you. The Query reference helps you fetch the query parameters.

[Blob("{Query.container}/{Query.blobpath}", FileAccess.Read)] string jsonBlobStr

Instead of declaring jsonBlobStr as str, you could rather declare as Stream, so that whole file is not loaded onto memory.

Then call the HttpTrigger endpoint by passing the required parameters.

http://localhost:7071/api/GetCustomer?container=my-container&blobpath=path/to/customer.json

Upvotes: 0

Claire Furney
Claire Furney

Reputation: 2378

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://learn.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'.

Upvotes: 4

maxspan
maxspan

Reputation: 14137

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");

}

Upvotes: 0

Related Questions