web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

How to check folder is present in Azure blob container

(4) ShareShare
ReportReport
Posted on by 230
Hi Team,
 
Inside the azure blob container name i am having multiple folders, like below.
 
ContinerName/Folder1/Folder2
 
In case of any folder is missing i would like to show some error message.
 
In above if Folder2 is missing show error like folder is not present.
 
Below is the code which i am using.
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.File;


 public void checkFolderName(str _filePath)
 {
     CloudBlobContainer blobcontainer;
     CloudBlobClient    blobClient;
      
     
     var credential     = new StorageCredentials("StorageAccountName", "StorageAccessKey");
     var storageAccount = new CloudStorageAccount(credential, true);
     blobClient         = storageAccount.CreateCloudBlobClient();
     blobcontainer      = blobClient.GetContainerReference(_filePath);
  }
 
Please let me know how to achieve this.
I have the same question (0)
  • Jonas "Jones" Melgaard Profile Picture
    4,930 Most Valuable Professional on at
    Folders in containers are not how you normally think of folders (I.e. they do not exist if there is no blobs in them).
    But if you want to check, you can use the method 'blobContainer.GetBlobDirectoryReference('path')' and check if any blobs are returned.
     
    There are examples on StackOverflow if you need it.
  • Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at
    There aren't actually folders in blob containers. For example, Folder1/file.txt is simply a blob named Folder1/file.txt, not file.txt blob in a folder.
  • CU21091228-0 Profile Picture
    230 on at
    Below code i am using.
    CloudBlobDirectory cloudBlobDirectory = blobContainer.GetBlobDirectoryReference('Folder1/Folder2');
    
    if (!cloudBlobDirectory)
    {
       throw error ("Folder name not found");
    }
    In case folder path is not present i am not getting any error, please let me know where i am wrong.
  • Suggested answer
    Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at
     
    In blob storage, directories don't exist as an item by themselves. What you can have is a blob that has a name that can be interpreted as being in a directory. If you look at the underlying REST API you'll see that that there's nothing in there about directories. What the storage client library is doing for you is searching for blobs that start with the directory name then the delimiter e.g. "DirectoryA/DirectoryB/FileName.txt". What this means is that for a directory to exist it must contain a blob. To check if the directory exists you can try either:
     
    var blobDirectory = client.GetBlobDirectoryReference("Path_to_dir");
    bool directoryExists = blobDirectory.ListBlobs().Count() > 0
     
    or
     
    bool directoryExists = client.ListBlobsWithPrefix("DirectoryA/DirectoryB/").Count();
  • CU21091228-0 Profile Picture
    230 on at
    I try like below.
     
    boolean directoryExists = CloudBlobDirectory.ListBlobs().Count() > 0
    But Ax is not support .Count() after ListBlobs().
     
     
     
     
  • Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at
    Split it to two calls. For example:
    var blobs = CloudBlobDirectory.ListBlobs();
    boolean directoryExists = blobs.Count() > 0;
    It'll likely fix your problem. If not, please describe the problem to us.
  • CU21091228-0 Profile Picture
    230 on at
    In Azure blob container having multiple folders, i would like to check those folder is present or not, if not present throw a error.
    Below us my AX code.
    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Auth;
    using Microsoft.WindowsAzure.Storage.Blob;
    using Microsoft.WindowsAzure.Storage.File;
    
    
     public void checkFolderName(str _filePath, str _containerName)
     {
         CloudBlobContainer blobcontainer;
         CloudBlobClient    blobClient;
          
         
         var credential     = new StorageCredentials("StorageAccountName", "StorageAccessKey");
         var storageAccount = new CloudStorageAccount(credential, true);
         blobClient         = storageAccount.CreateCloudBlobClient();
         blobcontainer      = blobClient.GetContainerReference(_containerName);
         CloudBlobDirectory cloudBlobDirectory = blobContainer.GetBlobDirectoryReference(_filePath);
      }
     
    I try below code, Ax is not supporting blobs.count()
    var blobs = CloudBlobDirectory.ListBlobs(false, 0, null, null);
    boolean directoryExists = blobs.Count() > 0;
  • Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at
    I'm sorry, but "Ax is not supporting blobs.count()" is not a good description of a problem. Please tell us what happened. For example, did you get a compilation error a runtime exception? What did the error message said?
  • Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at
    I think I see your problem. Your code (the call of ListBlobs()) assumes that you're using Microsoft.Azure.Storage.Blob.CloudBlobDirectory, but you aren't. You're using CloudBlobDirectory from Microsoft.WindowsAzure.Storage.Blob namespace.

    Note that the latest library for this purpose is Azure.Storage.Blobs. I think that the solution there is using BlobContainerClient.GetBlobs() with the folder path in the prefix parameter.
  • CU21091228-0 Profile Picture
    230 on at
    I have added below code.
    Using Azure.Storage.Blobs;
    
    public void checkFolderName(str _filePath, str _containerName)
    {
       BlobContainerClient blobContainerClient;
    
       //
       blobContainerClient.GetBlobs(_filePath);
    }
    Below error i am getting.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 664 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 522 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 303 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans