1
votes

enter image description hereI am trying to download a block blob from Azure storage explorer. I am able to download all the block blobs that exist in the root directory of my container. I am unable to download blobs that are nested in subfolders inside the container.

        CloudBlockBlob blob = container.GetBlockBlobReference(fileName);

        SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();            
        sasConstraints.SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddHours(1);
        sasConstraints.Permissions = SharedAccessBlobPermissions.Read;

        string sasBlobToken = blob.GetSharedAccessSignature(sasConstraints);

        return blob.Uri.AbsoluteUri + sasBlobToken;
3
I am not sure what you mean. Blob storage does not have the concept of subfolders. Only virtual folders. Can you describe better what you are trying to achieve? The posted code shows how to download a single blob.Peter Bons
By Subfolders, I mean folder inside a folder. I have created multiple folders inside my container.Harshith Reddy
Do you want to download all blobs inside a virtual folder or download a single blob that is inside a virtual folder?Gaurav Mantri
Folders only exist if you have files in them (since the folders are really just part of the file names). Do you have files in them?juunas

3 Answers

1
votes

I couldn't get the absolute path of blockBlob using GetBlockBlobReference(fileName). The below code solved my issue. I got the listing and then used LINQ to get the blockBlob with the absolute path details. This post helped as well

        do
        {
            var listingResult = await blobDirectory.ListBlobsSegmentedAsync(useFlatBlobListing, blobListingDetails, maxBlobsPerRequest, continuationToken, null, null);


           //The below lined fetched the blockBlob with the correct directory details.
            var blockBlob = listingResult.Results.Where(x => x.Uri.AbsolutePath.Contains(fileName)).Count()>0 ? (CloudBlockBlob)listingResult.Results.Where(x=>x.Uri.AbsolutePath.Contains(fileName)).FirstOrDefault():null;

            if (blockBlob != null)
            {                    
                sasConstraints.SharedAccessExpiryTime = expiryTimeSAS;
                sasConstraints.Permissions = SharedAccessBlobPermissions.Read;                    
                string sasBlobToken = blockBlob.GetSharedAccessSignature(sasConstraints);                    
                return blockBlob.Uri.AbsoluteUri + sasBlobToken;                    
            }                
            continuationToken = listingResult.ContinuationToken;                

        } while (continuationToken != null);

Correct me if there is any other efficient way of pulling the blob information from a list of directories in a container.

1
votes

Below Solution helps to access single File Absolute path residing under Directory( or Folder Path).

public static String GetBlobUri(string dirPath, string fileName)
      {
           //Get a reference to a blob within the container.
         CloudStorageAccount storageAccount = CloudStorageAccount.Parse("Blob Key");
          CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
          CloudBlobContainer container = blobClient.GetContainerReference("Blob Container");
          CloudBlockBlob blockBlob = container.GetBlockBlobReference(dirPath+fileName);

        return blockBlob.Uri.AbsoluteUri;
    }

Hope this helps someone trying to access Blob File path based on multi level Directory(Level1/Level2/Level3) path.

0
votes

Just use the ListBlobs mentioned in the answer from Gaurav Mantri to retrieve all files (blobs) within your desired subfolder. Then iterate over it and download it:

var storageAccount = CloudStorageAccount.Parse("yourConnectionString");
var client = storageAccount.CreateCloudBlobClient();
var container = client.GetContainerReference("yourContainer");
var blobs = container.ListBlobs(prefix: "subdirectory1/subdirectory2", useFlatBlobListing: true);
foreach (var blob in blobs) 
{
    blob.DownloadToFileAsync("yourFilePath");
}