6

I have an Azure function and I am trying to save a .xlsx file to a temporary folder. My code works locally, but when I publish to Azure I get an error message.

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.

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.

Any help would be greatly appreciated!!

silent
  • 14,494
  • 4
  • 46
  • 86
solarissf
  • 1,199
  • 2
  • 23
  • 58

4 Answers4

7

Please do not use something like Environment.CurrentDirectory in Azure Functions (or actually, just anywhere) to get a temp folder. Instead use the .NET-native method to do so:

Path.GetTempPath()

So ideally use something like this:

string pathTradeRecFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".xlsx");
silent
  • 14,494
  • 4
  • 46
  • 86
  • thank you silent, I actually just came across this and it seems to be working. how do I visually look at this folder in something like Kudu? meaning any idea where it physically puts it? – solarissf Jan 14 '21 at 15:02
  • just log the generated path? But not sure if you can really see it - at least not for long. that's the whole point of it being temporary ;) and latest, when the function instance gets recycled it will be cleared – silent Jan 14 '21 at 15:36
  • if you want to keep the files for debugging etc, you need to write them out to Blob storage etc. – silent Jan 14 '21 at 15:36
  • If that's the case. Do you think I should worry about deleting the file or it will automatically be done? – solarissf Jan 14 '21 at 15:37
  • it will be done automatically. but if you generate a lot of those in a short time, potentially on the same Function instance, it is probably a good idea to quickly delete it at the end of your function run – silent Jan 14 '21 at 15:38
1

My apologies, but I did not get your intention to save file in Azure storage file system? However if azure function allows to save file locally then you should use Directory.GetCurrentDirectory(); which resolves to D:\home\site\wwwroot path.

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; you can use MemoryStream as shown in below code to upload the content at 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);  
} 
user1672994
  • 10,509
  • 1
  • 19
  • 32
0

Environment variables as seen by an Azure Function aren't the same as the overall OS Environment variables. 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. 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-dotnet-class-library?tabs=v2%2Ccmd#environment-variables.

  • Don't simply link to answers. We have no control over the answer going away or continuing to be accurate (especially if the Microsoft Marketing department doesn't like the answer's implication about the product relative to their competition.) Put the answer details in the answer and link there for more info but the answer should still be whole and complete if the linked page goes away. – Rodger Jan 18 '22 at 20:12
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Rodger Jan 18 '22 at 20:13
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/30849622) – eglease Jan 21 '22 at 21:42
0

In case of Python runtime, base OS is going to be Linux so you can work it out something like below:

import tempfile

# Get the temporary file
# With suffix/prefix you can add suffix/prefix
# delete=False prevents this file from deletion as soon as you close it
temp_file = tempfile.NamedTemporaryFile(suffix=".tf.json", delete=False)

# File Name with full path
print(temp_file.name)

Source Documentation

kwick
  • 333
  • 3
  • 9