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:
I only want to list:
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?
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);
}