public void SyncBlobToLocal()
{
var seen = new HashSet<string>();
var blobs = _container.ListBlobs(
new BlobRequestOptions
{
UseFlatBlobListing = true,
BlobListingDetails = BlobListingDetails.Metadata
}).OfType<CloudBlob>();
foreach (var blob in blobs)
{
var path = blob.Uri.ToString().Substring(_container.Uri.ToString().Length + 1);
var entry = new FileEntry
{
CloudLastModified = blob.Properties.LastModifiedUtc,
IsDirectory = blob.Metadata.AllKeys.Any(k => k.Equals("IsDirectory")) &&
blob.Metadata["IsDirectory"].Equals(bool.TrueString, StringComparison.OrdinalIgnoreCase)
};
seen.Add(path);
if (!_entries.ContainsKey(path) || _entries[path].CloudLastModified < entry.CloudLastModified)
{
var tempPath = Path.Combine(_localTempPath, path);
if (entry.IsDirectory)
{
Directory.CreateDirectory(tempPath);
}
else
{
Directory.CreateDirectory(Path.Combine(_localTempPath, Path.GetDirectoryName(path)));
_logger.InfoFormat("[Blob => Local Storage] - Downloading file: '{0}'", path);
using (var stream = File.Open(tempPath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete))
{
blob.DownloadToStream(stream);
}
}
entry.LocalLastModified = new FileInfo(tempPath).LastWriteTimeUtc;
_entries[path] = entry;
}
}
foreach (var path in _entries.Keys.Where(k => !seen.Contains(k)).ToArray())
{
if (_entries[path].IsDirectory)
{
Directory.Delete(Path.Combine(_localTempPath, path), true);
}
else
{
try
{
File.Delete(Path.Combine(_localTempPath, path));
}
catch (Exception e)
{
_logger.Warn("Error cleaning up unused directory: {0}", e);
}
}
_entries.Remove(path);
}
}