Justin Caldicott
Justin Caldicott

Reputation: 788

Get Azure App Service local disk usage programmatically

Azure app services have a limit on the amount of local (temporary) storage used. But, as I understand it, the limit is across the whole app service plan.

I want to maximise usage of the local storage, without hitting the limit. But split across multiple app services in the same app service plan, this gets tricky.

Is there a REST API that returns the app service plan local disk usage? Or even better, some way to get this from the environment?

Usage can be viewed on the Environment tab on Kudu, but I want to do this in code within a running web app.

Upvotes: 1

Views: 1407

Answers (2)

Joaquín Vano
Joaquín Vano

Reputation: 464

You can call the GetDiskFreeSpace API to find out disk free space:

public static decimal GetAvailableSpaceInBytes(string path)
{
    uint sectorsPerCluster;
    uint bytesPerSector;
    uint numberOfFreeClusters;
    uint totalNumberOfClusters;

    GetDiskFreeSpace(path, out sectorsPerCluster, out bytesPerSector, out numberOfFreeClusters, out totalNumberOfClusters);
    long bytes = (long)numberOfFreeClusters * sectorsPerCluster * bytesPerSector;
    
    return bytes;
}

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern bool GetDiskFreeSpace(
    string lpRootPathName,
    out uint lpSectorsPerCluster,
    out uint lpBytesPerSector,
    out uint lpNumberOfFreeClusters,
    out uint lpTotalNumberOfClusters);

Keep in mind that App Service will set quotas to the folders used by the Web App and querying the GetDiskFreeSpace will return the free space in the context of the quotas of the Web App.

It is only interesting to know this for the writable folders for a given Azure App Service Web App.

Upvotes: 0

Jason Pan
Jason Pan

Reputation: 21916

You can list usages by restapi. There are something wrong when you test in offical document, you need use tools like Postman to test it.

e.g https://management.azure.com/subscriptions/{subscriptions}/resourceGroups/{resourceGroups}/providers/Microsoft.Web/sites/{webappname}/usages?api-version=2019-08-01&$filter=name.value eq 'FileSystemStorage'

enter image description here

Upvotes: 1

Related Questions