我正在处理Azure Blob存储实例中特定目录中包含的大量数据,我想获得某个目录中所有内容的大小。我知道如何获得整个容器的大小,但它是如何只是指定目录本身以从中提取数据。
我目前的代码如下:
private static long GetSizeOfBlob(CloudStorageAccount storageAccount, string nameOfBlob)
{
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Finding the container
CloudBlobContainer container = blobClient.GetContainerReference(nameOfBlob);
// Iterating to get container size
long containerSize = 0;
foreach (var listBlobItem in container.ListBlobs(null, true))
{
var blobItem = listBlobItem as CloudBlockBlob;
containerSize += blobItem.Properties.Length;
}
return containerSize;
}
如果我将blob容器指定为类似“democontainer / testdirectory”的东西,我会得到一个400错误(我想因为它是一个目录,使用反斜杠会让我只导航到我想要迭代的目录通过)。任何帮助将不胜感激。
答案 0 :(得分:0)
您需要先获得CloudBlobDirectory
,然后才能从那里开始工作。
例如:
var directories = new List<string>();
var folders = blobs.Where(b => b as CloudBlobDirectory != null);
foreach (var folder in folders)
{
directories.Add(folder.Uri);
}
答案 1 :(得分:0)
我已经解决了我自己的问题,但是留在这里为未来的azure blob存储资源管理器。
您实际上需要在调用container.ListBlobs(prefix: 'somedir', true)
时提供前缀,以便只访问所涉及的特定目录。此目录是您访问的容器名称之后的任何内容。
答案 2 :(得分:0)
private string GetBlobContainerSize(CloudBlobContainer contSrc)
{
var blobfiles = new List<string>();
long blobfilesize = 0;
var blobblocks = new List<string>();
long blobblocksize = 0;
foreach (var g in contSrc.ListBlobs())
{
if (g.GetType() == typeof(CloudBlobDirectory))
{
foreach (var file in ((CloudBlobDirectory)g).ListBlobs(true).Where(x => x as CloudBlockBlob != null))
{
blobfilesize += (file as CloudBlockBlob).Properties.Length;
blobfiles.Add(file.Uri.AbsoluteUri);
}
}
else if (g.GetType() == typeof(CloudBlockBlob))
{
blobblocksize += (g as CloudBlockBlob).Properties.Length;
blobblocks.Add(g.Uri.AbsoluteUri);
}
}
string res = string.Empty;
if (blobblocksize > 0) res += "size: " + FormatSize(blobblocksize) + "; blocks: " + blobblocks.Count();
if (!string.IsNullOrEmpty(res) && blobfilesize > 0) res += "; ";
if (blobfilesize > 0) res += "size: " + FormatSize(blobfilesize) + "; files: " + blobfiles.Count();
return res;
}
private string FormatSize(long len)
{
string[] sizes = { "B", "KB", "MB", "GB", "TB" };
int order = 0;
while (len >= 1024 && order < sizes.Length - 1)
{
order++;
len = len/1024;
}
return $"{len:0.00} {sizes[order]}".PadLeft (9, ' ');
}