Esempio n. 1
0
        public static Blob ToBlob(string containerName, IListBlobItem nativeBlob)
        {
            string GetFullName(string name) => containerName == null
               ? name
               : StoragePath.Combine(containerName, name);

            Blob blob = nativeBlob switch
            {
                CloudBlockBlob blockBlob => new Blob(GetFullName(blockBlob.Name), BlobItemKind.File),
                CloudAppendBlob appendBlob => new Blob(GetFullName(appendBlob.Name), BlobItemKind.File),
                CloudBlob cloudBlob => new Blob(GetFullName(cloudBlob.Name), BlobItemKind.File),
                CloudBlobDirectory dirBlob => new Blob(GetFullName(dirBlob.Prefix), BlobItemKind.Folder),
                _ => throw new InvalidOperationException($"unknown item type {nativeBlob.GetType()}")
            };

            //attach metadata if we can
            if (nativeBlob is CloudBlob metaBlob)
            {
                //no need to fetch attributes, parent request includes the details
                //await cloudBlob.FetchAttributesAsync().ConfigureAwait(false);
                AzConvert.AttachBlobMeta(blob, metaBlob);
            }

            return(blob);
        }
        private async Task <Blob> GetBlobAsync(string fullPath, CancellationToken cancellationToken)
        {
            (CloudBlobContainer container, string path) = await GetPartsAsync(fullPath, false).ConfigureAwait(false);

            if (container == null)
            {
                return(null);
            }

            if (string.IsNullOrEmpty(path))
            {
                //looks like it's a container reference
                await container.FetchAttributesAsync().ConfigureAwait(false);

                return(AzConvert.ToBlob(container));
            }
            else
            {
                CloudBlob blob = container.GetBlobReference(StoragePath.Normalize(path, false));
                if (!(await blob.ExistsAsync().ConfigureAwait(false)))
                {
                    return(null);
                }

                var r = new Blob(fullPath);
                await blob.FetchAttributesAsync().ConfigureAwait(false);

                AttachBlobMeta(r, blob);
                return(r);
            }
        }
Esempio n. 3
0
        public async Task <IReadOnlyCollection <Blob> > ListFolderAsync(ListOptions options, CancellationToken cancellationToken)
        {
            var result = new List <Blob>();

            await foreach (BlobHierarchyItem item in
                           _client.GetBlobsByHierarchyAsync(
                               delimiter: options.Recurse ? null : "/",
                               prefix: FormatFolderPrefix(options.FolderPath),
                               traits: options.IncludeAttributes ? BlobTraits.Metadata : BlobTraits.None).ConfigureAwait(false))
            {
                Blob blob = AzConvert.ToBlob(_prependContainerName ? _client.Name : null, item);

                if (options.IsMatch(blob) && (options.BrowseFilter == null || options.BrowseFilter(blob)))
                {
                    result.Add(blob);
                }
            }

            if (options.Recurse)
            {
                AssumeImplicitPrefixes(
                    _prependContainerName ? StoragePath.Combine(_client.Name, options.FolderPath) : options.FolderPath,
                    result);
            }

            return(result);
        }
Esempio n. 4
0
        protected virtual async Task <Blob> GetBlobAsync(string fullPath, CancellationToken cancellationToken)
        {
            (BlobContainerClient container, string path) = await GetPartsAsync(fullPath, false).ConfigureAwait(false);

            if (container == null)
            {
                return(null);
            }

            if (string.IsNullOrEmpty(path))
            {
                //it's a container

                Response <BlobContainerProperties> attributes = await container.GetPropertiesAsync(cancellationToken : cancellationToken).ConfigureAwait(false);

                return(AzConvert.ToBlob(container.Name, attributes));
            }

            BlobClient client = container.GetBlobClient(path);

            try
            {
                Response <BlobProperties> properties = await client.GetPropertiesAsync(cancellationToken : cancellationToken).ConfigureAwait(false);

                return(AzConvert.ToBlob(_containerName, path, properties));
            }
            catch (RequestFailedException ex) when(ex.ErrorCode == "BlobNotFound")
            {
                return(null);
            }
        }
