简体   繁体   English

如何获取 Azure 中子容器中的 blob 列表

[英]How to get a list of blobs in a sub container in Azure

I need to get all the files from a storage with url: "https://example.blob.core.windows.net/file/subfile1/subfile2" I only need to read data files( only.txt for example, so if I have.jpg or something else I should skip those) and only from subfile2, not the whole storage.我需要使用 url 从存储中获取所有文件:“https://example.blob.core.windows.net/file/subfile1/subfile2”我只需要读取数据文件(例如 only.txt,所以如果我有.jpg 或其他我应该跳过的内容)并且仅来自 subfile2,而不是整个存储。

With c# btw与 c# 顺便说一句

I tried in my system able to download the all txt files in container subfolder( test is container and test1 is folder in container ),Try with this code.我尝试在我的系统中下载容器子文件夹中的所有txt文件( test 是容器,test1 是容器中的文件夹),试试这个代码。

using Microsoft.Azure.Storage.Blob;
using System;
using System.Collections.Generic;
using System.IO;

namespace listtxtblobs
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse("Connection string");
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference("test//container name");

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




            }
            foreach(string name in blobnames)
            {
                Console.WriteLine(name);
                if (name.Contains(".txt"))
                {
                    string filePath = "local file path\\" + name;
                    CloudBlockBlob blockBlob = container.GetBlockBlobReference("test1\\"+ name);
                    blockBlob.DownloadToFile(filePath, FileMode.OpenOrCreate);
                    
                }
            }
        }
    }
}

Folder structure in azure storage azure存储中的文件夹结构

在此处输入图像描述

OUTPUT OUTPUT

在此处输入图像描述

Downloaded the all txt files in local folder下载本地文件夹中的所有txt文件

在此处输入图像描述

There are working samples for different versions有不同版本的工作样本

https://docs.microsoft.com/en-us/samples/azure-samples/storage-blobs-dotnet-quickstart/upload-download-blobs-net/ https://docs.microsoft.com/en-us/samples/azure-samples/storage-blobs-dotnet-quickstart/upload-download-blobs-net/

V11 works for me (I need to use this one due to.Net version limitation). V11 对我有用(由于.Net 版本限制,我需要使用这个)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM