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); }
/// <inheritdoc /> public void CopyFile(string sourceFileName, string[] sourceParentDirectories, string targetFileName, string[] targetParentDirectories, bool overwriteIfExists = false) { #region validation if (string.IsNullOrEmpty(sourceFileName)) { throw new ArgumentNullException(nameof(sourceFileName)); } if (string.IsNullOrEmpty(targetFileName)) { throw new ArgumentNullException(nameof(targetFileName)); } #endregion CloudFileDirectory sourceDirectory = CloudFileShare.GetDirectoryReference(path: sourceParentDirectories); CloudFileDirectory targetDirectory = CloudFileShare.GetDirectoryReference(path: targetParentDirectories); CloudFile sourceFile = sourceDirectory.GetFileReference(sourceFileName); CloudFile targetFile = targetDirectory.GetFileReference(targetFileName); if (overwriteIfExists) { TaskUtilities.ExecuteSync(targetFile.DeleteIfExistsAsync()); } if (!TaskUtilities.ExecuteSync(targetFile.ExistsAsync())) { TaskUtilities.ExecuteSync(targetFile.StartCopyAsync(sourceFile)); } }
/// <inheritdoc /> public void CopyFile(string sourceFileName, string[] sourceParentDirectories, string targetFileName, string[] targetParentDirectories, bool overwriteIfExists = false) { #region validation if (string.IsNullOrEmpty(sourceFileName)) { throw new ArgumentNullException(nameof(sourceFileName)); } if (string.IsNullOrEmpty(targetFileName)) { throw new ArgumentNullException(nameof(targetFileName)); } if (sourceParentDirectories == targetParentDirectories) { throw new InvalidOperationException("source can´t be equal target"); } #endregion CloudBlockBlob sourceCloudBlockBlob = CloudBlobContainer.GetCloudBlockBlobReference(sourceFileName, path: sourceParentDirectories); CloudBlockBlob targetCloudBlockBlob = CloudBlobContainer.GetCloudBlockBlobReference(targetFileName, path: targetParentDirectories); if (overwriteIfExists) { TaskUtilities.ExecuteSync(targetCloudBlockBlob.DeleteIfExistsAsync()); } if (!TaskUtilities.ExecuteSync(targetCloudBlockBlob.ExistsAsync())) { TaskUtilities.ExecuteSync(targetCloudBlockBlob.StartCopyAsync(sourceCloudBlockBlob)); } }
/// <inheritdoc /> public string[] CreateFile(string fileName, Stream fileContent, string[] path = null) { #region validation if (string.IsNullOrEmpty(fileName)) { throw new ArgumentNullException(nameof(fileName)); } if (fileContent == null) { throw new ArgumentNullException(nameof(fileContent)); } #endregion CloudFileDirectory currentDir = CloudFileShare.GetDirectoryReference(path: path); CloudFile newFile = currentDir.GetFileReference(fileName); TaskUtilities.ExecuteSync(newFile.UploadFromStreamAsync(fileContent)); string qualifiedUri = GetQualifiedUri(newFile); return(qualifiedUri.Split(DIRECTORY_SEPARATOR_CHAR)); }
/// <inheritdoc /> /// Copies only first tier sub directory and files. public void CopyDirectory(string directoryName, string[] sourcePath, string[] targetPath) { #region validation if (string.IsNullOrEmpty(directoryName)) { throw new ArgumentNullException(nameof(directoryName)); } if (sourcePath == targetPath) { throw new InvalidOperationException("source can´t be equal target"); } #endregion string[] source = sourcePath == null ? new[] { directoryName } : sourcePath.Append(directoryName); // get all files from source directory IEnumerable <string> sourceDirectoryFileNames = GetFiles(source) .Select(fullFilePath => fullFilePath.Substring(fullFilePath.LastIndexOf(DIRECTORY_SEPARATOR_CHAR, StringComparison.Ordinal)).Remove(0, 1)); // create target directory CreateDirectory(directoryName, targetPath); foreach (string sourceDirectoryFileName in sourceDirectoryFileNames) { using (Stream sourceStream = GetFileStream(sourceDirectoryFileName, sourcePath)) { string[] target = targetPath == null ? new [] { directoryName } : targetPath.Append(directoryName); SetFileStream(sourceDirectoryFileName, sourceStream, target); } } CloudFileDirectory sourceCloudFileDirectory = CloudFileShare.GetDirectoryReference(directoryName, sourcePath); foreach (IListFileItem listFileItem in TaskUtilities.ExecuteSync(sourceCloudFileDirectory.ListFilesAndDirectoriesSegmentedAsync(new FileContinuationToken())).Results) { if (!(listFileItem is CloudFileDirectory cloudFileDirectory)) { continue; } // Get target directory CloudFileDirectory targetCloudFileDirectory = CloudFileShare.GetDirectoryReference(directoryName, targetPath); // Get new sub directory to be created CloudFileDirectory subCloudFileDirectory = targetCloudFileDirectory.GetDirectoryReference(cloudFileDirectory.Name); // Create new sub directory TaskUtilities.ExecuteSync(subCloudFileDirectory.CreateIfNotExistsAsync()); } }
public void CopyFileTest(string sourceFileName, string[] sourceParentFolders, string targetFileName, string[] targetParentFolders, bool overwriteIfExists = false) { // Act _fileContainer.CopyFile(sourceFileName, sourceParentFolders, targetFileName, targetParentFolders, overwriteIfExists); CloudBlockBlob cloudBlockBlobReference = _cloudBlobContainer.GetCloudBlockBlobReference(targetFileName, path: targetParentFolders); bool result = TaskUtilities.ExecuteSync(cloudBlockBlobReference.ExistsAsync()); // Assert Assert.IsTrue(result); }
public void CreateDirectoryTest(string directoryName, string[] path) { // Act _fileContainer.CreateDirectory(directoryName, path); CloudFileDirectory cloudFileDirectory = _cloudFileShare.GetDirectoryReference(directoryName, path); bool result = TaskUtilities.ExecuteSync(cloudFileDirectory.ExistsAsync()); // Assert Assert.IsTrue(result); }
public void CreateFileWithStreamTest(string fileName, string[] path) { // Act _fileContainer.CreateFile(fileName, Stream.Null, path); CloudBlockBlob cloudBlockBlobReference = _cloudBlobContainer.GetCloudBlockBlobReference(fileName, path: path); bool result = TaskUtilities.ExecuteSync(cloudBlockBlobReference.ExistsAsync()); // Assert Assert.IsTrue(result); }
public void DeleteFileTest(string fileName, string[] path) { // Act _fileContainer.DeleteFile(fileName, path); CloudBlockBlob cloudBlockBlobReference = _cloudBlobContainer.GetCloudBlockBlobReference(fileName, path: path); bool result = TaskUtilities.ExecuteSync(cloudBlockBlobReference.ExistsAsync()); // Assert Assert.IsFalse(result); }
/// <summary> /// Enumerates the given directory and filters based on specified searchPattern /// </summary> /// <param name="cloudBlobDirectory">Directory to enumerate on</param> /// <param name="searchPattern">Search pattern to apply</param> /// <returns>Enumerable of strings which contains all found blobs</returns> public static IEnumerable <string> EnumerateDirectory(this CloudBlobDirectory cloudBlobDirectory, string searchPattern) { #region validation if (cloudBlobDirectory == null) { throw new ArgumentNullException(nameof(cloudBlobDirectory)); } if (string.IsNullOrEmpty(searchPattern)) { throw new ArgumentNullException(nameof(searchPattern)); } #endregion var result = new List <string>(); var blobContinuationToken = new BlobContinuationToken(); do { BlobResultSegment blobResultSegment = TaskUtilities.ExecuteSync(cloudBlobDirectory.ListBlobsSegmentedAsync(blobContinuationToken)); foreach (IListBlobItem listBlobItem in blobResultSegment.Results) { // replace wildcard for 'multiple letters' with regex-wildcard string regexeableSearchPattern = searchPattern.Replace("*", ".*"); // replace wildcard for 'single letter' with regex-wildcard regexeableSearchPattern = regexeableSearchPattern.Replace("?", ".?"); // set search pattern in 'begin-to-end'-symbols regexeableSearchPattern = $"^{regexeableSearchPattern}$"; var rgxBlobName = new Regex(regexeableSearchPattern); string itemName = listBlobItem.Uri.AbsolutePath; if (!string.IsNullOrEmpty(itemName) && rgxBlobName.IsMatch(itemName)) { result.Add(listBlobItem.StorageUri.PrimaryUri.ToString()); } } blobContinuationToken = blobResultSegment.ContinuationToken; } while (blobContinuationToken != null); return(result); }
/// <inheritdoc /> public bool ExistsDirectory(string directoryName, string[] path) { #region validation if (string.IsNullOrEmpty(directoryName)) { throw new ArgumentNullException(nameof(directoryName)); } #endregion CloudFileDirectory cloudFileDirectory = CloudFileShare.GetDirectoryReference(directoryName, path); return(TaskUtilities.ExecuteSync(cloudFileDirectory.ExistsAsync())); }
/// <inheritdoc /> /// <summary> /// Downloads the blob from azure blob storage. Usage of this methods requires a separate call of UploadFromStreamAsync() of a blob; /// </summary> /// <param name="fileName">Name of the file to get the write stream from</param> /// <param name="path">Path of the file to get the write stream from</param> /// <returns>A local memory stream.</returns> public Stream GetWriteStream(string fileName, string[] path = null) { #region validation if (string.IsNullOrEmpty(fileName)) { throw new ArgumentNullException(nameof(fileName)); } #endregion CloudBlockBlob cloudBlockBlob = CloudBlobContainer.GetCloudBlockBlobReference(fileName, path: path); return(TaskUtilities.ExecuteSync(cloudBlockBlob.OpenWriteAsync())); }
public void SetFileContentTest(string fileName, string fileContent, string[] path) { // Act _fileContainer.SetFileContent(fileName, fileContent, path); string result = TaskUtilities.ExecuteSync( _cloudFileShare .GetDirectoryReference(path: path) .GetFileReference(fileName) .DownloadTextAsync() ); // Assert Assert.AreEqual(fileContent, result); }
public void DeleteFileTest(string fileName, string[] path) { // Arrange CloudFileDirectory targetDirectory = _cloudFileShare.GetDirectoryReference(path: path); // Act _fileContainer.DeleteFile(fileName, path); CloudFile fileReference = targetDirectory.GetFileReference(fileName); bool result = TaskUtilities.ExecuteSync(fileReference.ExistsAsync()); // Assert Assert.IsFalse(result); }
/// <inheritdoc /> public bool ExistsPath(string[] path) { #region validation if (path == null) { throw new ArgumentNullException(nameof(path)); } #endregion CloudBlobDirectory cloudBlobDirectory = CloudBlobContainer.GetCloudBlobDirectoryReference(path); return(TaskUtilities.ExecuteSync(cloudBlobDirectory.ListBlobsSegmentedAsync(new BlobContinuationToken())).Results.Any()); }
public void CopyFileTest(string sourceFileName, string[] sourceParentFolders, string targetFileName, string[] targetParentFolders, bool overwriteIfExists = false) { // Arrange CloudFileDirectory targetDirectory = _cloudFileShare.GetDirectoryReference(path: targetParentFolders); // Act _fileContainer.CopyFile(sourceFileName, sourceParentFolders, targetFileName, targetParentFolders, overwriteIfExists); CloudFile fileReference = targetDirectory.GetFileReference(targetFileName); bool result = TaskUtilities.ExecuteSync(fileReference.ExistsAsync()); // Assert Assert.IsTrue(result); }
/// <inheritdoc /> public Stream GetFileStream(string fileName, string[] path = null) { #region validation if (string.IsNullOrEmpty(fileName)) { throw new ArgumentNullException(nameof(fileName)); } #endregion CloudFile cloudFile = GetCloudFileReference(fileName, path); return(TaskUtilities.ExecuteSync(cloudFile.OpenReadAsync())); }
/// <inheritdoc /> public void DeleteFile(string fileName, string[] path = null) { #region validation if (string.IsNullOrEmpty(fileName)) { throw new ArgumentNullException(nameof(fileName)); } #endregion CloudBlockBlob cloudBlockBlob = CloudBlobContainer.GetCloudBlockBlobReference(fileName, path: path); TaskUtilities.ExecuteSync(cloudBlockBlob.DeleteIfExistsAsync()); }
public void CreateFileWithByteArrayTest(string fileName, byte[] fileContent, string[] path) { // Arrange CloudFileDirectory targetDirectory = _cloudFileShare.GetDirectoryReference(path: path); // Act _fileContainer.CreateFile(fileName, fileContent, path); CloudFile fileReference = targetDirectory.GetFileReference(fileName); bool result = TaskUtilities.ExecuteSync(fileReference.ExistsAsync()); // Assert Assert.IsTrue(result); }
public void SetFileContentTest(string fileName, string fileContent, string[] path) { // Act _fileContainer.SetFileContent(fileName, fileContent, path); var target = new byte[6]; CloudBlockBlob cloudBlockBlobReference = _cloudBlobContainer.GetCloudBlockBlobReference(fileName, path: path); TaskUtilities.ExecuteSync(cloudBlockBlobReference.DownloadToByteArrayAsync(target, 0)); string result = Encoding.UTF8.GetString(target); // Assert Assert.AreEqual(fileContent, result); }
/// <inheritdoc /> public void DeleteFile(string fileName, string[] path) { #region validation if (string.IsNullOrEmpty(fileName)) { throw new ArgumentNullException(nameof(fileName)); } #endregion CloudFileDirectory currentDir = CloudFileShare.GetDirectoryReference(null, path); CloudFile file = currentDir.GetFileReference(fileName); TaskUtilities.ExecuteSync(file.DeleteIfExistsAsync()); }
/// <inheritdoc /> public string GetFileContent(string fileName, string[] path = null) { #region validation if (string.IsNullOrEmpty(fileName)) { throw new ArgumentNullException(nameof(fileName)); } #endregion CloudFileDirectory cloudFileDirectory = CloudFileShare.GetDirectoryReference(path: path); CloudFile cloudFile = cloudFileDirectory.GetFileReference(fileName); return(TaskUtilities.ExecuteSync(cloudFile.DownloadTextAsync())); }
/// <inheritdoc /> public Stream GetWriteStream(string fileName, string[] path) { #region validation if (string.IsNullOrEmpty(fileName)) { throw new ArgumentNullException(nameof(fileName)); } #endregion CloudFileDirectory currentDir = CloudFileShare.GetDirectoryReference(path: path); CloudFile cloudFile = currentDir.GetFileReference(fileName); return(TaskUtilities.ExecuteSync(cloudFile.OpenWriteAsync(1024 * 1024 * 5))); }
public void ReadAllBytesTest(string fileName, string[] path) { // Arrange var expectedResult = new byte[6]; TaskUtilities.ExecuteSync( _cloudFileShare.GetDirectoryReference(path: path) .GetFileReference(fileName) .DownloadToByteArrayAsync(expectedResult, 0) ); // Act byte[] actualResult = _fileContainer.ReadAllBytes(fileName, path); // Assert CollectionAssert.AreEqual(expectedResult, actualResult); }
public void SetFileStreamTest(string fileName, string[] path) { // Act using (var streamContent = new MemoryStream(Encoding.Default.GetBytes(FILE_CONTENT))) { _fileContainer.SetFileStream(fileName, streamContent, path); } CloudFileDirectory cloudFileDirectory = _cloudFileShare.GetDirectoryReference(path: path); CloudFile fileReference = cloudFileDirectory.GetFileReference(fileName); string result = TaskUtilities.ExecuteSync(fileReference.DownloadTextAsync()); // Assert Assert.AreEqual(FILE_CONTENT, result); }
public void MoveDirectoryTest(string directoryName, string[] sourcePath, string[] targetPath) { // Arrange string relativeAddress = BlobUtilities.GetPath(directoryName, sourcePath); CloudBlobDirectory sourceCloudBlobDirectory = _cloudBlobContainer.GetDirectoryReference(relativeAddress); int expectedChildCount = TaskUtilities .ExecuteSync( sourceCloudBlobDirectory.ListBlobsSegmentedAsync( useFlatBlobListing: true, blobListingDetails: BlobListingDetails.None, maxResults: null, currentToken: new BlobContinuationToken(), options: new BlobRequestOptions(), operationContext: new OperationContext() ) ) .Results.Count(); // Act _fileContainer.MoveDirectory(directoryName, sourcePath, targetPath); string path = BlobUtilities.GetPath(directoryName, targetPath); CloudBlobDirectory targetCloudBlobDirectory = _cloudBlobContainer.GetDirectoryReference(path); int actualChildCount = TaskUtilities .ExecuteSync( targetCloudBlobDirectory.ListBlobsSegmentedAsync( useFlatBlobListing: true, blobListingDetails: BlobListingDetails.None, maxResults: null, currentToken: new BlobContinuationToken(), options: new BlobRequestOptions(), operationContext: new OperationContext() ) ) .Results.Count(); // Assert Assert.IsFalse(_fileContainer.ExistsDirectory(directoryName, sourcePath)); Assert.AreEqual(expectedChildCount, actualChildCount); }
public void CreateFileWithByteArrayTest(string fileName, byte[] fileContent, string[] path) { // Act _fileContainer.CreateFile(fileName, fileContent, path); CloudBlob cloudBlob = _cloudBlobContainer.GetCloudBlockBlobReference(fileName, path: path); TaskUtilities.ExecuteSync(cloudBlob.FetchAttributesAsync()); var actualResult = new byte[cloudBlob.Properties.Length]; CloudBlockBlob cloudBlockBlobReference = _cloudBlobContainer.GetCloudBlockBlobReference(fileName, path: path); TaskUtilities.ExecuteSync(cloudBlockBlobReference.DownloadToByteArrayAsync(actualResult, 0)); // Assert CollectionAssert.AreEqual(fileContent, actualResult); }
public void SetFileStreamTest(string fileName, string[] path) { // Arrange using (var streamContent = new MemoryStream(Encoding.Default.GetBytes(FILE_CONTENT))) { _fileContainer.SetFileStream(fileName, streamContent, path); } var target = new byte[6]; CloudBlockBlob cloudBlockBlobReference = _cloudBlobContainer.GetCloudBlockBlobReference(fileName, path: path); TaskUtilities.ExecuteSync(cloudBlockBlobReference.DownloadToByteArrayAsync(target, 0)); string result = Encoding.UTF8.GetString(target); // Assert Assert.AreEqual(FILE_CONTENT, result); }
private static IEnumerable <CloudFileDirectory> ListSubDirectories(this CloudFileDirectory cloudFileDirectory) { #region validation if (cloudFileDirectory == null) { throw new ArgumentNullException(nameof(cloudFileDirectory)); } #endregion var subDirectories = new List <CloudFileDirectory>(); FileResultSegment fileResultSegment; do { fileResultSegment = TaskUtilities.ExecuteSync(cloudFileDirectory.ListFilesAndDirectoriesSegmentedAsync(new FileContinuationToken())); foreach (IListFileItem listFileItem in fileResultSegment.Results) { switch (listFileItem) { case CloudFile cloudFile: TaskUtilities.ExecuteSync(cloudFile.DeleteAsync()); break; case CloudFileDirectory subDirectory: subDirectories.Add(subDirectory); subDirectories.AddRange(subDirectory.ListSubDirectories()); break; } } } while (fileResultSegment.ContinuationToken != null); return(subDirectories); }
public void Initialize() { _fileContainer = new AzureBlobContainer(CONNECTION_STRING, CONTAINER_NAME); _cloudStorageAccount = CloudStorageAccount.Parse(CONNECTION_STRING); _cloudBlobContainer = _cloudStorageAccount.CreateCloudBlobClient().GetContainerReference(CONTAINER_NAME); // SourceDirectory/CopyFile.txt TaskUtilities.ExecuteSync(_cloudBlobContainer.GetCloudBlockBlobReference(FILE_NAME, SOURCE_DIRECTORY).UploadTextAsync(FILE_CONTENT)); // SourceDirectory/DummyDirectory/CopyFile.txt TaskUtilities.ExecuteSync(_cloudBlobContainer.GetCloudBlockBlobReference(FILE_NAME, DUMMY_DIRECTORY, new[] { SOURCE_DIRECTORY }).UploadTextAsync(FILE_CONTENT)); // TargetDirectory/CopyFile.txt TaskUtilities.ExecuteSync(_cloudBlobContainer.GetCloudBlockBlobReference(FILE_NAME, TARGET_DIRECTORY).UploadTextAsync(FILE_CONTENT)); // CopyFile.txt TaskUtilities.ExecuteSync(_cloudBlobContainer.GetCloudBlockBlobReference(FILE_NAME).UploadTextAsync(FILE_CONTENT)); }