Esempio n. 5
0
        private async Task ListFolderAsync(List <Blob> container, string path, ListOptions options, CancellationToken cancellationToken)
        {
            CloudBlobDirectory dir = GetCloudBlobDirectory(path);

            BlobContinuationToken token = null;

            var batch = new List <Blob>();

            await _throttler.WaitAsync();

            try
            {
                do
                {
                    BlobResultSegment segment = await dir.ListBlobsSegmentedAsync(
                        false,
                        //automatically include metadata in the response
                        options.IncludeAttributes?BlobListingDetails.Metadata : BlobListingDetails.None,
                        null, token, null, null, cancellationToken).ConfigureAwait(false);

                    token = segment.ContinuationToken;

                    foreach (IListBlobItem listItem in segment.Results)
                    {
                        Blob blob = AzConvert.ToBlob(_prependContainerName ? _container.Name : null, listItem);

                        if (options.IsMatch(blob) && (options.BrowseFilter == null || options.BrowseFilter(blob)))
                        {
                            batch.Add(blob);
                        }
                    }
                }while (token != null && ((options.MaxResults == null) || (container.Count + batch.Count < options.MaxResults.Value)));
            }
            finally
            {
                _throttler.Release();
            }

            batch = batch.Where(options.IsMatch).ToList();
            if (options.Add(container, batch))
            {
                return;
            }

            if (options.Recurse)
            {
                var folderIds = batch.Where(r => r.Kind == BlobItemKind.Folder).ToList();

                await Task.WhenAll(
                    folderIds.Select(folderId => ListFolderAsync(
                                         container,
                                         StoragePath.Combine(path, folderId.Name),
                                         options,
                                         cancellationToken))).ConfigureAwait(false);
            }
        }
        public async Task <Blob> CreateSnapshotAsync(string fullPath, CancellationToken cancellationToken)
        {
            (CloudBlobContainer container, string path) = await GetPartsAsync(fullPath).ConfigureAwait(false);

            CloudBlockBlob blob = container.GetBlockBlobReference(path);

            CloudBlob snapshot = await blob.SnapshotAsync(cancellationToken);

            return(AzConvert.ToBlob(_containerName == null ? container.Name : null, snapshot));

            //BlobResultSegment snaps = await container.ListBlobsSegmentedAsync(path, true, BlobListingDetails.Snapshots, null, null, null, null, cancellationToken);
        }
Esempio n. 7
0
        protected override async Task <Blob> GetBlobAsync(string fullPath, CancellationToken cancellationToken)
        {
            CloudFile file = await GetFileReferenceAsync(fullPath, false, cancellationToken).ConfigureAwait(false);

            try
            {
                await file.FetchAttributesAsync(cancellationToken).ConfigureAwait(false);

                return(AzConvert.ToBlob(StoragePath.GetParent(fullPath), file));
            }
            catch (AzStorageException ex) when(ex.RequestInformation.ErrorCode == "ShareNotFound")
            {
                return(null);
            }
            catch (AzStorageException ex) when(ex.RequestInformation.ErrorCode == "ResourceNotFound")
            {
                return(null);
            }
        }
Esempio n. 8
0
        protected override async Task <IReadOnlyCollection <Blob> > ListAtAsync(
            string path, ListOptions options, CancellationToken cancellationToken)
        {
            if (StoragePath.IsRootPath(path))
            {
                //list file shares

                ShareResultSegment shares = await _client.ListSharesSegmentedAsync(null, cancellationToken).ConfigureAwait(false);

                return(shares.Results.Select(AzConvert.ToBlob).ToList());
            }
            else
            {
                var chunk = new List <Blob>();

                CloudFileDirectory dir = await GetDirectoryReferenceAsync(path, cancellationToken).ConfigureAwait(false);

                FileContinuationToken token = null;
                do
                {
                    try
                    {
                        FileResultSegment segment = await dir.ListFilesAndDirectoriesSegmentedAsync(options.FilePrefix, token, cancellationToken).ConfigureAwait(false);

                        token = segment.ContinuationToken;

                        chunk.AddRange(segment.Results.Select(r => AzConvert.ToBlob(path, r)));
                    }
                    catch (AzStorageException ex) when(ex.RequestInformation.ErrorCode == "ShareNotFound")
                    {
                        break;
                    }
                    catch (AzStorageException ex) when(ex.RequestInformation.ErrorCode == "ResourceNotFound")
                    {
                        break;
                    }
                }while(token != null);

                return(chunk);
            }
        }