/// <summary> /// Copies a Google Cloud Storage object or folder to another object or folder. Used by Copy-Item. /// </summary> /// <param name="path">The path to copy from.</param> /// <param name="copyPath">The path to copy to.</param> /// <param name="recurse">If true, will copy all decendent objects as well.</param> protected override void CopyItem(string path, string copyPath, bool recurse) { if (!ShouldProcess($"Copy-Item from {path} to {copyPath}")) { return; } var dyanmicParameters = (GcsCopyItemDynamicParameters)DynamicParameters; if (recurse) { path = path.TrimEnd('\\') + "\\"; copyPath = copyPath.TrimEnd('\\') + "\\"; } var gcsPath = GcsPath.Parse(path); var gcsCopyPath = GcsPath.Parse(copyPath); if (recurse) { IEnumerable <Object> children = ListChildren(gcsPath, true); foreach (Object child in children) { string objectSubPath = gcsPath.RelativePathToChild(child.Name); string destinationObject = GcsPath.Parse(MakePath(copyPath, objectSubPath)).ObjectPath; ObjectsResource.CopyRequest childRequest = Service.Objects.Copy(null, child.Bucket, child.Name, gcsCopyPath.Bucket, destinationObject); childRequest.SourceGeneration = dyanmicParameters.SourceGeneration; childRequest.DestinationPredefinedAcl = dyanmicParameters.DestinationAcl; childRequest.Projection = ObjectsResource.CopyRequest.ProjectionEnum.Full; Object childObject = childRequest.Execute(); bool isContainer = (new GcsPath(childObject).Type != GcsPath.GcsPathType.Object); WriteItemObject(childObject, copyPath, isContainer); } } if (!recurse || GetBucketModel(gcsPath.Bucket).IsReal(gcsPath.ObjectPath)) { ObjectsResource.CopyRequest request = Service.Objects.Copy(null, gcsPath.Bucket, gcsPath.ObjectPath, gcsCopyPath.Bucket, gcsCopyPath.ObjectPath); request.SourceGeneration = dyanmicParameters.SourceGeneration; request.DestinationPredefinedAcl = dyanmicParameters.DestinationAcl; request.Projection = ObjectsResource.CopyRequest.ProjectionEnum.Full; Object response = request.Execute(); WriteItemObject(response, copyPath, gcsCopyPath.Type != GcsPath.GcsPathType.Object); } BucketModels.Clear(); TelemetryReporter.ReportSuccess(nameof(GoogleCloudStorageProvider), nameof(CopyItem)); }
protected override void ProcessRecord() { Object gcsObject; switch (ParameterSetName) { case ParameterSetNames.ByName: gcsObject = Service.Objects.Get(SourceBucket, SourceObjectName).Execute(); break; case ParameterSetNames.ByObject: gcsObject = InputObject; break; default: throw UnknownParameterSetException; } string destinationBucket = DestinationBucket ?? gcsObject.Bucket; string destinationObject = DestinationObjectName ?? gcsObject.Name; if (!Force) { try { ObjectsResource.GetRequest objGetReq = Service.Objects.Get(destinationBucket, destinationObject); objGetReq.Execute(); // If destination does not exist, jump to catch statment. if (!ShouldContinue( "Object exists. Overwrite?", $"{destinationBucket}/{destinationObject}")) { return; } } catch (GoogleApiException ex) when(ex.Error.Code == 404) { } } ObjectsResource.CopyRequest request = Service.Objects.Copy(gcsObject, gcsObject.Bucket, gcsObject.Name, destinationBucket, destinationObject); Object response = request.Execute(); WriteObject(response); }