skip to Main Content

I am using an Azure Function that has a HTTP trigger with a route parameter {id} which is the fileId of the JSON file I want to read.
I am using a Blob Input Binding to bind where my JSON files are stored. The JSON files are stored in a container called "conversations" and then in a folder called "Conversation".
An example of a file route is "https://<STORAGE_ACCOUNT_NAME>/conversations/Conversation/8da3d7ad3e35273-1aWpKU4rVghHiTaYkjOjVC-eu%7C0000000.json"

Below is my code.

public static class GetConvo
    {
        [FunctionName("GetConvo")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "getConvo/{id}")] HttpRequest req,
            [Blob("conversations/{id}", FileAccess.Read, Connection = "AzureWebJobsStorage")] string json,
            ILogger log, string id)

        {
            
            log.LogInformation($"File name: {id}");

            if (json == null)
            {
                log.LogInformation($"File {id} not found");
                return new NotFoundResult();
            }
            else
            {
                log.LogInformation($"Content: {json}");
            }


            return new OkObjectResult(JsonConvert.DeserializeObject<Message>(json));

The above code works if I move a JSON file to outside the "Conversation" folder, I can access it and receive a 200OK code.

enter image description here

I have tried changing the Blob input binding path to "conversations/Conversation/{id}" as below but that returns a 404 code.

[FunctionName("GetConvo")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "getConvo/{id}")] HttpRequest req,
            [Blob("conversations/Conversation/{id}", FileAccess.Read, Connection = "AzureWebJobsStorage")] string json,
            ILogger log, string id)

Is this a blob input path problem?

How would I read JSON files that are in a folder in a blob container using an azure function?

2

Answers


  1. Chosen as BEST ANSWER

    I found out what my error was.

    The blob input path was correct. It was a URL encoding problem for:

    https://<STORAGE_ACCOUNT_NAME>/conversations/Conversation/8da3d7ad3e35273-1aWpKU4rVghHiTaYkjOjVC-eu%7C0000000.json
    

    The % was not recognised and required; putting 25 after the % resolved this error:

    https://<STORAGE_ACCOUNT_NAME>/conversations/Conversation/8da3d7ad3e35273-1aWpKU4rVghHiTaYkjOjVC-eu%257C0000000.json
    

  2. @AjgB, yes the Blob path is incorrect. You need to provide the file extension.

    Lets say the file is placed directly in your ‘conversations’ folder. Then your BLOB input bindings should be –

    [Blob("conversations/{id}.json", FileAccess.Read, Connection = "AzureWebJobsStorage")] string json
    

    Note the .json in the blob path

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search