我没有在网上看到任何关于如何获取位于某个目录中的所有 blob 的示例BlobContainerClient
.
以前,我使用的是Microsoft.Azure.Storage
包,但这些已被弃用。我扫描目录中所有 blob 的旧代码是:
public async Task<void> ListAllBlobs(string path)
{
var myContainer = await GetCloudBlobClientAsync();
var directory = myContainer.GetDirectoryReference(path);
var blobs = await directory.ListBlobsSegmentedAsync(true, BlobListingDetails.None,
blobSettings.MaxResult, null, null, null);
var results = blobs.Results;
foreach(CloudBlockBlob b in results)
{
// non-relevant code
}
}
private async Task<CloudBlobContainer> GetCloudBlobClientAsync()
{
var storageAccount = CloudStorageAccount.Parse(azureBlobStorageConnectionString);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(blobStorageSettings.ContainerName);
if (!await container.ExistsAsync())
{
await container.CreateAsync();
}
return container;
}
本质上,我将上面的代码从Microsoft.Azure.Storage
到Azure.Storage.Blobs
.
如果我要重新创建ListAllBlobs(string path)
使用函数Azure.Storage.Blobs
,我对如何设置容器然后根据传入的路径访问内部容器 - 然后循环访问该容器中存在的 blob 感到困惑。有人可以帮忙吗?
这是我到目前为止所拥有的:
public async Task<void> ListAllBlobs(string path)
{
var myContainer = await GetCloudBlobClientAsync();
var directory = myContainer.GetBlobClient(path);
// This doesn't work because I can't do 'GetBlobs' on the Client, only on the container.
foreach(BlobItem blob in directory.GetBlobs(Blobtraits.None, BlobStates.None, string.Empty))
{
// more non-relevant code
}
}
澄清一下,在上面的代码中,它不喜欢我打电话的方式GetBlobs
在客户端上,而不是在容器上,但我无法传递容器的路径。