如何使用 ListBlobsSegmentedAsync 从 Azure BLOB

在尝试访问 Azure blob 文件夹的所有文件时,获取示例代码container.ListBlobs();但它看起来像旧的。


旧代码: container.ListBlobs();


新代码尝试: container.ListBlobsSegmentedAsync(continuationToken);


我正在尝试使用以下代码:


container.ListBlobsSegmentedAsync(continuationToken);

文件夹是这样的:


Container/F1/file.json

Container/F1/F2/file.json

Container/F2/file.json

寻找更新版本以从 Azure 文件夹中获取所有文件。任何示例代码都会有所帮助,谢谢!


翻阅古今
浏览 280回答 3
3回答

繁星coding

这是答案的代码:private async Task<List<IListBlobItem>> ListBlobsAsync(CloudBlobContainer container){&nbsp; &nbsp; BlobContinuationToken continuationToken = null;&nbsp; &nbsp; List<IListBlobItem> results = new List<IListBlobItem>();&nbsp; &nbsp; do&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;bool useFlatBlobListing = true;&nbsp; &nbsp; &nbsp; &nbsp;BlobListingDetails blobListingDetails = BlobListingDetails.None;&nbsp; &nbsp; &nbsp; &nbsp;int maxBlobsPerRequest = 500;&nbsp; &nbsp; &nbsp; &nbsp;var response = await container.ListBlobsSegmentedAsync(BOAppSettings.ConfigServiceEnvironment, useFlatBlobListing, blobListingDetails, maxBlobsPerRequest, continuationToken, null, null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continuationToken = response.ContinuationToken;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; results.AddRange(response.Results);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;while (continuationToken != null);&nbsp; &nbsp; &nbsp;return results;}然后你可以返回如下值:IEnumerable<IListBlobItem> listBlobs = await this.ListBlobsAsync(container);foreach(CloudBlockBlob cloudBlockBlob in listBlobs)&nbsp; {&nbsp; &nbsp; &nbsp;BOBlobFilesViewModel boBlobFilesViewModel = new BOBlobFilesViewModel&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CacheKey = cloudBlockBlob.Name,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name = cloudBlockBlob.Name&nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; listBOBlobFilesViewModel.Add(boBlobFilesViewModel);&nbsp; &nbsp;}//return listBOBlobFilesViewModel;

青春有我

C#代码:&nbsp; &nbsp;//connection string&nbsp; &nbsp; string storageAccount_connectionString = "**NOTE: CONNECTION STRING**";&nbsp; &nbsp; // Retrieve storage account from connection string.&nbsp; &nbsp; CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageAccount_connectionString);&nbsp; &nbsp; // Create the blob client.&nbsp; &nbsp; CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();&nbsp; &nbsp; // Retrieve reference to a previously created container.&nbsp; &nbsp; CloudBlobContainer container = blobClient.GetContainerReference("**NOTE:NAME OF CONTAINER**");&nbsp; &nbsp; //The specified container does not exist&nbsp; &nbsp; try&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //root directory&nbsp; &nbsp; &nbsp; &nbsp; CloudBlobDirectory dira = container.GetDirectoryReference(string.Empty);&nbsp; &nbsp; &nbsp; &nbsp; //true for all sub directories else false&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; var rootDirFolders = dira.ListBlobsSegmentedAsync(true, BlobListingDetails.Metadata, null, null, null, null).Result;&nbsp; &nbsp; &nbsp; &nbsp; foreach (var blob in rootDirFolders.Results)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.WriteLine("Blob", blob);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; catch (Exception e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; Block of code to handle errors&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Error", e);&nbsp; &nbsp; }

守着一只汪

该方法CloudBlobClient.ListBlobsSegmentedAsync用于返回包含容器中 blob 项集合的结果段。要列出所有 blob,我们可以使用ListBlobs方法,这是一个演示供您参考:&nbsp; &nbsp; public static List<V> ListAllBlobs<T, V>(Expression<Func<T, V>> expression, string containerName,string prefix)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; CloudStorageAccount storageAccount = CloudStorageAccount.Parse("YourConnectionString;");&nbsp; &nbsp; &nbsp; &nbsp; CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();&nbsp; &nbsp; &nbsp; &nbsp; CloudBlobContainer container = cloudBlobClient.GetContainerReference(containerName);&nbsp; &nbsp; &nbsp; &nbsp; container.CreateIfNotExists();&nbsp; &nbsp; &nbsp; &nbsp; var list = container.ListBlobs(prefix: prefix,useFlatBlobListing: true);&nbsp; &nbsp; &nbsp; &nbsp; List<V> data = list.OfType<T>().Select(expression.Compile()).ToList();&nbsp; &nbsp; &nbsp; &nbsp; return data;&nbsp; &nbsp; }用法和截图:列出一个文件夹下的所有 blob 名称:列出一个文件夹下所有 blob 的 URL:
打开App,查看更多内容
随时随地看视频慕课网APP