[英]Get report of file sizes from Azure Blob Storage
我需要訪問 blob 存儲並獲取文件大小和文件路徑報告。 我需要下載該報告。 我在 blob 存儲中的文件存儲結構是有四個命名文件夾。 然后對於每個用戶都有一個自動生成的 GUID 文件夾。 文件夾里面有文件。 我需要獲取文件大小和文件路徑(或文件名)的列表。 最好的方法是什么?
您可以使用GetBlobsAsync獲取帶有prefix
的文件,並使用ContentLength
(以字節為單位的大小)獲取文件大小。
例如,將前綴設置為“directoryA”時,您將獲取 directoryA 中的所有文件。 存儲 URL 看起來像https://myaccount.blob.core.windows.net/ContainerName/directoryA/filename
。
private static async Task ListBlobsFlatListing(BlobContainerClient blobContainerClient,
int? segmentSize, string yourPrefix)
{
try
{
// Call the listing operation and return pages of the specified size.
var resultSegment = blobContainerClient.GetBlobsAsync(prefix: yourPrefix)
.AsPages(default, segmentSize);
// Enumerate the blobs returned for each page.
await foreach (Azure.Page<BlobItem> blobPage in resultSegment)
{
foreach (BlobItem blobItem in blobPage.Values)
{
Console.WriteLine("Blob name: {0}", blobItem.Name);
Console.WriteLine("Blob content length: {0}", blobItem.Properties.ContentLength);
}
Console.WriteLine();
}
}
catch (RequestFailedException e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
throw;
}
}
您可以使用此方法將獲得的 ContentLength 轉換為 MB:
static double ConvertBytesToMegabytes(long bytes)
{
return (bytes / 1024f) / 1024f;
}
以上是基於存儲v12。 如果使用舊版本(v11),您可以參考這里。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.