我想列出容器中的 blob。对于桌面应用,Azure 存储 SDK 提供以下方法:
CloudBlobContainer container = blobClient.GetContainerReference("myBlob");
container.ListBlobs();
但是,使用WinRT 库时,该方法不存在(可能是由于其阻塞性质)。我应该改用什么?
您需要使用ListBlobsSegmentedAsync
blob 容器上的函数来获取 blob 列表。例如,请参见下面的代码:
CloudStorageAccount account = new CloudStorageAccount(new StorageCredentials("accountname", "accountkey"), true);
var client = account.CreateCloudBlobClient();
var container = client.GetContainerReference("containername");
BlobContinuationToken continuationToken = null;
string prefix = null;
bool useFlatBlobListing = true;
BlobListingDetails blobListingDetails = BlobListingDetails.All;
int maxBlobsPerRequest = 10;
List<IListBlobItem> blobs = new List<IListBlobItem>();
do
{
var listingResult = await container.ListBlobsSegmentedAsync(prefix, useFlatBlobListing, blobListingDetails, maxBlobsPerRequest, continuationToken, null, null);
continuationToken = listingResult.ContinuationToken;
blobs.AddRange(listingResult.Results);
}
while (continuationToken != null);
修改代码以支持 Azure 2.2
CloudStorageAccount account = new CloudStorageAccount(new StorageCredentials("accountname", "accountkey"), true);
var client = account.CreateCloudBlobClient();
var container = client.GetContainerReference("containername");
BlobContinuationToken continuationToken = null;
string prefix = null;
bool useFlatBlobListing = true;
BlobListingDetails blobListingDetails = BlobListingDetails.All;
int maxBlobsPerRequest = 10;
List<IListBlobItem> blobs = new List<IListBlobItem>();
do
{
var listingResult = await container.ListBlobsSegmentedAsync(prefix, useFlatBlobListing, blobListingDetails, maxBlobsPerRequest, continuationToken, null, null);
continuationToken = **listingResult.Result.ContinuationToken;**
blobs.AddRange(**listingResult.Result.Results**);
}
while (continuationToken != null);