/// <summary> /// Checks if the given object is a real object. An object could "exist" but not be "real" if it is a /// prefix for another object (a logical folder that is not "real"). /// </summary> /// <param name="objectName">The name of the object to check.</param> /// <returns>True if the object actually exists.</returns> public bool IsReal(string objectName) { if (_objectMap.ContainsKey(objectName)) { return(true); } else if (!_pageLimited) { return(false); } else { try { ObjectsResource.GetRequest request = _service.Objects.Get(_bucket, objectName); request.Projection = ObjectsResource.GetRequest.ProjectionEnum.Full; Object gcsObject = request.Execute(); _objectMap[gcsObject.Name] = gcsObject; return(true); } catch (GoogleApiException e) { if (e.HttpStatusCode == HttpStatusCode.NotFound) { return(false); } else { throw; } } } }
/// <summary> /// Checks if an object exists. If the model did not read all of the objects during its last update, it /// may make a service call if the object is not found in its data. /// </summary> public bool ObjectExists(string objectName) { if (_pageLimited) { if (_objectMap.ContainsKey(objectName)) { return(_objectMap[objectName] != null); } else if (_prefixes.ContainsKey(objectName.TrimEnd('/'))) { return(true); } else { try { ObjectsResource.GetRequest request = _service.Objects.Get(_bucket, objectName); request.Projection = ObjectsResource.GetRequest.ProjectionEnum.Full; _objectMap[objectName] = request.Execute(); return(true); } catch (GoogleApiException e) when(e.HttpStatusCode == HttpStatusCode.NotFound) { _objectMap[objectName] = null; return(false); } } } return(_objectMap.ContainsKey(objectName) || _prefixes.ContainsKey(objectName.TrimEnd('/'))); }
/// <summary> /// Gets the Google Cloud Storage object of a given object name. /// </summary> public Object GetGcsObject(string objectName) { if (_objectMap.ContainsKey(objectName)) { return(_objectMap[objectName]); } else if (_prefixes.ContainsKey(objectName)) { if (_objectMap.ContainsKey(objectName + Separator)) { return(_objectMap[objectName + Separator]); } else { return(new Object { Bucket = _bucket, Name = objectName, ContentType = "Folder" }); } } else { ObjectsResource.GetRequest request = _service.Objects.Get(_bucket, objectName); request.Projection = ObjectsResource.GetRequest.ProjectionEnum.Full; Object gcsObject = request.Execute(); _objectMap[gcsObject.Name] = gcsObject; return(gcsObject); } }
protected override void ProcessRecord() { base.ProcessRecord(); ObjectsResource.GetRequest getReq = Service.Objects.Get(Bucket, ObjectName); getReq.Projection = ObjectsResource.GetRequest.ProjectionEnum.Full; Object gcsObject = getReq.Execute(); WriteObject(gcsObject); }
/// <summary> /// Returns whether or not a storage object with the given name exists in the provided /// bucket. Will return false if the object exists but is not visible to the current /// user. /// </summary> protected bool TestObjectExists(StorageService service, string bucket, string objectName) { try { ObjectsResource.GetRequest getReq = service.Objects.Get(bucket, objectName); getReq.Execute(); return(true); } catch (GoogleApiException ex) when(ex.HttpStatusCode == HttpStatusCode.NotFound) { // Just swallow it. Ideally we wouldn't need to use an exception for // control flow, but alas the API doesn't seem to have a test method. } return(false); }
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); }
protected override void ProcessRecord() { base.ProcessRecord(); // Unfortunately there is no way to test if an object exists on the API, so we // just issue a GET and intercept the 404 case. try { ObjectsResource.GetRequest objGetReq = Service.Objects.Get(Bucket, ObjectName); objGetReq.Projection = ObjectsResource.GetRequest.ProjectionEnum.Full; objGetReq.Execute(); WriteObject(true); } catch (GoogleApiException ex) when(ex.Error.Code == 404) { WriteObject(false); } }
protected override void ProcessRecord() { base.ProcessRecord(); Stream contentStream; if (!string.IsNullOrEmpty(File)) { string qualifiedPath = GetFullPath(File); if (!System.IO.File.Exists(qualifiedPath)) { throw new FileNotFoundException("File not found.", qualifiedPath); } contentStream = new FileStream(qualifiedPath, FileMode.Open); } else { // Get the underlying byte representation of the string using the same encoding (UTF-16). // So the data will be written in the same format it is passed, rather than converting to // UTF-8 or UTF-32 when writen to Cloud Storage. byte[] contentBuffer = Encoding.Unicode.GetBytes(Contents ?? ""); contentStream = new MemoryStream(contentBuffer); } // Get the existing storage object so we can use its metadata. (If it does not exist, we will fall back to // default values.) Object existingGcsObject = null; Dictionary <string, string> existingObjectMetadata = null; using (contentStream) { try { ObjectsResource.GetRequest getReq = Service.Objects.Get(Bucket, ObjectName); getReq.Projection = ObjectsResource.GetRequest.ProjectionEnum.Full; existingGcsObject = getReq.Execute(); existingObjectMetadata = ConvertToDictionary(existingGcsObject.Metadata); // If the object already has metadata associated with it, we first PATCH the new metadata into the // existing object. Otherwise we would reimplement "metadata merging" logic, and probably get it wrong. if (Metadata != null) { Object existingGcsObjectUpdatedMetadata = UpdateObjectMetadata( Service, existingGcsObject, ConvertToDictionary(Metadata)); existingObjectMetadata = ConvertToDictionary(existingGcsObjectUpdatedMetadata.Metadata); } } catch (GoogleApiException ex) when(ex.HttpStatusCode == HttpStatusCode.NotFound) { if (!Force.IsPresent) { throw new PSArgumentException( $"Storage object '{ObjectName}' does not exist. Use -Force to ignore."); } } string contentType = GetContentType(ContentType, existingObjectMetadata, existingGcsObject?.ContentType); // Rewriting GCS objects is done by simply creating a new object with the // same name. (i.e. this is functionally identical to New-GcsObject.) // // We don't need to worry about data races and/or corrupting data mid-upload // because of the upload semantics of Cloud Storage. // See: https://cloud.google.com/storage/docs/consistency Object updatedGcsObject = UploadGcsObject( Service, Bucket, ObjectName, contentStream, contentType, null /* predefinedAcl */, existingObjectMetadata); WriteObject(updatedGcsObject); } }