Configuration
GCP Implementation supports the Google Cloud Storage BLOB and Google Cloud Storage BLOB PRIVATE storage types. To setup a connection to this service you need a JSON Key from Amazon.
Implementation
Save
public static async Task SaveBLOB(StorageInformation storageInformation) { try { XElement storageXML = XElement.Parse(storageInformation.StorageDefinition); string jsonKey = storageXML.GetElement("jsonKey", ""); string URL = $"https://storage.googleapis.com/{storageInformation.WorkspaceGUID}/"; // Create a connection to the GCP storage account... GoogleCredential gcpCredentials = GoogleCredential.FromJson(jsonKey); StorageClient gcpStorageClient = StorageClient.Create(gcpCredentials); try { await gcpStorageClient.GetBucketAsync(storageInformation.WorkspaceGUID.ToString()); } catch (Google.GoogleApiException) { storageInformation.ErrorMessage = $"Container {storageInformation.WorkspaceGUID} was not found"; } storageInformation.MemoryStream.Position = 0; string fileType = storageInformation.IsFileCompressed ? "zip" : $"{storageInformation.FileType.ToLower()}"; if (storageInformation.IsFilePublic) { // The BLOB can be accessed by the public UploadObjectOptions objectOptions = new() { PredefinedAcl = PredefinedObjectAcl.PublicRead }; await gcpStorageClient.UploadObjectAsync(storageInformation.WorkspaceGUID.ToString() , $"{storageInformation.FileName.ToLower()}.{fileType}" , null , storageInformation.MemoryStream , objectOptions ); storageInformation.FileURL = $"{URL}{storageInformation.FileName.ToLower()}.{fileType}"; } else { // The BLOB can only be accessed by the owner await gcpStorageClient.UploadObjectAsync(storageInformation.WorkspaceGUID.ToString() , $"{storageInformation.FileName.ToLower()}.{fileType}" , null , storageInformation.MemoryStream ); storageInformation.FileURL = $"PRIVATE:{URL}{storageInformation.FileName.ToLower()}.{fileType}"; } } catch (Exception) { storageInformation.ErrorMessage = "GCStorage.SaveBLOB - Unknown error"; } }
Read
public static async Task ReadBLOB(StorageInformation storageInformation) { try { XElement storageXML = XElement.Parse(storageInformation.StorageDefinition); string jsonKey = storageXML.GetElement("jsonKey", ""); // Create a connection to the GCP storage account... GoogleCredential gcpCredentials = GoogleCredential.FromJson(jsonKey); StorageClient gcpStorageClient = StorageClient.Create(gcpCredentials); try { await gcpStorageClient.GetBucketAsync(storageInformation.WorkspaceGUID.ToString()); } catch (Google.GoogleApiException) { storageInformation.ErrorMessage = $"Container {storageInformation.WorkspaceGUID} was not found"; } // Read the file from the GCP storage container... try { string fileType = storageInformation.IsFileCompressed ? "zip" : $"{storageInformation.FileType.ToLower()}"; await gcpStorageClient.DownloadObjectAsync(storageInformation.WorkspaceGUID.ToString() , $"{storageInformation.FileName.ToLower()}.{fileType}" , storageInformation.MemoryStream ); } catch (Exception) { storageInformation.ErrorMessage = $"File {storageInformation.FileName.ToLower()}.{storageInformation.FileType.ToLower()} does not exist"; } } catch (Exception) { storageInformation.ErrorMessage = "GCStorage.ReadBLOB - Unknown error"; } }