azure - 列出 Azure blob 存储中不同目录的 blob

标签 azure blob azure-blob-storage azure-data-lake-gen2 blobstorage

我们有以下文件夹结构,我们在 Azure 数据湖中存储数据。

enter image description here

我想列出数据目录中存在的所有 blob。 IE。 abc.txt,xyz.txt,lmn.txt,abc1.txt,xyz1.txt,lmn1.txt,abc2.txt,xyz2.txt,lmn2.txt,gh.txt,kim.txt,tae.txt,.. ..

如何获取仅位于数据目录中的所有 blob?

Azure 中的文件夹结构图像: enter image description here

我想从每个年份和月份文件夹中获取所有用户的数据文件夹中存在的所有 blob。

代码:

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Collections.Generic;

namespace DataLake
{
   class Program
    { 
      static void Main(string[] args)
      {
          CloudStorageAccount storageAccount = CloudStorageAccount.Parse("connection string");
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer container = blobClient.GetContainerReference("dev");


        List<string> blobnames = new List<string>();

        var allblobs = container.ListBlobs(useFlatBlobListing: true);
        foreach (var b in allblobs)
        {
            //Console.WriteLine("\t" + b.Uri);
            string name = ((CloudBlockBlob)b).Name;
      //      Console.WriteLine(name);
            string[] names = name.Split('/');
            blobnames.Add(names[names.Length - 1]);

        }
        foreach (var data in blobnames)
        {
            Console.WriteLine(data);
        }


    }
}

}

输出: enter image description here

最佳答案

尝试使用useFlatBlobListing

我在我的系统中测试了尝试使用此代码

  using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blob;
using System;

using System.Collections.Generic;
using System.IO;

namespace BlobList
{
    class Program
    {
        static void Main(string[] args)
        {




            CloudStorageAccount storageAccount = CloudStorageAccount.Parse("Connection String");
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference("test");


            List<string> blobnames = new List<string>();
            //var allblobs = container.ListBlobs(prefix: "test1/test2", useFlatBlobListing: true);
            var allblobs = container.ListBlobs(useFlatBlobListing: true);
            foreach (var b in allblobs)
            {
                //Console.WriteLine("\t" + b.Uri);
                string name = ((CloudBlockBlob)b).Name;
                Console.WriteLine(name);
                string[] names = name.Split('/');
                blobnames.Add(names[names.Length - 1]);




            }
            foreach (var data in blobnames)
            {
                Console.WriteLine(data);
            }


        }


    }
}

我尝试创建与您相同的文件夹结构

enter image description here

输出

enter image description here

关于azure - 列出 Azure blob 存储中不同目录的 blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69879544/

相关文章:

c# - 使用 C# 读取附加到 HTTP 扩展的 BLOB

azure - java.lang.IllegalArgumentException : Cannot create Shared Access Signature unless the Account Key credentials are used by the ServiceClient

php - 将 xml 字符串逐字转储到 mysql 数据库

android - 开发移动应用程序时将文件上传到 CDN 的最佳实践?

python - 如何通过 python 将 JSON 数据附加到存储在 Azure Blob 存储中的现有 JSON 文件?

azure - 无法使用 Azure Face API - 禁止错误

Azure Web 应用程序与 Azure 移动应用程序

c# - Azure函数: binding to DocumentClient versus static instance - what's recommended?

java - 在 Microsoft Azure Web 应用程序服务(而非 VM)上将 Websockets 与 Java Tomcat 结合使用

mysql - 将图像存储到数据库 blob;从数据库检索到 Picturebox