簡體   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