[英]save file to temporary location in Azure Functions
I have an Azure function and I am trying to save a.xlsx file to a temporary folder.我有一个 Azure function,我正在尝试将 a.xlsx 文件保存到一个临时文件夹中。 My code works locally, but when I publish to Azure I get an error message.
我的代码在本地运行,但是当我发布到 Azure 时,我收到一条错误消息。
string projectDirectory = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName + @"\temp\";
string tempFilename = "abc.xlsx";
string pathTradeRecFile = Path.Combine(projectDirectory, tempFilename);
My error message.我的错误信息。 <--- Access to the path 'C:\Program Files (x86)\SiteExtensions\temp\abc.xlsx' is denied.
<--- 拒绝访问路径“C:\Program Files (x86)\SiteExtensions\temp\abc.xlsx”。
Can someone please tell me how I can save this file somewhere?有人可以告诉我如何将这个文件保存在某个地方吗? I made a folder in my structure named "temp" as one possible solution, but I can't seem to access it.
我在我的结构中创建了一个名为“temp”的文件夹作为一种可能的解决方案,但我似乎无法访问它。
Any help would be greatly appreciated!!任何帮助将不胜感激!!
Please do not use something like Environment.CurrentDirectory
in Azure Functions (or actually, just anywhere) to get a temp folder.请不要使用 Azure 函数中的
Environment.CurrentDirectory
之类的东西(或者实际上,只是在任何地方)来获取临时文件夹。 Instead use the .NET-native method to do so:而是使用 .NET-native 方法来这样做:
Path.GetTempPath()
So ideally use something like this:所以理想情况下使用这样的东西:
string pathTradeRecFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".xlsx");
My apologies, but I did not get your intention to save file in Azure storage file system?抱歉,我没明白你要将文件保存在 Azure 存储文件系统中的意思? However if azure function allows to save file locally then you should use
Directory.GetCurrentDirectory();
但是,如果azure function允许在本地保存文件,那么您应该使用
Directory.GetCurrentDirectory();
which resolves to D:\home\site\wwwroot
path.解析为
D:\home\site\wwwroot
路径。
Coming to my initial point, if you have a requirement to save the file locally to finally upload at persistent storage like Azure Blob
then you don't need to save file locally at file system;回到我的初始点,如果您需要将文件保存在本地以最终上传到持久性存储(如
Azure Blob
,那么您不需要将文件保存在文件系统本地; you can use MemoryStream
as shown in below code to upload the content at Azure blob
您可以使用如下代码所示的
MemoryStream
将内容上传到Azure blob
using (var ms = new MemoryStream())
{
using (StreamWriter writer = new StreamWriter(ms))
{
writer.Write(obj); // here obj represents the file data which you need to upload
writer.Flush();
ms.Position = 0
};
await blob.UploadFromStreamAsync(ms);
}
Environment variables as seen by an Azure Function aren't the same as the overall OS Environment variables. Azure Function 看到的环境变量与整体操作系统环境变量不同。 See this page for how to configure them: https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings?tabs=portal#settings .
请参阅此页面了解如何配置它们: https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings?tabs=portal#settings . The System.Environment.GetEnvironmentVariable(name) call returns this value when running in Azure. Locally, the value comes from the local.settings.json file: https://learn.microsoft.com/en-us/azure/azure-functions/functions-do.net-class-library?tabs=v2%2Ccmd#environment-variables .
System.Environment.GetEnvironmentVariable(name) 调用在 Azure 中运行时返回此值。在本地,该值来自 local.settings.json 文件: https://learn.microsoft.com/en-us/azure/azure- functions/functions-do.net-class-library?tabs=v2%2Ccmd#environment-variables 。
I have an Azure function and I am trying to save a.xlsx file to a temporary folder.我有一个 Azure function 并且我正在尝试将 a.xlsx 文件保存到临时文件夹。 My code works locally, but when I publish to Azure I get an error message.
我的代码在本地工作,但是当我发布到 Azure 时,我收到一条错误消息。
string projectDirectory = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName + @"\temp\";
string tempFilename = "abc.xlsx";
string pathTradeRecFile = Path.Combine(projectDirectory, tempFilename);
My error message.我的错误信息。 <--- Access to the path 'C:\Program Files (x86)\SiteExtensions\temp\abc.xlsx' is denied.
<--- 对路径“C:\Program Files (x86)\SiteExtensions\temp\abc.xlsx”的访问被拒绝。
Can someone please tell me how I can save this file somewhere?有人可以告诉我如何将这个文件保存在某个地方吗? I made a folder in my structure named "temp" as one possible solution, but I can't seem to access it.
我在我的结构中创建了一个名为“temp”的文件夹作为一种可能的解决方案,但我似乎无法访问它。
Any help would be greatly appreciated!!任何帮助将不胜感激!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.