/// <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); } }
/// <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('/'))); }
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); }
// To download the file: public void Download(string bucketName) { ObjectsResource.GetRequest downloadRequest = null; downloadRequest = new ObjectsResource.GetRequest(_storageService, bucketName, "keyName"); MemoryStream stream = new MemoryStream(); downloadRequest.Download(stream); // I am just downloading to a memory stream. You can download to a file stream as well. }
/// <summary> /// Downloads the file from the bucket. /// Returns a stream for further processing. /// </summary> /// <param name="bucketName">name of the bucket</param> /// <param name="fileName">name of the file in the bucket</param> /// <returns></returns> public async Task <Stream> DownloadFile(string bucketName, string fileName) { var downloadRequest = new ObjectsResource.GetRequest(Service, bucketName, fileName); downloadRequest.OauthToken = AccessToken; var file = await downloadRequest.ExecuteAsync(); var stream = new MemoryStream(); await downloadRequest.MediaDownloader.DownloadAsync(file.MediaLink, stream); stream.Seek(0, SeekOrigin.Begin); return(stream); }
/// <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); } }
public byte[] DownloadFile(File fileInfo) { var newObject = new Google.Apis.Storage.v1.Data.Object() { Bucket = BucketToUpload, Name = fileInfo.Id.ToString() }; Stream memoryStream = new MemoryStream(); byte[] result = null; try { var downloadRequest = new ObjectsResource.GetRequest(StorageService, BucketToUpload, fileInfo.Id.ToString()); var resultStatus = downloadRequest.DownloadWithStatus(memoryStream); result = ReadToEnd(memoryStream); #region Decryption and decompressing if (fileInfo.Encrypted) { result = Decrypt(result, Convert.FromBase64String(fileInfo.EncryptionKey), Convert.FromBase64String(fileInfo.IV)).Take((int)fileInfo.FileSize).ToArray(); } if (fileInfo.Compressed) { result = Decompress(result); } #endregion } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { memoryStream.Dispose(); } return(result); }
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); } }