Reference file from Azure function 2.0 with .net core

1.3k Views Asked by At

I am trying to create an Azure function that reads from a .mmdb file GeoLite2 Country DB

I have added the geolite2 file next to my function. But I cannot find a way programmatically to reference the file path such that it remains the same on my local machine as well as deployed/published.

string GeoLocationDbPath = "D:<path_to_project>\Functions\GeoLocation-Country.mmdb"
var reader = new DatabaseReader($"{GeoLocationDbPath}");

I came across this article How to add assembly references to an Azure Function App

I was hoping there was a better way to reference a file both locally and deployed.

Any ideas?


Other links I've looked at:

How to add and reference a external file in Azure Function

How to add a reference to an Azure Function C# project?

Retrieving information about the currently running function

Azure functions – Read file and use SendGrid to send an email

2

There are 2 best solutions below

4
Mikhail Shilkov On

You can get the path to folder by injecting ExecutionContext to your function:

public static HttpResponseMessage Run(HttpRequestMessage req, ExecutionContext context)
{
    var funcPath = context.FunctionDirectory; // e.g. d:\home\site\wwwroot\HttpTrigger1
    var appPath = context.FunctionAppDirectory; // e.g. d:\home\site\wwwroot
    // ...
}
0
Sarah On

I've come to the conclusion that referencing files local to the Azure function was not a good approach. I read the Azure functions best practices and Azure functions are meant to be stateless. Also, folder structure changes when you deploy.

If I were to continue I would upload the .mmdb file to a Azure blob container and use the CloudStorageAccount to access the file.