touchofevil
touchofevil

Reputation: 613

Azure functions reading custom files

I am creating a generic azure function with an HTTP trigger. I have usecase something like this

Clients --> Function --> API

My function gets a certain input, it manipulates the structure and then sends that to another API for further processing. For this I have created a fairly static configuration settings required by API and stored it in JSON format. I would deserialize this into an object to pass to API. However am not able to load this JSON as the file doesn't seem to be found. This file was created in the root at the time of project creation.

I have tried to verify the files available but couldn't see the JSON on server. I tried printing list of files and directories in

Environment.CurrentDirectory

For now, I have used packaged deployment mode from VS17. This function is V1 and framework is 461.

Alternative solution,

  1. I could create an object directly with those settings and update as and when needed, at the cost of flexibility and readability. However JSON is much simpler to understand and change(not all people using this function are C# users).

  2. I can write a post build command to copy the file in function's bin folder so it can be packaged along.

This is not an application setting hence I don't want to put it there. Also its a nested JSON structure. I wanted to know if there's a better way of achieving this than what I could come up above? Am hoping to avoid having to move to V2 yet due to some dependencies while doing the function processing logic.

Upvotes: 2

Views: 2183

Answers (1)

Bhaumik D
Bhaumik D

Reputation: 38

I am working on similar project and had to use different json files to read data from. So was able to upload/read/write files in same folder as function folder after few attempts. You can basically upload the files in same folder as your azure function using kudu or function files editor.

Following is code snippet that will read from same folder as your function's.

 var path = Path.Combine(context.FunctionDirectory, "filename.json");
 dynamic fileData = await ReadAndDisplayFilesAsync(path , context);

I have created following function to read files:

    public static async Task<dynamic> ReadAndDisplayFilesAsync(string fileName, Microsoft.Azure.WebJobs.ExecutionContext context)
    {

        char[] buffer;

        using (var sr = new StreamReader(fileName))
        {
            buffer = new Char[(int)sr.BaseStream.Length];
            await sr.ReadAsync(buffer, 0, (int)sr.BaseStream.Length);
        }

        string bufferAsString = new String(buffer);
        dynamic bufferAsStringJson = JsonConvert.DeserializeObject(bufferAsString);
        return bufferAsStringJson;
    }

Do not forget to add "ExecutionContext context" as argument in your "Run" as following:

Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log, ExecutionContext context)

Upvotes: 2

Related Questions