azureazure-functionsstanford-nlpml.net

How to use local files in an Azure Function hosted on the Linux Consumption plan?


I have an event grid triggered function on Azure under the Linux Consumption plan. The code requires finding / loading some local model files. How do I get these files deployed to the Azure function and specify the path where the files are located? Any help is appreciated. Thanks!


Solution

  • To deploy the local file to Azure you need to Add Item in your .csproj file.

      <ItemGroup>
        <None Include="models\**\*">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
      </ItemGroup>
    

    Note : here models\**\* is used to add all subdirectories and all its files

    to get the path of the file use

    var path = Path.GetFullPath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
    

    Thanks to @benspeth and @Louie Almeda For reference check this GitHub and SO Link respectively.

    #My Directory

    enter image description here

    In Isolated model we need to define custom type for Event Properties as mentioned in document.

    EventGridTrigger.cs:

    using System;
    using System.Text.Json;
    using Azure.Messaging;
    using System.Reflection;
    using Microsoft.Azure.Functions.Worker;
    using Microsoft.Extensions.Logging;
    
    namespace Company.Function
    {
        public class EventGridTrigger
        {
            private readonly ILogger<EventGridTrigger> _logger;
    
            public EventGridTrigger(ILogger<EventGridTrigger> logger)
            {
                _logger = logger;
            }
    
            [Function(nameof(EventGridTrigger))]
            public void Run([EventGridTrigger] MyEventType myEvent, FunctionContext context)
            {
                Console.WriteLine($"Event type: {myEvent.EventType}, Event subject: {myEvent.Subject}");
    
                try
                {
                    var path = Path.GetFullPath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
                    Console.WriteLine(path);
                    string modelContent = File.ReadAllText(path+"/models/model1.json");
    
                    Console.Write($"Data of the file: {modelContent.ToString()}");
                }
                catch (Exception ex)
                {
                    _logger.LogError($"Error processing model1.json: {ex.Message}");
                }
            }
        }
        public class MyEventType
        {
            public string Id { get; set; }
    
            public string Subject { get; set; }
    
            public string Topic { get; set; }
    
            public string EventType { get; set; }
    
            public DateTime EventTime { get; set; }
    
            public IDictionary<string, object> Data { get; set; }
        }
    }
    

    Csharp.csproj:

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <AzureFunctionsVersion>v4</AzureFunctionsVersion>
        <OutputType>Exe</OutputType>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
      </PropertyGroup>
      <ItemGroup>
        <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.20.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.EventGrid" Version="3.3.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.2" />
        <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.0.0" />
      </ItemGroup>
      <ItemGroup>
        <None Update="host.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
        <None Update="local.settings.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <CopyToPublishDirectory>Never</CopyToPublishDirectory>
        </None>
      </ItemGroup>
      <ItemGroup>
        <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
      </ItemGroup>
      <ItemGroup>
        <None Include="models\**\*">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
      </ItemGroup>
    </Project>
    

    model.json:

    {
      "model_name": "Model1",
      "description": "This is a sample model file.",
      "value":10,
      "version": "1.0"
    }
    

    OUTPUT: enter image description here

    enter image description here

    enter image description here