C# retrieving a list of blobs from Azure

时光总嘲笑我的痴心妄想 提交于 2019-12-01 00:42:44

Try this pattern. Can be handy when browsing big storages. I found it much more GC and memory footprint friendly

var blobAccount = "<account>";
var apiKey = "<api-key>";
var containerName = "<container>";
var storageCredentials = new StorageCredentials(blobAccount, apiKey);

var account = new CloudStorageAccount(storageCredentials, true);
var blobClient = account.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(containerName);
var blobLimit = 500

if (container == null) { return; }

var blobContinuationToken = new BlobContinuationToken();

using (var fs = new FileStream("Output.csv", FileMode.Create))
{
    var sw = new StreamWriter(fs);
    sw.WriteLine("Type,Name,Length");

    BlobContinuationToken continuationToken = null;
    do
    {   
        var blobList = container.ListBlobsSegmented("",
                                   true,
                                   BlobListingDetails.Metadata,
                                   blobLimit,
                                   continuationToken,
                                   new BlobRequestOptions
                                   {
                                       LocationMode = LocationMode.PrimaryOnly
                                   },
                                   null);

        continuationToken = blobList.ContinuationToken;

        // I was looking only for BlockBlobs
        foreach (var item in blobList.Results.OfType<CloudBlockBlob>())
        {
            sw.WriteLine($"block,\"{item.Name}\",{item.Properties.Length}");
        }

    } while (continuationToken != null);
}

Use the UseFlatBlobListing parameter like this:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse("");
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("ctr");

var blobList = container.ListBlobs(useFlatBlobListing: true)
foreach(var blob in blobList)
{
    logger.Info($"Blob Name: {blob.Uri}");
}

This will give you all blobs in a flattened way.

See https://docs.microsoft.com/en-us/dotnet/api/microsoft.windowsazure.storage.blob.cloudblobcontainer.listblobs?view=azure-dotnet

If you also include the prefix parameter you can filter results based on the folder structure. To get everything in may 2017 you can do

var blobList = container.ListBlobs(prefix: "2017/5/", useFlatBlobListing: true)

This might help reducing the list of blobs depending on your retention.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!