Azure-functions-host: Make it easier for functions to access other files in their folder

Created on 1 Jun 2016  路  21Comments  路  Source: Azure/azure-functions-host

This came from internal Yammer group.

If a function folder contains a data.txt file, it is currently difficult to read that file from the function code. Typically, common patterns to do this would be:

  1. Rely on the current folder being set to the function folder as it runs
  2. Rely on some environment variable pointing to the function folder

Unfortunately, these can't work today since all functions run in the same process. We need to come up with an approach to solve this.

improvement

Most helpful comment

@abergs See here for a sample that does that.

All 21 comments

This is interesting for C#. Node.js can use __dirname for relative references (which is best practice). @fabiocav, ideas? Talk with .csx folks?

I have a use case today where I want to execute an exe in a powershell script to untar files - I guess this might be the stumbling block to achieving it. It would be very handy to be able to hold important reference files & metadata in the directory

@stephlocke - With reference to your original inquiry on StackOverflow, I believe that you also need to upload 7z.dll into the wwwroot directory.

Could you try using the following code-segment in your script?

Set-Location D:\home\site\wwwroot\poshUntar
.\7z.exe x *.tar.gz 
.\7z.exe x *.tar -ologs

Duplicate #803 currently being worked on.

One more SO question based on the assumption about default directory = function directory http://stackoverflow.com/questions/43498379/azure-functions-execution-error

@fabiocav that is set to resolved, and a commit has been registered against it, but no solution is provided.

This will be deployed on the next release. I'll add some documentation to the Wiki and link here.

Feature documentation here.
This is being rolled out with the next deployment (which began today, 5/30).

The properties on the ExecutionContext are very helpful for C# projects. But I have another question/feature request:

I have some static files as part of my project (custom encoder presets stored as json or xml, etc) that I want to File.Read() into my function at runtime. Given that C# now uses attributes instead of folders, I keep all of my individual functions as .cs files in the root of my project along side the static files. ExecutionContext.FunctionDirectory only has function.json in it, and if i ../ up one level, my project-level static files are not in that folder. It turns out that they are in ../bin/ relative to the function folder.

However, when running the project locally using the VS Tools, the project .dlls and other output are not in a 'bin' subfolder but at the root of the output directory. So I cant reference the file using the same path locally and when deployed.

PROJECT
-MyFunction1.cs
-MyFunction2.cs
-SomeOtherFile.json

How can I reference SomeOtherFile.json from my function both locally and when deployed?

Same question as @briandunnington

@abergs See here for a sample that does that.

@davidebbo I think what @briandunnington is looking for is a way to pick a different path when function is running locally or on cloud.

With a Data\HelloHttpOutputTemplate.txt file under your project root, as in your example, when running locally that path will be a sibling of function directory (say HelloHttp\). When running on cloud instead, that path will be a children of function directory.

So all in all I guess one can find a way to detect whether it's running locally or on cloud (e.g. check some environment variable), and then have:

```c#
templateFile = isRunningLocally
? Path.Combine(context.FunctionAppDirectory, "../", "Data", "HelloHttpOutputTemplate.txt")
: Path.Combine(context.FunctionAppDirectory, "Data", "HelloHttpOutputTemplate.txt");

Does that make sense?

**EDIT**

Actually just found out that things are (not) working differently. I just published a similar function, with some `outfiles\publish.me` file in function project, only to find that on azure file storage I see:

wwwroot\
\
...
outfiles\
publish.me
```

According to your example, I'd expected to see <my function name>\outfiles\publish.me. What are we doing wrong? TA

@BrainCrumbz I'm not sure I follow. With the sample I posted, the behavior is the same locally and on Azure. Did you try that?

Doh! Just noticed that your example uses Microsoft.NET.Sdk.Functions 1.0.5 and context.FunctionAppDirectory, so that takes care of Data directory being a sibling of HelloHttp function directory.

All is set now: same behavior between local machine and cloud.
BTW thanks for always quick replies!

@BrainCrumbz yes, it's unfortunate that new project end up with the older reference that dont' have that. I know this is something that will be fixed by VS team, though not exactly sure when. Anyway, glad you figured it out!

[C#]
For the function class, I'm using a static constructor to initialize a series of objects. I wonder if it is possible to somehow access the execution context in this static constructor? I mean, things like FunctionAppDirectory and FunctionDirectory should be available at this time, while InvocationId and FunctionName should not.

@Fabiest currently, that is not possible/supported. We have some DI changes landing soon, and we could consider passing that information into an _instance_ constructor with that model.

Is it possible to access the TraceWriter object for logging within an function of type ActivityTrigger?

If not possible, can you consider using via DI for this object also?

Thank you

@fabiocav is there any sort of time estimate for when these DI changes will be released and will those constructor changes be included?

Thank you

So how is it now possible to access a file in a nodejs azure function?
How does the file gets included in the packaged zip?

Was this page helpful?
0 / 5 - 0 ratings