0

We are using Azure File Shares (File shares, not GPV2, meaning we're not using blobs or queues, just File Shares) to store our files.

We need to check if a list of file paths exist of not.

Is there a "bulk" version of ShareFileClient.ExistsAsync ?

What's the best workaround otherwise ?

We tried calling Exists on each path, each call in it's own task, but it takes too long to return (for 250 paths it takes around 25 seconds):

var tasks = paths.AsParallel().Select(p => Task.Run(() =>
{
  // share is a captured variable of type ShareClient
  var dir = share.GetDirectoryClient(GetDirName(p));
  var file = dir.GetFileClient(GetFileName(p));
  var result = file.Exists();
  return result.Value;
}));
Abdelhakim
  • 815
  • 1
  • 5
  • 19
  • 2 questions - 1) How many files are there in the directory? 2 - Do the files you want to check follow some naming pattern e.g. all of the files start with a common set of characters? – Gaurav Mantri Nov 03 '22 at 15:58
  • @GauravMantri 1) There are 8 directories in the file share, each directory having subdirectories...etc. the leaf directories contain around 20 files each. 2) Not really. There are some files starting with a common set of characters, but there is no possible logic to locate them. – Abdelhakim Nov 03 '22 at 16:28
  • Thanks. From your code above, it seems you want to check the existence of a file in a particular directory and not within that directory and its sub directories. Is that correct? – Gaurav Mantri Nov 03 '22 at 16:33
  • @GauravMantri that is correct. – Abdelhakim Nov 03 '22 at 16:36

1 Answers1

0

As such there's no direct way to check the existence of multiple files.

However, there is a workaround:

What you can do is list the files and subdirectories in a directory using GetFilesAndDirectoriesAsync(ShareDirectoryGetFilesAndDirectoriesOptions, CancellationToken) method. Once you get the list, you will loop over the list and and can check if a file by a particular name exists in a directory or not.

This will be much faster and cost efficient because you are making a single request to get the list of files instead of calling FileClient.Exists method on each file where each method call is a separate network request.

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241