1

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

Sarah
  • 128
  • 10

2 Answers2

1

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
    // ...
}
Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107
  • Yeah, but the .mmdb file is not present on the server. I believe there is an extra build step I would need to do to copy it to the right directory?? – Sarah Jul 24 '18 at 20:00
  • In Kudu I can look inside that directory `D:\home\site\wwwroot\ls` `function.json` My .mmdb file is not there – Sarah Jul 24 '18 at 20:07
  • @Sarah You can add your file to Visual Studio project, mark it as Content and Copy to Output – Mikhail Shilkov Jul 24 '18 at 21:18
  • Ahh okay, I have found an internal method that is working. So I haven't checked if "Copy to Output" will work. Hopefully this will be helpful of others that stumble across this :) – Sarah Jul 24 '18 at 22:36
0

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.

Sarah
  • 128
  • 10