private static IEnumerable <GcsFileTransferOperation> CreateUploadOPerationsForPath( string bucket, string bucketPath, string src) { var info = new FileInfo(src); var isDirectory = (info.Attributes & FileAttributes.Directory) != 0; if (isDirectory) { return(CreateUploadOperationsForDirectory( sourceDir: info.FullName, bucket: bucket, baseGcsPath: GcsPathUtils.Combine(bucketPath, info.Name))); } else { return(new GcsFileTransferOperation[] { new GcsFileTransferOperation( localPath: info.FullName, gcsItem: new GcsItemRef(bucket: bucket, name: GcsPathUtils.Combine(bucketPath, info.Name))) }); } }
/// <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> /// Creates the <seealso cref="GcsFileTransferOperation"/> instances for all of the files in the given directory. The /// target directory will be based on <paramref name="baseGcsPath"/>. /// </summary> /// <param name="sourceDir">The local dir to process.</param> /// <param name="bucket">The name of the bucket.</param> /// <param name="baseGcsPath">The base gcs path where to copy the files.</param> private static IEnumerable <GcsFileTransferOperation> CreateUploadOperationsForDirectory( string sourceDir, string bucket, string baseGcsPath) { var fileOperations = Directory.EnumerateFiles(sourceDir) .Select(file => new GcsFileTransferOperation( localPath: file, gcsItem: new GcsItemRef( bucket: bucket, name: GcsPathUtils.Combine(baseGcsPath, Path.GetFileName(file))))); var directoryOperations = Directory.EnumerateDirectories(sourceDir) .Select(subDir => CreateUploadOperationsForDirectory( subDir, bucket: bucket, baseGcsPath: GcsPathUtils.Combine(baseGcsPath, Path.GetFileName(subDir)))) .SelectMany(x => x); return(Enumerable.Concat(fileOperations, directoryOperations)); }