// `CloudFileDirectory.GetFileReference` expects a file name, not a // path to a file. Calling it with a relative path will create a // reference to a file in the root directory of the file share, with // the directory segments included in the file name, rather than // creating a reference to a file in a subdirectory of the root file // share directory. We therefore we need to "walk" the path segments // when building a file reference from a file path. public static CloudFile GetFileReference(this CloudFileShare fileShare, string path) { var fileName = Path.GetFileName(path); var directoryPath = Path.GetDirectoryName(path); var parentDirectory = fileShare.GetDirectoryReference(directoryPath); return(parentDirectory.GetFileReference(fileName)); }
public override async Task DeleteDirectoryAsync( string path, CancellationToken cancellationToken = default(CancellationToken)) { var contents = await GetDirectoryContentsAsync(path); if (contents.Exists) { foreach (var item in contents) { if (item.IsDirectory) { await DeleteDirectoryAsync(item.Path); } else { await DeleteFileAsync(item.Path); } } var directory = _share.GetDirectoryReference(path); await directory.DeleteIfExistsAsync(); } }
public void Initialize() { _fileContainer = new AzureFileContainer(CONNECTION_STRING, SHARE_NAME); _cloudStorageAccount = CloudStorageAccount.Parse(CONNECTION_STRING); _cloudFileShare = _cloudStorageAccount.GetShareReference(SHARE_NAME); _cloudFileRootDirectory = _cloudFileShare.GetRootDirectoryReference(); _cloudFileSourceDirectory = _cloudFileShare.GetDirectoryReference(path: new[] { SOURCE_DIRECTORY }, createIfNotExists: true); _cloudFileShare.GetDirectoryReference(path: new[] { SOURCE_DIRECTORY, DUMMY_DIRECTORY }, createIfNotExists: true); _cloudFileShare.GetDirectoryReference(path: new[] { TARGET_DIRECTORY }, createIfNotExists: true); _cloudRootFile = _cloudFileRootDirectory.GetFileReference(FILE_NAME); _cloudSourceFile = _cloudFileSourceDirectory.GetFileReference(FILE_NAME); TaskUtilities.ExecuteSync(_cloudRootFile.UploadTextAsync(FILE_CONTENT)); TaskUtilities.ExecuteSync(_cloudSourceFile.UploadTextAsync(FILE_CONTENT)); }
public async Task <IDirectoryContents> GetDirectoryContentsAsync( string path, CancellationToken cancellationToken = default(CancellationToken)) { var directory = _share.GetDirectoryReference(path); try { await directory.FetchAttributesAsync(); var directoryContents = new AzureFileStorageDirectoryContents(directory); return(directoryContents); } catch (StorageException ex) when(StorageExceptionHelper.IsNotFoundStorageException(ex)) { return(new NotFoundDirectoryContents(path)); } }
public void CopyDirectoryTest(string directoryName, string[] sourcePath, string[] targetPath) { // Arrange CloudFileDirectory sourceCloudFileDirectory = _cloudFileShare.GetDirectoryReference(directoryName, sourcePath); int expectedChildCount = TaskUtilities .ExecuteSync(sourceCloudFileDirectory.ListFilesAndDirectoriesSegmentedAsync(new FileContinuationToken())) .Results .Count(); // Act _fileContainer.CopyDirectory(directoryName, sourcePath, targetPath); CloudFileDirectory targetCloudFileDirectory = _cloudFileShare.GetDirectoryReference(directoryName, targetPath); bool result = TaskUtilities.ExecuteSync(targetCloudFileDirectory.ExistsAsync()); int actualChildCount = TaskUtilities .ExecuteSync(targetCloudFileDirectory.ListFilesAndDirectoriesSegmentedAsync(new FileContinuationToken())) .Results .Count(); // Assert Assert.IsTrue(result); Assert.AreEqual(expectedChildCount, actualChildCount); }