public void CloudFileDirectoryCreateIfNotExists() { CloudFileShare share = GetRandomShareReference(); share.Create(); try { CloudFileDirectory directory = share.GetRootDirectoryReference().GetDirectoryReference("directory1"); Assert.IsTrue(directory.CreateIfNotExists()); Assert.IsFalse(directory.CreateIfNotExists()); directory.Delete(); Assert.IsTrue(directory.CreateIfNotExists()); } finally { share.Delete(); } }
public AzureSession(string connectionString, string shareName, string systemDir, int waitForLockMilliseconds = 5000, bool optimisticLocking = true, bool enableCache = true, CacheEnum objectCachingDefaultPolicy = CacheEnum.Yes) : base(systemDir, waitForLockMilliseconds, optimisticLocking, enableCache, objectCachingDefaultPolicy) { m_cloudStorageAccount = CloudStorageAccount.Parse(connectionString); if (Path.IsPathRooted(systemDir) == false) SystemDirectory = systemDir; m_shareName = shareName; m_cloudFileClient = m_cloudStorageAccount.CreateCloudFileClient(); m_cloudShare = m_cloudFileClient.GetShareReference(shareName); if (m_cloudShare.Exists()) { m_rootDir = m_cloudShare.GetRootDirectoryReference(); m_databaseDir = m_rootDir.GetDirectoryReference(systemDir); m_databaseDir.CreateIfNotExists(); } }
private void Initialise(LoggingEvent loggingEvent) { if (_storageAccount == null || AzureStorageConnectionString != _thisConnectionString) { _storageAccount = CloudStorageAccount.Parse(AzureStorageConnectionString); _thisConnectionString = AzureStorageConnectionString; _client = null; _share = null; _folder = null; _file = null; } if (_client == null) { _client = _storageAccount.CreateCloudFileClient(); _share = null; _folder = null; _file = null; } if (_share == null || _share.Name != ShareName) { _share = _client.GetShareReference(ShareName); _share.CreateIfNotExists(); _folder = null; _file = null; } if (_folder == null || Path != _thisFolder) { var pathElements = Path.Split(new[] {'\\', '/'}, StringSplitOptions.RemoveEmptyEntries); _folder = _share.GetRootDirectoryReference(); foreach (var element in pathElements) { _folder = _folder.GetDirectoryReference(element); _folder.CreateIfNotExists(); } _thisFolder = Path; _file = null; } var filename = Regex.Replace(File, @"\{(.+?)\}", _ => loggingEvent.TimeStamp.ToString(_.Result("$1"))); if (_file == null || filename != _thisFile) { _file = _folder.GetFileReference(filename); if (!_file.Exists()) _file.Create(0); _thisFile = filename; } }
// TODO: extract the common pattern(Upload and Download) private static void UploadFiles(Dictionary<string, DataSyncFileInfo> localOnly, CloudFileClient fileClient) { List<Task> waitTasks = new List<Task>(); foreach (var file in localOnly) { var path = file.Key.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); if (path.Length < 2) { throw new Exception("share is needed, so you should create a flod in local."); } string shareString = path[0]; fileClient.GetShareReference(shareString).CreateIfNotExists(); string dirString = path[0] + "/"; for (int i = 1; i < path.Length - 1; i++) { dirString += path[i] + "/"; CloudFileDirectory fileDir = new CloudFileDirectory( new Uri(fileClient.BaseUri.AbsoluteUri + dirString), fileClient.Credentials); fileDir.CreateIfNotExists(); } var uploadTask = Task.Run(() => { Console.WriteLine($"upload {file.Key} ..."); CloudFile cloudFile = new CloudFile(new Uri(fileClient.BaseUri.AbsoluteUri + file.Key.Substring(1)), fileClient.Credentials); cloudFile.UploadFromFile(file.Value.LocalFile.FullName); Console.WriteLine($"{file.Key} is uploaded!"); }); waitTasks.Add(uploadTask); // TODO: config the number of running task while (waitTasks.Count(task => task.Status == TaskStatus.Running || task.Status == TaskStatus.WaitingToRun) >= 8) { Task.WaitAny(waitTasks.ToArray()); } } Task.WaitAll(waitTasks.ToArray()); }
/// <inheritdoc /> public override bool CreateDirectory(string path) { var share = m_rootDir.GetDirectoryReference(path); if (!share.Exists()) { m_rootDir = m_cloudShare.GetRootDirectoryReference(); m_databaseDir = m_rootDir.GetDirectoryReference(path); m_databaseDir.CreateIfNotExists(); return true; } return false; }
private static void CreateCloudFileDirectoryRecursively(CloudFileDirectory dir) { if (null == dir) { return; } CloudFileDirectory parent = dir.Parent; // null == parent means dir is root directory, // we should not call CreateIfNotExists in that case if (null != parent) { CreateCloudFileDirectoryRecursively(parent); dir.CreateIfNotExists(Transfer_RequestOptions.DefaultFileRequestOptions); } }