Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 329
Alex Gordon Avatar asked Nov 19 '18 01:11

Alex Gordon


People also ask

Can Azure function save file locally?

Azure Function save file locally First, copy the local file and keep it in the Azure Function root directory. Then we need to set the property of that file i.e CopyToOutputDirectory to PreserveNewest. Now use the ExecutionContext context as the Azure function parameter so that you can get access to the context.

Does Azure function need storage account?

Storage account requirements When creating a function app, you must create or link to a general-purpose Azure Storage account that supports Blob, Queue, and Table storage. This is because Functions relies on Azure Storage for operations such as managing triggers and logging function executions.


1 Answers

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> 
like image 109
Jerry Liu Avatar answered Oct 19 '22 16:10

Jerry Liu