如何列出位于未知文件夹内的 Blob?

huangapple go评论70阅读模式
英文:

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/&lt;containername&gt;/&lt;folder1&gt;/&lt;folder2&gt;/&lt;folder3&gt;/&lt;file&gt;

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&lt;List&lt;string&gt;&gt; GetRecentGalleriesAsync()
        {
            var blobNames = new List&lt;string&gt;();
            await foreach (BlobHierarchyItem blobItem in _containerClient.GetBlobsByHierarchyAsync(delimiter: &quot;/&quot;, prefix: &quot;*/*/*/&quot;))
            {

                    blobNames.Add($&quot;{_containerClient.Uri.AbsoluteUri}/{blobItem.Blob.Name}&quot;);
            }

            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:

在门户中:

如何列出位于未知文件夹内的 Blob?

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:

输出:

如何列出位于未知文件夹内的 Blob?

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:

如何列出位于未知文件夹内的 Blob?

Have 2 folders with structure &lt;folder1&gt;&lt;folder2&gt;&lt;folder3&gt;&lt;file&gt;

C# code which worked for me:

using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;

BlobContainerClient conClient = new BlobContainerClient(&quot;DefaultEndpointsProtocol=https;AccountName=rith;AccountKey=BT2tkejS7Hi3aG7qRkHBqyG4q2qCHQ6Nlj5CUkJyETwjv8nJpnRTxHk8oExoW8gj2qEJctmDqvYy+ASt6qFsSA==;EndpointSuffix=core.windows.net&quot;, &quot;rithwik&quot;);

string[] arr = new string[0];
char cc = &#39;/&#39;;
await foreach (BlobItem b in conClient.GetBlobsAsync())
{ 
    
    int count = b.Name.Count(c =&gt; c == cc);
    if (count == 3)
    {
        Array.Resize(ref arr, arr.Length + 1);
        arr[arr.Length - 1] = b.Name;
        Console.WriteLine(b.Name);

    }
}

Output:

如何列出位于未知文件夹内的 Blob?

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(&quot;DefaultEndpointsProtocol=https;AccountName=rith;AccountKey=BT2tkejS7Hi3aG7qRkHBqyG4q2qCHQ6Nlj5CUkJyETwjv8nJpnRTxHk8oExoW8gj2qEJctmDqvYy+ASt6qFsSA==;EndpointSuffix=core.windows.net&quot;, &quot;rithwik&quot;);

List&lt;string&gt; l = new List&lt;string&gt;();
char cc = &#39;/&#39;;
await foreach (BlobItem b in conClient.GetBlobsAsync())
{ 
    
    int count = b.Name.Count(c =&gt; c == cc);
    if (count == 3)
    {
        l.Add(b.Name);
     
    }
}

foreach (var item in l)
{
    Console.WriteLine( item);
}

huangapple
  • 本文由 发表于 2023年6月14日 23:29:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475223.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定