简体   繁体   中英

Azure Function with HTTP Trigger and Blob Input Binding - can't read JSON files that are in a folder. Possible blob file path error?

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.

在此处输入图像描述

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?

@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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM