如何获取 Azure 子容器中的 blob 列表
How to get a list of blobs in a sub container in Azure
我需要从 url 的存储中获取所有文件:“https://example.blob.core.windows.net/file/subfile1/subfile2”
我只需要读取数据文件(例如只有 .txt,所以如果我有 .jpg 或其他东西我应该跳过那些)并且只从子文件 2,而不是整个存储。
使用 c# 顺便说一句
我尝试在我的系统中下载容器子文件夹中的所有 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);
}
}
}
}
}
Azure 存储中的文件夹结构
输出
已下载本地文件夹中的所有 txt 文件
我需要从 url 的存储中获取所有文件:“https://example.blob.core.windows.net/file/subfile1/subfile2” 我只需要读取数据文件(例如只有 .txt,所以如果我有 .jpg 或其他东西我应该跳过那些)并且只从子文件 2,而不是整个存储。
使用 c# 顺便说一句
我尝试在我的系统中下载容器子文件夹中的所有 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);
}
}
}
}
}
Azure 存储中的文件夹结构
输出
已下载本地文件夹中的所有 txt 文件