简体   繁体   English

如何使用 Azure.Storage.Blobs BlobClient 检索 blob 目录路径中的 blob?

[英]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.

相关问题 使用 Azure.Storage.Blobs 枚举大型 Azure BLOB 容器 - Enumerating large Azure BLOB containers using Azure.Storage.Blobs 使用新的 Azure.Storage.Blobs 时如何计算存储帐户中 Blob 存储容器的总大小 - How to calculate the total size of Blob storage containers in an storage account when using the new Azure.Storage.Blobs 如何使用 Azure.Storage.Blobs 程序集对 Azure blob 存储操作设置重试策略? - How do I set a retry policy on an Azure blob storage operation using the Azure.Storage.Blobs assembly? 如何使用 C# 中的 Azure.Storage.Blobs 以 ByteArray 格式从 Azure 存储 blob 中获取文件 - How to get file from Azure storage blob in a ByteArray format using Azure.Storage.Blobs in C# 如何使用新的 Azure.Storage.Blobs 命名空间仅获取 blob 中的文件夹名称 - How to get only folder name in blob using new Azure.Storage.Blobs namespace 如何模拟 Azure.Storage.Blobs 的 BlobContainerClient()? - How to Mock BlobContainerClient() of Azure.Storage.Blobs? 如何使用 Azure.Storage.Blobs 上传 stream - How to upload stream with Azure.Storage.Blobs 无法使用 Azure.Storage.Blobs NuGet 包将文件上传到 Azure Blob 存储 - Unable to upload a file to Azure Blob Storage using Azure.Storage.Blobs NuGet package 有没有办法在 Azure.Storage.Blobs 中获取 blob 的 ContentType? - Is there a way to get a blob's ContentType in Azure.Storage.Blobs? 想要使用新的 sdk - Azure.Storage.Blobs package 从 blob 内的文件夹下载和上传文件 - Want to download and upload file from a folder inside the blob using new sdk - Azure.Storage.Blobs package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM