Search code examples
c#azureazure-storagesmbazure-files

Azure file Storage SMB slow to list files in directory


We have an app that lists files in a folder through Azure Files. When we use the C# method:

Directory.GetFiles(@"\\account.file.core.windows.net\xyz")

It takes around a minute when there are 2000 files.

If we use CloudStorageAccount to do the same:

  CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
  CloudFileDirectory directory = fileClient.GetShareReference("account").GetRootDirectoryReference().GetDirectoryReference("abc");
  Int64 totalLength = 0;
  foreach (IListFileItem fileAndDirectory in directory.ListFilesAndDirectories())
  {
    CloudFile file = (CloudFile)fileAndDirectory;
    if (file == null) //must be directory if null
      continue;

    totalLength += file.Properties.Length;
  }

It returns all the files, but takes around 10 seconds. Why is there such a large difference in performance?


Solution

  • When using Directory.GetFiles (System File API), it actually talks to Azure File Storage via SMB protocol (v2.1 or v3.0 depends on client OS version). However when switch to CloudStorageAccount, it talks to File Storage via REST. If you use Wireshark you will discover the SMB protocol will have several back and forth requests between client and server due to the nature of the protocol. The reason for Azure File Storage supports both SMB and REST access is to allow your legacy code/application(which used to access file shares hosted by file servers) can now talk to a file share in Cloud without code change.

    So the recommendation in your case is using REST call to access Azure File Storage for better performance.