问题
I am currently building a multi tenant Web App to be hosted in Azure which will use Azure File Services to store each clients data - a distinct File Share will be used for each client to provide isolation.
My question is - how can I discover the size of all files within a particular file share? (For billing purposes).
I have PowerShell scripts etc to calculate the size of Blob storage, but nothing for File Storage. Does anyone know if this is possible and how it can be done, preferably from my C# application?
回答1:
I have PowerShell scripts etc to calculate the size of Blob storage, but nothing for File Storage. Does anyone know if this is possible and how it can be done, preferably from my C# application?
You could leverage Microsoft Azure Configuration Manager Library for .NET and retrieve the rough usage for a specific file share as follows:
CloudFileShare share = fileClient.GetShareReference("{your-share-name}");
ShareStats stats = share.GetStats();
Console.WriteLine("Current file share usage: {0} GB, maximum size: {1} GB", stats.Usage.ToString(), share.Properties.Quota);
For more details, you could refer to Develop with File storage.
Result:
Current file share usage: 1 GB, maximum size: 5120 GB
You could leverage Microsoft Azure Storage Explorer to check the Usage and Quota of your file share as follows:
Moreover, for retrieving the exact usage of a specific file share, I assumed that you need to iterate the file and directory under the file share and accumulate the files byte size. I wrote a code snippet for achieving this purpose, you could refer to it:
static void FileShareByteCount(CloudFileDirectory dir,ref long bytesCount)
{
FileContinuationToken continuationToken = null;
FileResultSegment resultSegment = null;
do
{
resultSegment = dir.ListFilesAndDirectoriesSegmented(100, continuationToken, null, null);
if (resultSegment.Results.Count() > 0)
{
foreach (var item in resultSegment.Results)
{
if (item.GetType() == typeof(CloudFileDirectory))
{
var CloudFileDirectory = item as CloudFileDirectory;
Console.WriteLine($" List sub CloudFileDirectory with name:[{CloudFileDirectory.Name}]");
FileShareByteCount(CloudFileDirectory,ref bytesCount);
}
else if (item.GetType() == typeof(CloudFile))
{
var CloudFile = item as CloudFile;
Console.WriteLine($"file name:[{CloudFile.Name}],size:{CloudFile.Properties.Length}B");
bytesCount += CloudFile.Properties.Length;
}
}
}
} while (continuationToken != null);
}
Usage:
CloudFileShare share = fileClient.GetShareReference("logs");
CloudFileDirectory rootDir = share.GetRootDirectoryReference();
long bytesCount = 0;
FileShareByteCount(rootDir, ref bytesCount);
Console.WriteLine("Current file share usage: {0:f3} MB", bytesCount / (1024.0 * 1024.0));
来源:https://stackoverflow.com/questions/44876642/how-to-get-or-calculate-size-of-azure-file-share-or-service