I have a scheduled WebJob which is triggered every 5 minutes.
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 LastModified
property 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:
var shareItems = shareDirectory.GetFilesAndDirectories().Where(item => item.IsDirectory == false).ToList();
foreach (var item in shareItems)
{
ShareFileClient file = shareDirectory.GetFileClient(item.Name);
if (file.ExistsAsync().GetAwaiter().GetResult())
{
var fileLastModifiedTime = file.GetProperties().Value.LastModified;
var currentTime = DateTime.UtcNow;
//Only download files that have finished uploading 5 minutes ago
if (fileLastModifiedTime.AddMinutes(5).CompareTo(currentTime) < 0) {
string filePath = Path.Combine(destPath, item.Name);
using (FileStream stream = File.OpenWrite(filePath))
{
try
{
ShareFileDownloadInfo download = file.DownloadAsync().GetAwaiter().GetResult();
download.Content.CopyTo(stream);
}
catch (Exception ex)
{
throw;
}
stream.Flush();
stream.Close();
}
}