如何获取Azure中容器中所有blob的列表?

时间:2015-08-17 18:53:14

标签: c# azure cloud

我拥有Azure中存储帐户的帐户名和帐户密钥。我需要获取该帐户中容器中所有blob的列表。 (" $ logs"容器)。

我可以使用CloudBlobClient类获取特定blob的信息,但无法弄清楚如何获取$ logs容器中所有blob的列表。

5 个答案:

答案 0 :(得分:12)

有一个如何在https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/#list-the-blobs-in-a-container列出容器中所有blob的示例:

// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));

// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("photos");

// Loop over items within the container and output the length and URI.
foreach (IListBlobItem item in container.ListBlobs(null, false))
{
    if (item.GetType() == typeof(CloudBlockBlob))
    {
        CloudBlockBlob blob = (CloudBlockBlob)item;
        Console.WriteLine("Block blob of length {0}: {1}", blob.Properties.Length, blob.Uri);
    }
    else if (item.GetType() == typeof(CloudPageBlob))
    {
        CloudPageBlob pageBlob = (CloudPageBlob)item;
        Console.WriteLine("Page blob of length {0}: {1}", pageBlob.Properties.Length, pageBlob.Uri);
    }
    else if (item.GetType() == typeof(CloudBlobDirectory))
    {
        CloudBlobDirectory directory = (CloudBlobDirectory)item;
        Console.WriteLine("Directory: {0}", directory.Uri);
    }
}

答案 1 :(得分:4)

由于您的容器名称是$ logs,所以我认为您的blob类型是追加blob。这是获取所有blob并返回IEnumerable的方法:

    private static CloudBlobClient _blobClient = CloudStorageAccount.Parse("connectionstring").CreateCloudBlobClient();
    public IEnumerable<CloudAppendBlob> GetBlobs()
    {
        var container = _blobClient.GetContainerReference("$logs");
        BlobContinuationToken continuationToken = null;

        do
        {
            var response = container.ListBlobsSegmented(string.Empty, true, BlobListingDetails.None, new int?(), continuationToken, null, null);
            continuationToken = response.ContinuationToken;
            foreach (var blob in response.Results.OfType<CloudAppendBlob>())
            {
                yield return blob;
            }
        } while (continuationToken != null);
    }

该方法可以是异步的,只需使用ListBlobsSegmentedAsync即可。您需要注意的一件事是参数useFlatBlobListing需要为true,这意味着ListBlobs将返回一个平面文件列表而不是分层列表。

答案 2 :(得分:4)

使用新软件包Azure.Storage.Blobs

BlobServiceClient blobServiceClient = new BlobServiceClient("YourStorageConnectionString");
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("YourContainerName");
var blobs = containerClient.GetBlobs();

foreach (var item in blobs){
    Console.WriteLine(item.Name);
}

答案 3 :(得分:3)

使用ListBlobsSegmentedAsync返回总结果集的一部分和一个延续令牌。

REF:https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-dotnet?tabs=windows

答案 4 :(得分:1)

这是WindowsAzure.Storage v9.0的更新API调用:

private static CloudBlobClient _blobClient = CloudStorageAccount.Parse("connectionstring").CreateCloudBlobClient();

public async Task<IEnumerable<CloudAppendBlob>> GetBlobs()
{
    var container = _blobClient.GetContainerReference("$logs");
    BlobContinuationToken continuationToken = null;

    //Use maxResultsPerQuery to limit the number of results per query as desired. `null` will have the query return the entire contents of the blob container
    int? maxResultsPerQuery = null;

    do
    {
        var response = await container.ListBlobsSegmentedAsync(string.Empty, true, BlobListingDetails.None, maxResultsPerQuery, continuationToken, null, null);
        continuationToken = response.ContinuationToken;
        foreach (var blob in response.Results.OfType<CloudAppendBlob>())
        {
            yield return blob;
        }
    } while (continuationToken != null);
}