Alex Gordon
Alex Gordon

Reputation: 60741

Where to store files for Azure function?

I have files that I reference from inside by C# code such as:

public static string Canonical()
{
    return File.ReadAllText(@"C:\\myapp\\" + "CanonicalMessage.xml");
}

How do I reference this file from within an Azure Function?

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    var data = File.ReadAllText(@"c:\\myapp\\" + "CanonicalMessage.xml");

    //etc
}

Perhaps I can simply embed this resource in the project?

Upvotes: 10

Views: 10095

Answers (1)

Jerry Liu
Jerry Liu

Reputation: 17790

Yes, put the file at the root of Azure Function project and set its property Copy to Output Directory to Copy if newer. Use code below.

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log, ExecutionContext context)
{
    var data = File.ReadAllText(context.FunctionAppDirectory+"/CanonicalMessage.xml");

    //etc
}

Check the doc for more details.

If we need to add this file from anywhere locally, right click on Function project, Edit <FunctionProjectName>.csproj. Add Item below, relative or absolute path are both ok.

<ItemGroup>
  <None Include="c:\\myapp\\CanonicalMessage.xml">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </None>
</ItemGroup> 

Upvotes: 12

Related Questions