如何使用ListBlobsSegmentedAsync从Azure BLOB中的目录获取所有文件

如何使用ListBlobsSegmentedAsync从Azure BLOB中的目录获取所有文件

问题描述:

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

While trying to access all files of the Azure blob folder, getting sample code for container.ListBlobs(); however it looks like an old one.

旧代码:container.ListBlobs();

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

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

I am trying to use the below code :

container.ListBlobsSegmentedAsync(continuationToken);

文件夹就像:

Container/F1/file.json
Container/F1/F2/file.json
Container/F2/file.json

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

Looking for the updated version to get all files from an Azure folder. Any sample code would help, thanks!

方法CloudBlobClient.ListBlobsSegmentedAsync用于返回结果段,该结果段包含容器中的blob项的集合.

The method CloudBlobClient.ListBlobsSegmentedAsync is used to return a result segment containing a collection of blob items in the container.

要列出所有斑点,我们可以使用ListBlobs方法

To list all blobs, we can use ListBlobs method,

以下是一个演示供您参考:

Here is a demo for your reference:

    public static List<V> ListAllBlobs<T, V>(Expression<Func<T, V>> expression, string containerName,string prefix)
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse("YourConnectionString;");

        CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();

        CloudBlobContainer container = cloudBlobClient.GetContainerReference(containerName);
        container.CreateIfNotExists();

        var list = container.ListBlobs(prefix: prefix,useFlatBlobListing: true);

        List<V> data = list.OfType<T>().Select(expression.Compile()).ToList();
        return data;
    }

用法和屏幕截图:

在一个文件夹下列出所有blob的名称:

List all blobs' names under one folder:

在一个文件夹下列出所有Blob的URL:

List all blobs' URLs under one folder: