Ejemplo n.º 1
0
        public async Task <BlobElement[]> ListAsync(string path, CancellationToken cancellationToken = default)
        {
            await _client.AutoConnectAsync(cancellationToken).ConfigureAwait(false);

            try
            {
                var items = await _client.GetListingAsync(path).ConfigureAwait(false);

                return(items
                       .Select(i => i.Type == FtpFileSystemObjectType.Directory ?
                               BlobElement.CreateContainer(i.FullName, i.Name) :
                               new BlobElement(i.FullName, i.Name, BlobElementType.Blob, i.Size, i.Created.ToUniversalTime(), i.Modified.ToUniversalTime()))
                       .ToArray());
            }
            catch (FtpCommandException e) when(e.CompletionCode == "550")
            {
                throw new ContainerNotFoundException(path, e);
            }
        }
Ejemplo n.º 2
0
        public async Task <BlobElement[]> ListAsync(string path, CancellationToken cancellationToken = default)
        {
            var pathSegments = PathUtilities.GetSegments(path);

            if (pathSegments.Length == 0)
            {
                var cloudBlobClient = _storageAccount.CreateCloudBlobClient();

                BlobContinuationToken continuationToken = null;
                var results = new List <BlobElement>();
                do
                {
                    var response = await cloudBlobClient.ListContainersSegmentedAsync(continuationToken).ConfigureAwait(false);

                    continuationToken = response.ContinuationToken;
                    results.AddRange(response.Results.Select(c => BlobElement.CreateContainer(c.Name)));
                }while (continuationToken != null);

                return(results.ToArray());
            }
            else
            {
                var containerName = pathSegments.First();
                var containerPath = string.Join(PathUtilities.Delimiter, pathSegments.Skip(1));
                var container     = await GetCloudBlobContainerAsync(containerName, cancellationToken).ConfigureAwait(false);

                BlobContinuationToken continuationToken = null;
                var results = new List <BlobElement>();
                do
                {
                    var response = pathSegments.Skip(1).Any() ?
                                   await container.ListBlobsSegmentedAsync(containerPath + PathUtilities.Delimiter, continuationToken).ConfigureAwait(false) :
                                   await container.ListBlobsSegmentedAsync(continuationToken).ConfigureAwait(false);

                    continuationToken = response.ContinuationToken;
                    results.AddRange(response.Results.Select(i =>
                    {
                        if (i is CloudBlobDirectory directory)
                        {
                            return(BlobElement.CreateContainer(directory.Prefix, PathUtilities.GetSegments(directory.Prefix).Last()));
                        }
                        else if (i is CloudBlob blob)
                        {
                            var blobNameSegments = blob.Name.Split(PathUtilities.DelimiterChar);
                            return(new BlobElement(
                                       blob.Name,
                                       string.Join(PathUtilities.Delimiter, blobNameSegments.Skip(blobNameSegments.Length - 1)),
                                       BlobElementType.Blob,
                                       blob.Properties.Length,
                                       blob.Properties.Created,
                                       blob.Properties.LastModified));
                        }
                        else
                        {
                            return(null);
                        }
                    }).Where(c => c != null));
                }while (continuationToken != null);

                if (results.Count == 0)
                {
                    throw new ContainerNotFoundException(path, null);
                }

                return(results.ToArray());
            }
        }