繁体   English   中英

将文件保存到 Azure 函数中的临时位置

[英]save file to temporary location in Azure Functions

我有一个 Azure function,我正在尝试将 a.xlsx 文件保存到一个临时文件夹中。 我的代码在本地运行,但是当我发布到 Azure 时,我收到一条错误消息。

string projectDirectory = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName + @"\temp\";
string tempFilename = "abc.xlsx";
string pathTradeRecFile = Path.Combine(projectDirectory, tempFilename);

我的错误信息。 <--- 拒绝访问路径“C:\Program Files (x86)\SiteExtensions\temp\abc.xlsx”。

有人可以告诉我如何将这个文件保存在某个地方吗? 我在我的结构中创建了一个名为“temp”的文件夹作为一种可能的解决方案,但我似乎无法访问它。

任何帮助将不胜感激!!

请不要使用 Azure 函数中的Environment.CurrentDirectory之类的东西(或者实际上,只是在任何地方)来获取临时文件夹。 而是使用 .NET-native 方法来这样做:

Path.GetTempPath()

所以理想情况下使用这样的东西:

string pathTradeRecFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".xlsx");

抱歉,我没明白你要将文件保存在 Azure 存储文件系统中的意思? 但是,如果azure function允许在本地保存文件,那么您应该使用Directory.GetCurrentDirectory(); 解析为D:\home\site\wwwroot路径。

回到我的初始点,如果您需要将文件保存在本地以最终上传到持久性存储(如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);  
} 

Azure Function 看到的环境变量与整体操作系统环境变量不同。 请参阅此页面了解如何配置它们: https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings?tabs=portal#settings . 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

我有一个 Azure function 并且我正在尝试将 a.xlsx 文件保存到临时文件夹。 我的代码在本地工作,但是当我发布到 Azure 时,我收到一条错误消息。

string projectDirectory = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName + @"\temp\";
string tempFilename = "abc.xlsx";
string pathTradeRecFile = Path.Combine(projectDirectory, tempFilename);

我的错误信息。 <--- 对路径“C:\Program Files (x86)\SiteExtensions\temp\abc.xlsx”的访问被拒绝。

有人可以告诉我如何将这个文件保存在某个地方吗? 我在我的结构中创建了一个名为“temp”的文件夹作为一种可能的解决方案,但我似乎无法访问它。

任何帮助将不胜感激!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM