示例#1
0
        private async Task UploadFile(AsyncLimiter limiter, string localPath)
        {
            using (await limiter.AcquireOneAsync())
            {
                using (Stream source = File.OpenRead(localPath))
                {
                    try
                    {
                        string localRelativePath = localPath.Substring(_rootSourceFolder.Length);
                        string destPath          = StoragePath.Combine(_targetRootPath, localRelativePath.Replace(Path.DirectorySeparatorChar, StoragePath.PathSeparator));

                        await _storage.WriteAsync(destPath, source);

                        _copied += new FileInfo(localPath).Length;
                        SendUpdate();
                    }
                    catch (Exception ex)
                    {
                        _failedBlobs[localPath] = ex;
                    }
                }

                _filesCopied += 1;
            }

            SendUpdate();
        }
示例#2
0
        private async Task CopyBlobAsync(Blob blob, AsyncLimiter limiter)
        {
            string destPath = blob.FullPath.Substring(_sourceRoot.Length);

            destPath = StoragePath.Combine(_destinationRootPath, destPath);

            try
            {
                using (await limiter.AcquireOneAsync())
                {
                    using (Stream src = await _sourceStorage.OpenReadAsync(blob))
                    {
                        if (src != null)
                        {
                            await _destinationStorage.WriteAsync(destPath, src);

                            _bytesComplete += blob.Size.Value;
                            UpdateProgress(_bytesComplete, _totalSizeBytes);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _failedBlobs.Add(blob);
                Log.Error(ex, "filed to copy");
            }

            _filesComplete += 1;
            Message         = $"{_filesComplete} of {"blob".ToQuantity(_allSourceBlobs.Count)} copied";
        }
        private async Task ListFolderAsync(List <Blob> container, string path, ListOptions options, CancellationToken cancellationToken)
        {
            var request = new ListObjectsV2Request()
            {
                BucketName = _bucketName,
                Prefix     = FormatFolderPrefix(path),
                Delimiter  = "/" //this tells S3 not to go into the folder recursively
            };

            var folderContainer = new List <Blob>();

            while (options.MaxResults == null || (container.Count < options.MaxResults))
            {
                ListObjectsV2Response response;

                using (await _limiter.AcquireOneAsync().ConfigureAwait(false))
                {
                    response = await _client.ListObjectsV2Async(request, cancellationToken).ConfigureAwait(false);
                }

                folderContainer.AddRange(response.ToBlobs(options));

                if (response.NextContinuationToken == null)
                {
                    break;
                }

                request.ContinuationToken = response.NextContinuationToken;
            }

            container.AddRange(folderContainer);

            if (options.Recurse)
            {
                List <Blob> folders = folderContainer.Where(b => b.Kind == BlobItemKind.Folder).ToList();

                await Task.WhenAll(folders.Select(f => ListFolderAsync(container, f.FullPath, options, cancellationToken))).ConfigureAwait(false);
            }
        }