Search code examples
c#azure.net-framework-versionazure-file-share

Downloading Files from Azure File Share C# .NET Framework 4.8 (Getting error from ShareDirectoryClient crashes when subdirectory detected)


I can download all Files from Azure File share when there are no sub directories, but whenever subdirectory is within the directory, the code crashes to error:

Azure.RequestFailedException: 'The specified resource does not exist.
RequestId:0ccda3e3-d01a-00ae-42a5-21fad0000000
Time:2024-10-18T21:31:20.6046993Z
Status: 404 (The specified resource does not exist.)
ErrorCode: ResourceNotFound

Code: (What I tried to do is use item.IsDirectory but that doesn't skip over directories)

 public void DownloadALlFiles()
 {

    
         // Get a connection string to our Azure Storage account.
         string connectionString = "connectionstring";

         // Get a reference to a share named "sample-share"
         ShareClient share = new ShareClient(connectionString, "clientshare");

     // Get a reference to a directory named "sample-dir"
      ShareDirectoryClient dir = share.GetDirectoryClient("directory/");


    

         foreach (ShareFileItem item in dir.GetFilesAndDirectories())
         {

             Console.WriteLine(item.Name);
             // Get a reference to a file named "sample-file" in directory "sample-dir"
             ShareFileClient file = dir.GetFileClient(item.Name);

             // Download the file
            
             ShareFileDownloadInfo download = file.Download();

             using (FileStream stream = File.Open("localpath"+ item.Name, FileMode.CreateNew))
             {
                 download.Content.CopyTo(stream);
                 stream.FlushAsync();
                 stream.Close();
             }
         

            // await file.DeleteAsync();
         }

         //Console.ReadLine();
     
 }

Let me elaborate, so I want to download just the files ignoring the subdirectory. When, there is no subdirectories present, the code downloads all files just fine. But, whenever a sub directory is there, it crashes with error show above. I eliminated the access issue, and other issues that might be a common one.

I tried to use the following code to skip over directory, but this is also skipping over the files.

if (item.IsDirectory)
{

}

I am pretty sure that using item.IsDirectory is a solution to this, but I do not how to effectively integrate it.


Solution

  • Your issue is simply a question of creating a method that can then be called recursively in order to handle subdirectories, for example:

    public void DownloadFiles(ShareDirectoryClient dir) 
    {
       foreach (ShareFileItem it in dir.GetFilesAndDirectories()) 
          {
             if (it.IsDirectory)
             {
                ShareDirectoryClient subDir = dir.GetSubdirectoryClient(item.Name); // According to the SDK documentation, you're going to need to use this to get a ShareDirectoryClient that can handle the sub-directory insidide the parent.
                string subPath = Path.Combine("localpath", item.Name);
                Directory.CreateDirectory(subPath); // This should prepare the directory structure so you can actually successfully perform the download afterwards
                DownloadFiles(subDir, subPath); // Now, we call the method again, but, instead of feeding it the original ShareDirectoryClient used for the base directory, we're feeding it a client for the subdirectory.
             }
             else
             {
                // Your normal file download code for when you're dealing with an actual file and not a directory.
             }
          }
    }
    

    Thea idea is to create the method in a way that you'll be able to call it from inside the method itself in order to handle subdirectories correctly.

    Hope this helps.