Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 Community / Forums / Finance forum / How to check folder is...
Finance forum
Suggested answer

How to check folder is present in Azure blob container

Posted on by 118
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.
  • Martin Dráb Profile Picture
    Martin Dráb 228,552 Most Valuable Professional on at
    How to check folder is present in Azure blob container
    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.
  • Martin Dráb Profile Picture
    Martin Dráb 228,552 Most Valuable Professional on at
    How to check folder is present in Azure blob container
    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?
  • CU21091228-0 Profile Picture
    CU21091228-0 118 on at
    How to check folder is present in Azure blob container
    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
    Martin Dráb 228,552 Most Valuable Professional on at
    How to check folder is present in Azure blob container
    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
    CU21091228-0 118 on at
    How to check folder is present in Azure blob container
    I try like below.
     
    boolean directoryExists = CloudBlobDirectory.ListBlobs().Count() > 0
    But Ax is not support .Count() after ListBlobs().
     
     
     
     
  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 228,552 Most Valuable Professional on at
    How to check folder is present in Azure blob container
     
    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
    CU21091228-0 118 on at
    How to check folder is present in Azure blob container
    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.
  • Martin Dráb Profile Picture
    Martin Dráb 228,552 Most Valuable Professional on at
    How to check folder is present in Azure blob container
    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.
  • How to check folder is present in azure blob container in D365FO
    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.

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,552 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,552 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans