Azure blob files are stored in a plain list without any physical folder structure, but we can create virtual folders where each file's folder path is a part of it's name.
It brings out another problem, how to retrieve a list of ALL files in virtual sub-folder, using only that folder's name?
Actually, there's a simpler way to do that and it is available in the library itself. If you look at CloudBlobContainer.ListBlobs
method, it accepts two parameters:
- prefix: This is the name of your directory. If it is a nested directory, you will need to specify the full path e.g. myfolder/mysubfolder.
useFlatBlobListing: Setting this value to true
will ensure that only blobs are returned (including inside any sub folders inside that directory) and not directories and blobs.
var account = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
var blobClient = account.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("blob-container-name");
var blobs = container.ListBlobs(prefix: "container-directory", useFlatBlobListing: true);
You will get a list of all blobs belonging in the "container-directory" in blobs
variable.
This static class BlobHelper
will load the list of all the blob files in a given blob folder, and all of it's sub-folders.
Just call it like this:
var blobs = BlobHelper.ListFolderBlobs("blob-container-name", "container-directory");
Here is full BlobHelper
code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
// Class to contain list of blob files info
public class BlobFileInfo {
public string FileName { get; set; }
public string BlobPath { get; set; }
public string BlobFilePath { get; set; }
public IListBlobItem Blob { get; set; }
}
public static class BlobHelper {
// Load blob container
public static CloudBlobContainer GetBlobContainer(string containerName) {
var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(containerName);
return container;
}
// Get recursive list of files
public static IEnumerable<BlobFileInfo> ListFolderBlobs(string containerName, string directoryName) {
var blobContainer = GetBlobContainer(containerName);
var blobDirectory = blobContainer.GetDirectoryReference(directoryName);
var blobInfos = new List<BlobFileInfo>();
var blobs = blobDirectory.ListBlobs().ToList();
foreach (var blob in blobs) {
if (blob is CloudBlockBlob) {
var blobFileName = blob.Uri.Segments.Last().Replace("%20", " ");
var blobFilePath = blob.Uri.AbsolutePath.Replace(blob.Container.Uri.AbsolutePath + "/", "").Replace("%20", " ");
var blobPath = blobFilePath.Replace("/" + blobFileName, "");
blobInfos.Add(new BlobFileInfo {
FileName = blobFileName,
BlobPath = blobPath,
BlobFilePath = blobFilePath,
Blob = blob
});
}
if (blob is CloudBlobDirectory) {
var blobDir = blob.Uri.OriginalString.Replace(blob.Container.Uri.OriginalString + "/", "");
blobDir = blobDir.Remove(blobDir.Length - 1);
var subBlobs = ListFolderBlobs(containerName, blobDir);
blobInfos.AddRange(subBlobs);
}
}
return blobInfos;
}
}
From Gaurav Mantri's answer, here's a simple way to show the files recursively as a hierarchy.
public class UriNode
{
public Uri ThisUri { get; private set; }
public IEnumerable<UriNode> Children { get; private set; }
public UriNode(CloudBlobContainer container, Uri thisUri = null)
{
ThisUri = thisUri;
if (ThisUri == null)
{
Children = container.ListBlobs().Select(b => new UriNode(container, b.Uri));
return;
}
if (!new Regex(@"\/$").IsMatch(ThisUri.AbsolutePath)) return;
var prefix = string.Join("/", ThisUri.Segments.Skip(2).Take(ThisUri.Segments.Length - 2));
Children = container.ListBlobs(prefix).Select(b => new UriNode(container, b.Uri));
}
}
usage:
new UriNode(container);