[英]How do I upload file to Azure function while publishing the code from Visual Studio?
I have created a Azure Function
, which is having a dependency on ini
file.我创建了一个
Azure Function
,它依赖于ini
文件。
public class DataProcessingFunction
{
FunctionName("DataProcessingFunction")]
public async Task Run([EventGridTrigger]EventGridEvent eventGridEvent,ILogger log)
{
string iniFolderPath = $@"{Directory.GetCurrentDirectory()}\Ini\";
string iniFileName = "Sample.ini";
var iniConfig = FileManager.ReadFile(iniFolderPath, iniFileName);
}
}
I have selected Copy if never
option in Visual Studio while publishing the code to Azure function Also, I have tried selecting Embedded Resource
.在将代码发布到 Azure 函数时,我在 Visual Studio 中选择了
Copy if never
选项此外,我尝试选择Embedded Resource
。 But I am not able to find the file但是我找不到文件
I get an exception我得到一个例外
File not found.
文件未找到。
Add/Upload option in Azure portal is disabled because I am publishing the function from Visual studio Azure 门户中的添加/上传选项被禁用,因为我正在从 Visual Studio 发布该功能
Question: Do I need to upload file to blob and then refer it in a code?问题:我是否需要将文件上传到 blob,然后在代码中引用它?
The final conclusion is that the file was successfully uploaded, the problem is that an error occurred while reading the path.最后的结论是文件上传成功,问题是读取路径时出错。 It seems that using
Directory.GetCurrentDirectory()
in Azure is not reliable.在 Azure 中使用
Directory.GetCurrentDirectory()
似乎不可靠。
I just tried it and Directory.GetCurrentDirectory()
got the wrong path in azure (I printed it out and it showed "D:\\Program Files (x86)\\SiteExtensions\\Functions\\2.0.12961\\32bit"
, This is obviously not the current folder), and eventually it fails to find the Sample.ini file.我刚刚试了一下,
Directory.GetCurrentDirectory()
在 azure 中得到了错误的路径(我打印出来并显示"D:\\Program Files (x86)\\SiteExtensions\\Functions\\2.0.12961\\32bit"
,这显然不是当前文件夹),并最终无法找到 Sample.ini 文件。 Since the function is your own, you can set the path to something like "D:\\\\home\\\\site\\\\wwwroot>\\\\Ini\\\\Sample.ini"
.由于该函数是您自己的,您可以将路径设置为类似
"D:\\\\home\\\\site\\\\wwwroot>\\\\Ini\\\\Sample.ini"
。 This should read the Sample.ini file.这应该读取 Sample.ini 文件。
Here is the way to do it.这是这样做的方法。
ExecutionContext context;
// You can modify your function method to take an additional parameter of type ExecutionContext // 您可以修改您的函数方法以采用类型为 ExecutionContext 的附加参数
public static async Task Run(<.... Other parameters>, ILogger log, ExecutionContext context)
Then build the path by combining function directory, "templates" is the directory I have in the function app project which is deployed along with other code, emails.html is file within templates (For each of these files you have to set, copy-always or copy-if-newer in the properties.).然后通过组合函数目录构建路径,“模板”是我在函数应用程序项目中与其他代码一起部署的目录,emails.html 是模板中的文件(对于每个必须设置的文件,复制-始终或在属性中复制如果更新。)。 Instead of templates you can have your .ini file.
您可以使用 .ini 文件代替模板。
string templatePath = Path.Combine(context.FunctionAppDirectory, "Templates", "emails.html");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.