I have a scheduled WebJob which is triggered every 5 minutes.
- It checks if there are any files in Azure File Share using ShareClient libraries.
- If files are available then the WebJob downloads the file and deletes the original file.
- So when there is a large file being uploaded from portal to the File Share(>2GB its taking ~1 minute to upload and the file share is not mounted)
- And simultaneously WebJob is triggered and starts running, the WebJob finds the file being uploaded and it is downloading part of the file content which is uploaded till that time.
- The rest of the content is not downloaded.
How can I download the entire file in this scenario? Is it possible to download the file only once it is completely uploaded?
Sample code:
var shareItems = shareDirectory.GetFilesAndDirectories().Where(item => item.IsDirectory == false).ToList();
foreach (var item in shareItems)
{
ShareFileClient file = shareDirectory.GetFileClient(item.Name);
if (await file.ExistsAsync())
{
string filePath = Path.Combine(destPath, item.Name);
using (FileStream stream = File.OpenWrite(filePath))
{
try
{
ShareFileDownloadInfo download = await file.DownloadAsync();
download.Content.CopyTo(stream);
}
catch (Exception ex)
{
throw;
}
stream.Flush();
stream.Close();
}
}
//Delete the original file from file share
file.Delete();
}
Per my understanding, you want to make sure that only a big file has been uploaded completely that you will download it. If so, maybe you can just check the time gap between the file last modified timestamp and the current UTC timestamp.
If a file is being uploaded, its
LastModifiedproperty will be changing all the time, per my testing, the gap between the current UTC timestamp is about 1-3 seconds.So you can just try this: