Utilize HOME and TEMP/TMP Folders in Azure App Services

Kontext Kontext event 2022-08-30 visibility 5,228
more_vert
Utilize HOME and TEMP/TMP Folders in Azure App Services

For ASP.NET Core applications hosing on Azure App services, the hosting environment are slightly different from traditional data center application hosting. For Windows based application, Windows Sandbox are used to segregate apps from each other. Sometimes it might be useful to cache some local files into these sandboxes.

This article shows you how to access temporary or home folder within your applications. 

Access application file system via Console blade

The easy way to access your application sandbox is to use Console blade on Azure portal.

To do that, please first navigate to your Azure App service resource blade. And then click Console from Development Tools section. The console looks like the following screenshot:

2022083070230-image.png

By default, it will navigate to D:\home\site\wwwroot folder. 

_ _____ _ ___ ___
/_\ |_ / | | | _ \ __|
_ ___/ _ \__/ /| |_| | / _|___ _ _
(___ /_/ \_\/___|\___/|_|_\___| _____)
(_______ _ _) _ ______ _)_ _
(______________ _ ) (___ _ _) Manage your web app environment by running common commands ('mkdir', 'cd' to change directories, etc.) This is a sandbox environment, so any commands that require elevated privileges will not work.

D:\home\site\wwwroot>echo %HOME%
D:\home D:\home\site\wwwroot>echo %TEMP%
D:\local\Temp D:\home\site\wwwroot>echo %TMP%
D:\local\Temp

We can use echo command to print out these three variables:

  • HOME: home folder for the application.
  • TEMP or TMP: temporary folder for the application.
warning We cannot use Console to run commands that need elevated permissions since it is a sandbox. However, we do have permissions to create folders or files in these folders. 

Access HOME folder

In ASP.NET Core, we can access HOME folder via built-in function:

var folder = Environment.GetEnvironmentVariable("HOME");

The value will be NULL if the variable doesn't exist.

For applications on Windows the value is D:\home while it is /home for Linux or Linux container based applications.

We can create sub folders or files in this HOME folder.

var subFolder = Path.Combine(folder, "sub1");
if (!Directory.Exists(subFolder))
{
	Directory.CreateDirectory(folder);
}

In the above example, it creates a sub folder named sub1 if it doesn't exist in the home folder. Files in this home folder are persisted and will use your ASP (application service plan) space. For Basic tier, the total available space is 10GB for all apps hosted in the plan as the following screenshot shows:

2022083072036-image.png

Access temporary folder

For accessing temporary folder, we can use the following code:

var tempFolder = System.IO.Path.GetTempPath();

By default, the path on Windows sandboxes is D:\local\Temp.

Durability of content in temporary folder

Once application restart, the files in the temporary folder will be gone. Thus please ensure your application can work even if the creates temporary files are deleted. 

Other folders

There are also some other temporary folders that might be useful:

  • %APPDATA% maps to %SYSTEMDRIVE%\local\AppData.
  • %ProgramData% maps to %SYSTEMDRIVE%\local\ProgramData.
  • %SYSTEMDRIVE%\local\DynamicCache for Dynamic Cache feature.

Usage of these folders

One of the common usage for me is to cache frequent used small files locally after downloading from Azure Blob Storage. However please do consider the impacts and limitations on file spaces before adopting similar approach.

If your purpose is to cache files at edge, i.e. accelerating content delivery to your customers globally, you can consider Azure CDN product. 

References

Understanding the Azure App Service file system · projectkudu/kudu Wiki

File structure on azure · projectkudu/kudu Wiki

More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts