英文:
How to list a blob that is inside a unknown folder?
问题
我目前正在尝试列出我的存储账户中特定的 Blob。
例如,我的容器包含以下结构的 Blob。
https://storage.blob.core.windows.net/
我想要获取位于第三个文件夹位置的所有 Blob。
所以如果我的容器包含以下 Blob:
- animal/herbivore/tall/giraffe.txt
- animal/herbivore/zebra.txt
- car/electric/tesla/modely.txt
- car/electric/testla.txt
我只想列出:
- animal/herbivore/tall/giraffe.txt
- car/electric/tesla/modely.txt
public async Task<List<string>> GetRecentGalleriesAsync()
{
var blobNames = new List<string>();
await foreach (BlobHierarchyItem blobItem in _containerClient.GetBlobsByHierarchyAsync(delimiter: "/", prefix: "*/*/*/"))
{
blobNames.Add($"{_containerClient.Uri.AbsoluteUri}/{blobItem.Blob.Name}");
}
return blobNames;
}
我尝试过在这个方法中使用不同的分隔符和前缀值。此外,我认为参数中不允许使用 '*' 符号。不幸的是,我得到了一个空数组。如何只列出位于第三个文件夹中的 Blob?
英文:
I'm currently trying to list specific Blobs in my storage account.
For example my container contains blob in the following structure.
https://storage.blob.core.windows.net/<containername>/<folder1>/<folder2>/<folder3>/<file>
I want to receive all the blobs that are in a location of a third folder.
So if my container had the following blobs:
- animal/herbivore/tall/giraffe.txt
- animal/herbivore/zebra.txt
- car/electric/tesla/modely.txt
- car/electric/testla.txt
I only want to list:
- animal/herbivore/tall/giraffe.txt
- car/electric/tesla/modely.txt
public async Task<List<string>> GetRecentGalleriesAsync()
{
var blobNames = new List<string>();
await foreach (BlobHierarchyItem blobItem in _containerClient.GetBlobsByHierarchyAsync(delimiter: "/", prefix: "*/*/*/"))
{
blobNames.Add($"{_containerClient.Uri.AbsoluteUri}/{blobItem.Blob.Name}");
}
return blobNames;
}
I have tried using different values for the delimiter and prefix on this method. Also, I believe the use of '*' is not allowed in the parameters. Unfortunately, I am left with an empty array. How can I only list the blobs that sits in a third folder?
答案1
得分: 2
以下是您要的翻译:
>I want to receive all the blobs that are in a location of a third folder.
我想要接收位于第三个文件夹位置的所有 blob。
I have reproduced in my environment and got expected results as below:
我在我的环境中重现了,并获得了如下预期结果:
In portal:
在门户中:
Have 2 folders with structure <folder1><folder2><folder3><file>
有两个文件夹,结构如下:<folder1><folder2><folder3><file>
C# code which worked for me:
我使用的有效的 C# 代码:
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
BlobContainerClient conClient = new BlobContainerClient("DefaultEndpointsProtocol=https;AccountName=rith;AccountKey=BT2tkejS7Hi3aG7qRkHBqyG4q2qCHQ6Nlj5CUkJyETwjv8nJpnRTxHk8oExoW8gj2qEJctmDqvYy+ASt6qFsSA==;EndpointSuffix=core.windows.net", "rithwik");
string[] arr = new string[0];
char cc = '/';
await foreach (BlobItem b in conClient.GetBlobsAsync())
{
int count = b.Name.Count(c => c == cc);
if (count == 3)
{
Array.Resize(ref arr, arr.Length + 1);
arr[arr.Length - 1] = b.Name;
Console.WriteLine(b.Name);
}
}
Output:
输出:
If you want to get from 4th Folder you need to keep equal to 4( in if condition) in the code.
如果您想要从第四个文件夹获取,您需要在代码中将条件设置为等于4。
EDIT:
编辑:
WITH LIST:
使用列表:
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
BlobContainerClient conClient = new BlobContainerClient("DefaultEndpointsProtocol=https;AccountName=rith;AccountKey=BT2tkejS7Hi3aG7qRkHBqyG4q2qCHQ6Nlj5CUkJyETwjv8nJpnRTxHk8oExoW8gj2qEJctmDqvYy+ASt6qFsSA==;EndpointSuffix=core.windows.net", "rithwik");
List<string> l = new List<string>();
char cc = '/';
await foreach (BlobItem b in conClient.GetBlobsAsync())
{
int count = b.Name.Count(c => c == cc);
if (count == 3)
{
l.Add(b.Name);
}
}
foreach (var item in l)
{
Console.WriteLine( item);
}
希望这些翻译对您有所帮助。
英文:
>I want to receive all the blobs that are in a location of a third folder.
I have reproduced in my environment and got expected results as below:
In portal:
Have 2 folders with structure <folder1><folder2><folder3><file>
C# code which worked for me:
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
BlobContainerClient conClient = new BlobContainerClient("DefaultEndpointsProtocol=https;AccountName=rith;AccountKey=BT2tkejS7Hi3aG7qRkHBqyG4q2qCHQ6Nlj5CUkJyETwjv8nJpnRTxHk8oExoW8gj2qEJctmDqvYy+ASt6qFsSA==;EndpointSuffix=core.windows.net", "rithwik");
string[] arr = new string[0];
char cc = '/';
await foreach (BlobItem b in conClient.GetBlobsAsync())
{
int count = b.Name.Count(c => c == cc);
if (count == 3)
{
Array.Resize(ref arr, arr.Length + 1);
arr[arr.Length - 1] = b.Name;
Console.WriteLine(b.Name);
}
}
Output:
If you want to get from 4th Folder you need to keep equal to 4( in if condition) in the code.
EDIT:
WITH LIST:
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
BlobContainerClient conClient = new BlobContainerClient("DefaultEndpointsProtocol=https;AccountName=rith;AccountKey=BT2tkejS7Hi3aG7qRkHBqyG4q2qCHQ6Nlj5CUkJyETwjv8nJpnRTxHk8oExoW8gj2qEJctmDqvYy+ASt6qFsSA==;EndpointSuffix=core.windows.net", "rithwik");
List<string> l = new List<string>();
char cc = '/';
await foreach (BlobItem b in conClient.GetBlobsAsync())
{
int count = b.Name.Count(c => c == cc);
if (count == 3)
{
l.Add(b.Name);
}
}
foreach (var item in l)
{
Console.WriteLine( item);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论