如何在 Linux 消费计划上托管的 Azure 函数中使用本地文件?

问题描述 投票:0回答:1

我在 Linux 消费计划下的 Azure 上有一个事件网格触发功能。该代码需要查找/加载一些本地模型文件。如何将这些文件部署到Azure功能并指定文件所在路径?任何帮助表示赞赏。谢谢!

azure azure-functions stanford-nlp ml.net
1个回答
0
投票

要将本地文件部署到 Azure,您需要在

.csproj
文件中添加项目。

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

注意:此处

models\**\*
用于添加所有子目录及其所有文件

获取文件的路径使用

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

感谢@benspeth 和@Louie Almeda 如需参考,请分别检查此 GitHubSO Link

#我的目录

enter image description here

Isolated
模型中,我们需要为事件属性定义自定义类型,如文档中所述。

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

© www.soinside.com 2019 - 2024. All rights reserved.