/// <summary> /// Starts all of the necessary operations to rename the directory <paramref name="oldLeafName"/> to /// the <paramref name="newLeafName"/> name. This includes renaming the directory and all subdirectories and files /// under the directory being renamed. /// </summary> /// <param name="bucket">The bucket where the directory to rename resides.</param> /// <param name="parentName">The parent directory of the directory being renamed.</param> /// <param name="oldLeafName">The old (current) name of the directory.</param> /// <param name="newLeafName">The new name for the directory.</param> /// <param name="cancellationToken">The cancellation token for the operaitons.</param> /// <returns></returns> public async Task <OperationsQueue> StartDirectoryRenameOperationsAsync( string bucket, string parentName, string oldLeafName, string newLeafName, CancellationToken cancellationToken) { var oldNamePrefix = GcsPathUtils.Combine(parentName, oldLeafName) + "/"; var newNamePrefix = GcsPathUtils.Combine(parentName, newLeafName) + "/"; var filesToMove = await GetGcsFilesFromPrefixAsync(bucket, oldNamePrefix); var moveOperations = new List <GcsMoveFileOperation>(); foreach (var file in filesToMove) { var movedName = newNamePrefix + file.Name.Substring(oldNamePrefix.Length); var movedItem = new GcsItemRef(bucket, movedName); moveOperations.Add(new GcsMoveFileOperation(file, movedItem)); } var operationsQueue = new OperationsQueue(cancellationToken); operationsQueue.EnqueueOperations(moveOperations, _dataSource.StartMoveOperation); operationsQueue.StartOperations(); return(operationsQueue); }
/// <summary> /// Starts the file upload operations for the given file sources, does not wait for the operations to complete. /// </summary> /// <param name="sources">The files, or directories, to upload.</param> /// <param name="bucket">The destination bucket.</param> /// <param name="bucketPath">The destination path in the bucket.</param> /// <param name="cancellationToken">The cancellation token source to use for the operations.</param> /// <returns>The list of operations started.</returns> public OperationsQueue StartUploadOperations( IEnumerable <string> sources, string bucket, string bucketPath, CancellationToken cancellationToken) { var uploadOperations = CreateUploadOperations(sources, bucket: bucket, bucketPath: bucketPath); var operationsQueue = new OperationsQueue(cancellationToken); operationsQueue.EnqueueOperations(uploadOperations, _dataSource.StartFileUploadOperation); operationsQueue.StartOperations(); return(operationsQueue); }
/// <summary> /// Starts a set of download operations for files and directories stored on GCS. It will enumerate /// all of the directories and subdirectories specified as well as all of the files stored. /// </summary> /// <param name="sources">The list of files or directories to download.</param> /// <param name="destinationDir">Where to store the downloaded files.</param> /// <param name="cancellationToken">The cancellation token for the operation.</param> /// <returns>The list of operations started.</returns> public async Task <OperationsQueue> StartDownloadOperationsAsync( IEnumerable <GcsItemRef> sources, string destinationDir, CancellationToken cancellationToken) { List <GcsFileTransferOperation> downloadOperations = new List <GcsFileTransferOperation>( sources .Where(x => x.IsFile) .Select(x => new GcsFileTransferOperation( localPath: Path.Combine(destinationDir, GcsPathUtils.GetFileName(x.Name)), gcsItem: x))); // Collect all of the files from all of the directories to download, collects all of the // local file system directories that need to be created to mirror the source structure. var subDirs = new HashSet <string>(); foreach (var dir in sources.Where(x => x.IsDirectory)) { var files = await GetGcsFilesFromPrefixAsync(dir.Bucket, dir.Name); foreach (var file in files) { var relativeFilePath = Path.Combine( GcsPathUtils.GetFileName(dir.Name), GetRelativeName(file.Name, dir.Name).Replace('/', '\\')); var absoluteFilePath = Path.Combine(destinationDir, relativeFilePath); // Create the file operation for this file. downloadOperations.Add(new GcsFileTransferOperation( localPath: absoluteFilePath, gcsItem: file)); // Collects the list of directories to create. subDirs.Add(Path.GetDirectoryName(absoluteFilePath)); } } // Create all of the directories. foreach (var dir in subDirs) { Directory.CreateDirectory(dir); } var operationsQueue = new OperationsQueue(cancellationToken); operationsQueue.EnqueueOperations(downloadOperations, _dataSource.StartFileDownloadOperation); operationsQueue.StartOperations(); return(operationsQueue); }
/// <summary> /// Enumerates all of the files and directories and starts the operations to delete them. /// </summary> /// <param name="sources">The list of sources to delete.</param> /// <param name="cancellationToken">The cancellation token to use for the operations.</param> /// <returns>The list of operations started.</returns> public async Task <OperationsQueue> StartDeleteOperationsAsync( IEnumerable <GcsItemRef> sources, CancellationToken cancellationToken) { var deleteOperations = new List <GcsDeleteFileOperation>(sources .Where(x => x.IsFile) .Select(x => new GcsDeleteFileOperation(x))); foreach (var dir in sources.Where(x => x.IsDirectory)) { var filesInPrefix = await GetGcsFilesFromPrefixAsync(dir.Bucket, dir.Name); deleteOperations.AddRange(filesInPrefix.Select(x => new GcsDeleteFileOperation(x))); } var operationsQueue = new OperationsQueue(cancellationToken); operationsQueue.EnqueueOperations(deleteOperations, _dataSource.StartDeleteOperation); operationsQueue.StartOperations(); return(operationsQueue); }