36

When I try to save a file to the Temp storage in Azure Functions directory (D:\home\data\temp\response.pdf), I get the following error. Why can't I write to this directory?

mscorlib: Exception has been thrown by the target of an invocation. System: An exception occurred during a WebClient request. mscorlib: ***Could not find a part of the path 'D:\home\data\temp\response.pdf'.***
2017-09-19T07:05:24.353 Function completed (Failure, Id=3aa4b740-ba8a-465c-ad7c-75b38fa2a472, Duration=334ms)
2017-09-19T07:06:31  No new trace in the past 1 min(s).
4
  • The same code when I try on my local system, it works fine.
    – naag
    Sep 19, 2017 at 7:46
  • Please make sure that D:\home\data\temp is existing. We could use Kudu tool(youfunctionappName.scm.azurewebsites.net) to check that. Sep 19, 2017 at 8:01
  • D:\home\data\temp folder does exist. Created that from Console
    – naag
    Sep 20, 2017 at 1:26
  • 1
    You can also write to: D:\local\Temp, this Temp directory is already existing for me. Nov 10, 2017 at 13:13

4 Answers 4

44

I recommend using System.IO.Path.GetTempPath() as this will always give us a valid path for any given system.

Additionally, functions may execute multiple times simultaneously for a given instance, so it's best to ensure we have a unique path for each execution. Here's a simple example:

var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

Alternately, we can use System.IO.Path.GetTempFileName() which will additionally create the file before returning the full path and unique filename.

1
  • 1
    For those upvoting, don't forget to also upvote the question :)
    – defines
    Nov 9, 2020 at 18:14
6

I find a better choice. You can use System.IO.Path.GetTempFileName() this create a %userprofile%\Local\Temp\tmpE128.tmp file

1
  • 1
    I share a more secure method to get a temp filename System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetRandomFileName()) Mar 18, 2021 at 13:30
4

According to the exception, it seems that D:\home\data\temp\ is not existing in your function project. Please have a try to check it with Azure Kudu tool(https://yourwebsiteName.scm.azurewebsites.net). If the path is not existing, please have a try to add the temp folder and try again.

We could get more info about Azure WebApp from the Azure Web App sandbox. More detail info about file structure on azure,please refer to this document.

2
  • D:\home\data\temp folder does exist. Created that from Console.
    – naag
    Sep 20, 2017 at 1:25
  • I create a httptrigger function and test it on my side, it works correctly. My test code: File.WriteAllText(@"D:\home\data\temp\test.txt","tomtest"); Sep 20, 2017 at 2:35
0

GetTempPath returns a path to a directory in the local file system, which you can use for temp files created by the Azure Function. It's fast, but there's not much space available to your function.

The HOME environment variable points to a mapped network drive. Your Azure Function can also read/write there, but this drive is mapped to an Azure Blob Storage account, so it's much slower than the GetTempPath directory. There is much more available space in the HOME directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.