如何使用ListBlobsSegmentedAsync从Azure BLOB中的目录获取所有文件 [英] How to get all files from a directory in Azure BLOB using ListBlobsSegmentedAsync

查看:190
本文介绍了如何使用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:

这篇关于如何使用ListBlobsSegmentedAsync从Azure BLOB中的目录获取所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