none
Download File from container that has recursive directories and files

    Question

  • Hi Team,

    I used below code snipet to download files from blob storage.


    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("StorageConnectionString"));

    // Create the blob client.
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

    // Retrieve reference to a previously created container.
    CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

    // Retrieve reference to a blob named "photo1.jpg".
    CloudBlockBlob blockBlob = container.GetBlockBlobReference("photo1.jpg");

    // Save blob contents to a file.
    using (var fileStream = System.IO.File.OpenWrite(@"path\myfile"))
    {
        blockBlob.DownloadToStream(fileStream);
    }


                    foreach (IListBlobItem item in container.ListBlobs(null, false))
                    {                  

                       if (item.GetType() == typeof(CloudBlockBlob))
                        {
                            CloudBlockBlob blob = (CloudBlockBlob)item;


                                        string path = @"DownloadFolder\" + filename;
                                        using (var fileStream = System.IO.File.Create(path))
                                        {
                                            blob.DownloadToStream(fileStream);
                                        }

                        }

                    }


    This perfectly works fine if the container has direct file in child directory
    Ex:
    Container: Photos
    filename : anil.jpg

    but if i have below structure it does not work.

    Container: Photos
    filename : test/guid23434324335/logs/anil.jpg

    Can you please help in this regard on how to traverse and downlaod anil.jpg

    Friday, December 11, 2015 11:55 AM

All replies