【问题标题】:How to get or calculate size of Azure File/Share or Service如何获取或计算 Azure 文件/共享或服务的大小
【发布时间】:2017-12-06 04:41:22
【问题描述】:

我目前正在构建一个托管在 Azure 中的多租户 Web 应用程序,它将使用 Azure 文件服务来存储每个客户端的数据 - 每个客户端都将使用一个不同的文件共享来提供隔离。

我的问题是 - 我如何才能发现特定文件共享中所有文件的大小? (用于计费目的)。

我有 PowerShell 脚本等来计算 Blob 存储的大小,但没有用于文件存储。有谁知道这是否可能以及如何完成,最好是从我的 C# 应用程序中?

【问题讨论】:

  • 您解决了这个问题吗,需要进一步的帮助吗?

标签: c# azure-storage-files


【解决方案1】:

我有 PowerShell 脚本等来计算 Blob 存储的大小,但没有用于文件存储。有谁知道这是否可能以及如何完成,最好是从我的 C# 应用程序中?

您可以利用Microsoft Azure Configuration Manager Library for .NET 并检索特定文件共享的粗略使用情况,如下所示:

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);

更多详情可以参考Develop with File storage

结果:

Current file share usage: 1 GB, maximum size: 5120 GB

您可以利用Microsoft Azure Storage Explorer 来检查您的文件共享的使用情况和配额,如下所示:

此外,为了检索特定文件共享的确切使用情况,我假设您需要迭代文件共享下的文件和目录并累积文件字节大小。为了达到这个目的我写了一个代码sn-p,你可以参考一下:

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);
}

用法:

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));

【讨论】:

  • 我已经更新了我的答案并为您添加了一些教程,您可以参考它们,有任何问题,请随时告诉我。
  • 您能解释一下 Do-While 循环的目的吗? continuationToken 将始终为空,所以 do-while 循环只会运行一次?
  • 如何获得最大尺寸?我检查了 share.properties.quota 但它是空的..
猜你喜欢
  • 2020-08-19
  • 1970-01-01
  • 2016-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多