[英]How to retrieve blobs within a blob directory path using the Azure.Storage.Blobs BlobClient?
I'm not seeing any examples online on how to get all the blobs located inside a certain directory within a BlobContainerClient
.我没有在网上看到任何关于如何获取位于
BlobContainerClient
中某个目录内的所有 blob 的示例。
Previously, I was using the Microsoft.Azure.Storage
packages, but those have since been deprecated.以前,我使用的是
Microsoft.Azure.Storage
包,但这些包已被弃用。 My old code that was scanning a directory for all blobs was:我正在扫描所有 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;
}
Essentially, I'm moving the above code from Microsoft.Azure.Storage
over to Azure.Storage.Blobs
.本质上,我将上述代码从
Microsoft.Azure.Storage
移至Azure.Storage.Blobs
。
If I were to recreate the ListAllBlobs(string path)
function to use Azure.Storage.Blobs
, I'm confused on how to setup a container and then access an inner container based on a path that's passed in - then cycle through the blobs that exist within that container.如果我要重新创建
ListAllBlobs(string path)
function 以使用Azure.Storage.Blobs
,我对如何设置容器然后根据传入的路径访问内部容器感到困惑 - 然后循环通过 blobs存在于该容器中。 Can anyone help?任何人都可以帮忙吗?
Here's what I have so far:这是我到目前为止所拥有的:
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
}
}
To clarify, in the above code, it doesn't like that I'm calling GetBlobs
on a Client, rather than on the Container, but I can't pass in a path to the container.澄清一下,在上面的代码中,我不喜欢我在客户端而不是容器上调用
GetBlobs
,但我不能传入容器的路径。
Try this...尝试这个...
static async Task GetBlobs()
{
string connectionString = "<connection_string>";
string containerName = "<container_name>";
var blobs = blobContainerClient.GetBlobs(Azure.Storage.Blobs.Models.BlobTraits.All, Azure.Storage.Blobs.Models.BlobStates.All,
"YourPrefix");
foreach (var blob in blobs)
{
Console.WriteLine(blob.Name);
}
}
... that worked for me. ...这对我有用。
( EDIT : I misread your question, so corrected my answer ) (编辑:我误读了你的问题,所以更正了我的答案)
You were almost there.你快到了。 You would still use
BlobContainerClient
and call GetBlobsAsync
method on that.您仍将使用
BlobContainerClient
并在其上调用GetBlobsAsync
方法。 What you missed is that you will need to set the prefix
parameter's value as the path
.您错过的是您需要将
prefix
参数的值设置为path
。
So your code would be something like:所以你的代码会是这样的:
var myContainer = await GetCloudBlobClientAsync();
var blobsListingResult = await myContainer.GetBlobsAsync(prefix=path);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.