示例#1
0
        /// <summary>
        /// Get the reference of a cloud block blob.
        /// </summary>
        /// <param name="cloudBlobContainer">The container to get the blob reference from</param>
        /// <param name="blobName">Name of the desired blob</param>
        /// <param name="directoryName">Name of the virtual directory</param>
        /// <param name="path">Path of the virtual directory / blob</param>
        /// <returns></returns>
        public static CloudBlockBlob GetCloudBlockBlobReference(this CloudBlobContainer cloudBlobContainer, string blobName, string directoryName = null, string[] path = null)
        {
            #region validation

            if (cloudBlobContainer == null)
            {
                throw new ArgumentNullException(nameof(cloudBlobContainer));
            }

            if (string.IsNullOrEmpty(blobName))
            {
                throw new ArgumentNullException(blobName);
            }

            #endregion

            string fullBlobPath;

            if (string.IsNullOrEmpty(directoryName))
            {
                fullBlobPath = BlobUtilities.GetPath(blobName, path);
            }
            else
            {
                string fullDirectoryPath = BlobUtilities.GetPath(directoryName, path);

                fullBlobPath = BlobUtilities.GetPath(blobName, fullDirectoryPath);
            }

            return(cloudBlobContainer.GetBlockBlobReference(fullBlobPath));
        }
示例#2
0
        /// <summary>
        /// Returns a cloud blob directory reference at a specified location
        /// </summary>
        /// <param name="cloudBlobContainer">Container reference to get the directory reference from</param>
        /// <param name="path">Path to the cloud blob directory</param>
        /// <returns>Cloud blob directory reference at the desired location</returns>
        public static CloudBlobDirectory GetCloudBlobDirectoryReference(this CloudBlobContainer cloudBlobContainer, string[] path = null)
        {
            #region validation

            if (cloudBlobContainer == null)
            {
                throw new ArgumentNullException(nameof(cloudBlobContainer));
            }
            #endregion

            string fullPath = BlobUtilities.GetPath(path);

            return(cloudBlobContainer.GetDirectoryReference(fullPath));
        }
示例#3
0
        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);
        }
        /// <inheritdoc />
        public void CopyDirectory(string directoryName, string[] sourcePath, string[] targetPath)
        {
            #region validation

            if (string.IsNullOrEmpty(directoryName))
            {
                throw new ArgumentNullException(nameof(directoryName));
            }

            #endregion

            if (sourcePath.IsEqualTo(targetPath))
            {
                // return if source and target are the same directory
                // nothing to do here
                return;
            }

            // Identify sub virtual directories through evaluation of path segments for each blob reference
            foreach (var listBlobItem in BlobContainerClient.GetBlobs(prefix: BlobUtilities.GetPath(directoryName, GetContainerPath(sourcePath))))
            {
                // Get the new blob reference as BlockBlob so we can use the upload text method
                BlockBlobClient newCloudBlockBlob = GetCloudBlockBlobReference(listBlobItem.Name[(listBlobItem.Name.LastIndexOf(DIRECTORY_SEPARATOR_CHAR + directoryName + DIRECTORY_SEPARATOR_CHAR) + directoryName.Length + 2)..], directoryName, targetPath);
示例#5
0
        /// <summary>
        /// Builds the full qualified uri for a new blob
        /// </summary>
        /// <param name="cloudBlob">The blob reference to build the uri for</param>
        /// <param name="targetName">The name of the target directory</param>
        /// <param name="targetPath">The target path of the new blob</param>
        /// <returns>Full qualified uri for a new blob</returns>
        public static string GetTargetUri(this CloudBlob cloudBlob, string targetName, string[] targetPath)
        {
            string basePath = $"https://{cloudBlob.Uri.Authority}/{cloudBlob.Container.Name}/{BlobUtilities.GetPath(targetName, targetPath)}/{cloudBlob.Uri.Segments.Last()}";

            return(basePath);
        }
示例#6
0
        /// <inheritdoc />
        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

            // Source directory
            CloudBlobDirectory sourceCloudBlobDirectory = CloudBlobContainer.GetCloudBlobDirectoryReference(directoryName, sourcePath);

            // Get blobs and directories on current level
            IEnumerable <IListBlobItem> sourceListBlobItems = sourceCloudBlobDirectory
                                                              .EnumerateDirectory("*")
                                                              .Select <string, IListBlobItem>(
                listBlobItemName =>
            {
                if (listBlobItemName.EndsWith(DIRECTORY_SEPARATOR_CHAR))
                {
                    return(CloudBlobContainer.GetDirectoryReference(listBlobItemName));
                }

                return(CloudBlobContainer.GetBlobReference(listBlobItemName));
            });


            // Identify sub virtual directories through evaluation of path segments for each blob reference
            foreach (IListBlobItem listBlobItem in sourceListBlobItems)
            {
                string[] subSource = BlobUtilities.GetPath(directoryName, sourcePath).Split(DIRECTORY_SEPARATOR_CHAR);

                string[] subTarget = BlobUtilities.GetPath(directoryName, targetPath).Split(DIRECTORY_SEPARATOR_CHAR);

                switch (listBlobItem)
                {
                case CloudBlob cloudBlob:
                    // Get the new blob reference as BlockBlob so we can use the upload text method
                    CloudBlockBlob newCloudBlockBlob = CloudBlobContainer.GetCloudBlockBlobReference(cloudBlob.Uri.Segments.Last(), directoryName, targetPath);

                    // Create the blob
                    TaskUtilities.ExecuteSync(newCloudBlockBlob.UploadTextAsync(string.Empty));

                    // Get the new uri
                    var source = new Uri(cloudBlob.GetTargetUri(directoryName, sourcePath));

                    // Copy the blob content
                    TaskUtilities.ExecuteSync(newCloudBlockBlob.StartCopyAsync(source));

                    break;

                case CloudBlobDirectory cloudBlobDirectory:
                    string lastSegment = cloudBlobDirectory.Uri.Segments.Last();

                    string subDirectoryName = lastSegment.Remove(lastSegment.LastIndexOf(DIRECTORY_SEPARATOR_CHAR));

                    CopyDirectory(subDirectoryName, subSource, subTarget);

                    break;
                }
            }
        }