获取通过GitHub Actions部署的AWS .NET 6 Lambda函数中目录的路径。

huangapple go评论71阅读模式
英文:

Get the path to a directory in an AWS .NET 6 Lambda deployed via GitHub Actions

问题

我正在通过GitHub Actions部署AWS .NET 6 Lambda。函数处理程序需要读取一堆文本文件。文件夹结构如下:

text-files
  file1.txt
  file2.txt
  ...
Program.cs(处理程序在此)
MyFunction.csproj

我正在尝试获取text-files的路径,但这样做失败了:

var directoryPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "text-files");
// var directoryPath = Path.Combine(Directory.GetCurrentDirectory(), "text-files");
// var directoryPath = Path.Combine(Environment.GetEnvironmentVariable("LAMBDA_TASK_ROOT"), "text-files");

当我打印directoryPath的值时,所有上述方法都返回:

/var/task/text-files

这会失败,因为上述路径不存在。获取text-files路径的正确方法是什么,以便我可以读取该文件夹中的每个文件?

英文:

I'm deploying an AWS .NET 6 Lambda via GitHub Actions. The function handler needs to read a bunch of text files. The folder structure looks like this:

text-files
  file1.txt
  file2.txt
  ...
Program.cs (handler is here)
MyFunction.csproj

I'm trying to get the path to text-files, but this is failing:

var directoryPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "text-files");
// var directoryPath = Path.Combine(Directory.GetCurrentDirectory(), "text-files");
// var directoryPath = Path.Combine(Environment.GetEnvironmentVariable("LAMBDA_TASK_ROOT"), "text-files");

When I print out the value for directoryPath, all approaches above return:

/var/task/text-files

This fails because the above path does not exist. What's the right way to get the path to text-files so I can read each file in that folder?

答案1

得分: 1

使用IHostEnvironment.ContentRootPath获取应用程序的根路径,然后从那里构建您的路径。

英文:

Use IHostEnvironment.ContentRootPath to get the root path of your application and then construct your path from there.

huangapple
  • 本文由 发表于 2023年2月27日 19:10:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定