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

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

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

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

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

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

  private static CloudBlobClient _blobClient = CloudStorageAccount.Parse("connectionstring").CreateCloudBlobClient(); public IEnumerable 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()) { yield return blob; } } while (continuationToken != null); } 

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

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

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

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

 private static CloudBlobClient _blobClient = CloudStorageAccount.Parse("connectionstring").CreateCloudBlobClient(); public async Task> 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()) { yield return blob; } } while (continuationToken != null); }