Example #1
0
        /// <inheritdoc />
        public IEnumerable <string> GetFiles(string[] path = null)
        {
            CloudBlobDirectory cloudBlobDirectory = CloudBlobContainer.GetCloudBlobDirectoryReference(path);

            List <string> fullBlobPaths = cloudBlobDirectory.EnumerateDirectory(EBlobType.Blobs, "*").ToList();

            return(fullBlobPaths);
        }
Example #2
0
        /// <inheritdoc />
        public IEnumerable <string> GetFileNames(string searchPattern, string[] path = null)
        {
            CloudBlobDirectory cloudBlobDirectory = CloudBlobContainer.GetCloudBlobDirectoryReference(path);

            IEnumerable <string> fullFilePaths = cloudBlobDirectory.EnumerateDirectory(EBlobType.Blobs, searchPattern);

            return(fullFilePaths.Select(fullFilePath => fullFilePath.Split(DIRECTORY_SEPARATOR_CHAR).Last()));
        }
Example #3
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;
                }
            }
        }