/// <summary> /// Clears the content of an object. Used by Clear-Content. /// </summary> /// <param name="path">The path of the object to clear.</param> public void ClearContent(string path) { if (!ShouldProcess(path, "Clear-Content")) { return; } var gcsPath = GcsPath.Parse(path); Object body = new Object { Name = gcsPath.ObjectPath, Bucket = gcsPath.Bucket }; var memoryStream = new MemoryStream(); var contentType = GcsCmdlet.UTF8TextMimeType; ObjectsResource.InsertMediaUpload request = Service.Objects.Insert(body, gcsPath.Bucket, memoryStream, contentType); IUploadProgress response = request.Upload(); if (response.Exception != null) { throw response.Exception; } TelemetryReporter.ReportSuccess(nameof(GoogleCloudStorageProvider), nameof(ClearContent)); }
/// <summary> /// Uploads a local file to Google Cloud storage, overwriting any existing object and clobber existing metadata /// values. /// </summary> protected Object UploadGcsObject( StorageService service, string bucket, string objectName, Stream contentStream, string contentType, PredefinedAclEnum?predefinedAcl, Dictionary <string, string> metadata) { // Work around an API wart. It is possible to specify content type via the API and also by // metadata. if (metadata != null && metadata.ContainsKey("Content-Type")) { metadata["Content-Type"] = contentType; } Object newGcsObject = new Object { Bucket = bucket, Name = objectName, ContentType = contentType, Metadata = metadata }; ObjectsResource.InsertMediaUpload insertReq = service.Objects.Insert( newGcsObject, bucket, contentStream, contentType); insertReq.PredefinedAcl = predefinedAcl; insertReq.Projection = ProjectionEnum.Full; var finalProgress = insertReq.Upload(); if (finalProgress.Exception != null) { throw finalProgress.Exception; } return(insertReq.ResponseBody); }
public UploadResult UploadFile(IFormFile file, string fileName, bool encryption, bool compressing) { var newObject = new Google.Apis.Storage.v1.Data.Object() { Bucket = BucketToUpload, Name = fileName }; string encryptionKey = null, iv = null; // Actions with data here encrypting / compressing var fileStream = file.OpenReadStream(); var fileBytes = ReadToEnd(fileStream); #region Encryption and compressing if (compressing) { fileBytes = Compress(fileBytes); } if (encryption) { var actionData = GenerateKeyAndIV(FileActionsConstants.AESFlavour); fileBytes = Encrypt(fileBytes, actionData.Item1, actionData.Item2); encryptionKey = Convert.ToBase64String(actionData.Item1); iv = Convert.ToBase64String(actionData.Item2); } #endregion Stream fileOutStream = new MemoryStream(fileBytes); try { var uploadRequest = new ObjectsResource.InsertMediaUpload(StorageService, newObject, BucketToUpload, fileOutStream, encryption || compressing ? "application/octet-stream" : file.ContentType); var res = uploadRequest.Upload(); } catch (Exception ex) { Console.WriteLine(ex.Message); return(new UploadResult { Success = false }); } finally { if (fileStream != null) { fileStream.Dispose(); } } return(new UploadResult(true, encryptionKey, iv)); }
internal Object Execute() { _mediaUpload.Upload(); CheckFinalProgress(); var result = _mediaUpload.ResponseBody; var hash = _crc == null ? result.Crc32c : Convert.ToBase64String(_crc.GetHash()); if (hash != result.Crc32c) { AggregateException additionalFailures = null; try { _validationFailureAction?.Invoke(result); } catch (Exception e) { additionalFailures = new AggregateException(e); } throw new UploadValidationException(hash, result, additionalFailures); } return(result); }
private Object NewObject(GcsPath gcsPath, NewGcsObjectDynamicParameters dynamicParameters, Stream contentStream) { Object newGcsObject = new Object { Bucket = gcsPath.Bucket, Name = gcsPath.ObjectPath, ContentType = dynamicParameters.ContentType }; ObjectsResource.InsertMediaUpload insertReq = Service.Objects.Insert( newGcsObject, newGcsObject.Bucket, contentStream, newGcsObject.ContentType); insertReq.PredefinedAcl = dynamicParameters.PredefinedAcl; insertReq.Projection = ObjectsResource.InsertMediaUpload.ProjectionEnum.Full; IUploadProgress finalProgress = insertReq.Upload(); if (finalProgress.Exception != null) { throw finalProgress.Exception; } return(insertReq.ResponseBody); }
public static async Task <string> UploadDocument(string folderName, IFormFile filedata, string fileName, string ext, JObject googleCredentialPathFile1, GoogleCredentialModel googleCredential) { string exten = Path.GetExtension(fileName).ToLower(); string bucketFolderWithFilePath = string.Empty; Console.WriteLine("---------------------GCBUCKET ----------------code"); ProjectProposalDetail model = new ProjectProposalDetail(); var scopes = new[] { @"https://www.googleapis.com/auth/cloud-platform" }; var cts = new CancellationTokenSource(); StorageService service = new StorageService(); UserCredential credential; ClientSecrets secrets = new ClientSecrets(); secrets.ClientId = googleCredentialPathFile1["installed"]["client_id"].ToString(); secrets.ClientSecret = googleCredentialPathFile1["installed"]["client_secret"].ToString(); Console.WriteLine($"secrets"); //var service = new StorageService(); //UserCredential credential; //using (var stream = new FileStream(googleCredentialPathFile1, FileMode.Open, FileAccess.Read)) //{ credential = await GoogleWebAuthorizationBroker.AuthorizeAsync( secrets, scopes, StaticResource.EmailId, CancellationToken.None); //} // Create the service. service = new StorageService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = StaticResource.ApplicationName, }); var bucketsQuery = service.Buckets.List(StaticResource.ProjectId); bucketsQuery.OauthToken = credential.Token.AccessToken; var buckets = bucketsQuery.Execute(); FileStream fileStream = null; try { ApiResponse response = new ApiResponse(); var newObject = new Google.Apis.Storage.v1.Data.Object() { Bucket = StaticResource.BucketName, Name = StaticResource.ProjectsFolderName + "/" + folderName + "/" + StaticResource.ProjectActivityFolderName + "/" + fileName }; var mimetype = GetMimeType(ext); var uploadRequest = new ObjectsResource.InsertMediaUpload(service, newObject, StaticResource.BucketName, filedata.OpenReadStream(), mimetype); var fileResponse = uploadRequest.Upload(); Console.WriteLine($"File upload on bucket status: {fileResponse.Status}"); if (fileResponse.Status.ToString() == "Completed" && fileResponse.Exception == null) { bucketFolderWithFilePath = newObject.Bucket + "/" + newObject.Name; } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { if (fileStream != null) { fileStream.Dispose(); } } if (!string.IsNullOrEmpty(bucketFolderWithFilePath)) { return(bucketFolderWithFilePath); } else { throw new Exception(StaticResource.UnableToUploadFile); } }
//upload files public static async Task <ProjectProposalDetail> uploadOtherProposaldoc(string folderName, IFormFile filedata, string fileName, GoogleCredentialModel googleCredential, string EmailId, string logginUserEmailId, string ext, JObject googleCredentialPathFile, string ProposalType) { ProjectProposalModel res = new ProjectProposalModel(); string exten = Path.GetExtension(fileName).ToLower(); ProjectProposalDetail model = new ProjectProposalDetail(); //there are different scopes, which you can find here https://cloud.google.com/storage/docs/authentication var scopes = new[] { @"https://www.googleapis.com/auth/cloud-platform" }; var cts = new CancellationTokenSource(); ClientSecrets secrets = new ClientSecrets(); secrets.ClientId = googleCredentialPathFile["installed"]["client_id"].ToString(); secrets.ClientSecret = googleCredentialPathFile["installed"]["client_secret"].ToString(); UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync( secrets, scopes, StaticResource.EmailId, CancellationToken.None); // Create the service. StorageService service = new StorageService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = StaticResource.ApplicationName, }); var bucketsQuery = service.Buckets.List(StaticResource.ProjectId); bucketsQuery.OauthToken = credential.Token.AccessToken; var buckets = bucketsQuery.Execute(); FileStream fileStream = null; try { ApiResponse response = new ApiResponse(); var newObject = new Google.Apis.Storage.v1.Data.Object() { Bucket = StaticResource.BucketName, Name = StaticResource.ProjectsFolderName + "/" + folderName + "/" + fileName }; var mimetype = GetMimeType(ext); var uploadRequest = new ObjectsResource.InsertMediaUpload(service, newObject, StaticResource.BucketName, filedata.OpenReadStream(), mimetype); uploadRequest.OauthToken = credential.Token.AccessToken; var fileResponse = uploadRequest.Upload(); Console.WriteLine($"Other files response from bucket: {fileResponse}"); var bucketFolderWithFilePath = newObject.Bucket + "/" + newObject.Name; if (fileResponse.Status.ToString() == "Completed" && fileResponse.Exception == null) { if (ProposalType == "Proposal") { model.FolderName = folderName; model.ProposalFileName = fileName; model.ProposalWebLink = bucketFolderWithFilePath; model.IsDeleted = false; model.CreatedDate = DateTime.UtcNow; } if (ProposalType == "EOI") { model.FolderName = folderName; model.EDIFileName = fileName; model.EDIFileWebLink = bucketFolderWithFilePath; model.EDIFileExtType = ext; model.IsDeleted = false; model.ModifiedDate = DateTime.UtcNow; } else if (ProposalType == "BUDGET") { model.FolderName = folderName; model.IsDeleted = false; model.BudgetFileName = fileName; model.BudgetFileWebLink = bucketFolderWithFilePath; model.BudgetFileExtType = ext; model.ModifiedDate = DateTime.UtcNow; } else if (ProposalType == "CONCEPT") { model.FolderName = folderName; model.IsDeleted = false; model.ConceptFileName = fileName; model.ConceptFileWebLink = bucketFolderWithFilePath; model.ConceptFileExtType = ext; model.ModifiedDate = DateTime.UtcNow; } else if (ProposalType == "PRESENTATION") { model.FolderName = folderName; model.IsDeleted = false; model.PresentationFileName = fileName; model.PresentationFileWebLink = bucketFolderWithFilePath; model.PresentationExtType = ext; model.ModifiedDate = DateTime.UtcNow; } } else { throw new Exception(StaticResource.UnableToUploadFile); } ////code to delete the file //ObjectsResource.ListRequest request = service.Objects.List(googleCredential.BucketName); //request.Delimiter = folderName + "/"; ////request.Prefix = Delimiter; //var filePath = folderName + "/" + projectProposalfilename; //Google.Apis.Storage.v1.Data.Objects response1 = request.Execute(); //foreach (var data in response1.Items) //{ // if (data != null && data.Name == filePath) // { // //delete the file in containing folder****** // ObjectsResource.DeleteRequest deleteRequest = null; // deleteRequest = new ObjectsResource.DeleteRequest(service, googleCredential.BucketName, data.Name); // deleteRequest.OauthToken = credential.Token.AccessToken; // deleteRequest.Execute(); // } // else // { // } //} //if (response1.Prefixes != null) //{ // // return // var data = response; //} } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { if (fileStream != null) { fileStream.Dispose(); } } return(model); }
public static async Task <ProjectProposalDetail> AuthExplicit(string filefullPath, string projectProposalfilename, JObject googleCredentialpathFile, string folderName, GoogleCredentialModel googleCredential, long Projectid, string userid) { ProjectProposalModel res = new ProjectProposalModel(); ProjectProposalDetail model = new ProjectProposalDetail(); //there are different scopes, which you can find here https://cloud.google.com/storage/docs/authentication //var scopes = new[] { @"https://www.googleapis.com/auth/cloud-platform" }; var scopes = new[] { @"https://www.googleapis.com/auth/devstorage.full_control" }; var cts = new CancellationTokenSource(); StorageService service = new StorageService(); ClientSecrets secrets = new ClientSecrets(); secrets.ClientId = googleCredentialpathFile["installed"]["client_id"].ToString(); secrets.ClientSecret = googleCredentialpathFile["installed"]["client_secret"].ToString(); UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync( secrets, scopes, StaticResource.EmailId, CancellationToken.None); service = new StorageService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = StaticResource.ApplicationName, }); var bucketsQuery = service.Buckets.List(StaticResource.ProjectId); bucketsQuery.OauthToken = credential.Token.AccessToken; var buckets = bucketsQuery.Execute(); // create bucket first time //var newBucket = new Google.Apis.Storage.v1.Data.Bucket() //{ // Name = "clear-fusion" //}; //var newBucketQuery = service.Buckets.Insert(newBucket, projectId); // newBucketQuery.OauthToken = userCredential.Result.Token.AccessToken; //you probably want to wrap this into try..catch block // newBucketQuery.Execute(); //enter bucket name to which you want to upload file FileStream fileStream = null; try { ApiResponse response = new ApiResponse(); Console.WriteLine("reading file path"); var path = Directory.GetCurrentDirectory() + "/Documents/Proposal/Proposal.docx"; Console.WriteLine($"File local Path with environment variables : {path}"); path = path.Replace(@"\", "/"); Console.WriteLine($"convert File local Path in linux : {path}"); var newObject = new Google.Apis.Storage.v1.Data.Object() { Bucket = googleCredential.BucketName, Name = googleCredential.Projects + "/" + folderName + "/" + projectProposalfilename + ".docx", }; fileStream = new FileStream(path, FileMode.Open); var uploadRequest = new ObjectsResource.InsertMediaUpload(service, newObject, googleCredential.BucketName, fileStream, "application/vnd.google-apps.document"); uploadRequest.OauthToken = credential.Token.AccessToken; var fileResponse = uploadRequest.Upload(); Console.WriteLine($"File upload status: {fileResponse.Status}"); var bucketFolderWithFilePath = newObject.Bucket + "/" + newObject.Name; if (fileResponse.Status.ToString() == "Completed" && fileResponse.Exception == null) { model.FolderName = folderName; model.ProposalFileName = projectProposalfilename; model.ProposalWebLink = bucketFolderWithFilePath; model.ProjectId = Projectid; model.IsDeleted = false; model.CreatedById = userid; model.CreatedDate = DateTime.Now; model.ProposalExtType = ".docx"; } else { Console.WriteLine("not completed"); throw new Exception(StaticResource.UnableToUploadFile); } } catch (Exception ex) { Console.WriteLine("Exception occured"); Console.WriteLine(ex.Message); } finally { if (fileStream != null) { fileStream.Dispose(); } } return(model); }
//****************read credential using service account from directory 21/03/19 //public static async Task<ProjectProposalDetail> StartProposalByDirectory(string projectProposalfilename, string googleCredentialpathFile, string folderName, ViewModels.Models.Project.GoogleCredentialModel googleCredential, long Projectid, string userid) //{ // FileStream fileStream = null; // ProjectProposalDetail model = new ProjectProposalDetail(); // try // { // ProjectProposalModel res = new ProjectProposalModel(); // //var scopes = new[] { @"https://www.googleapis.com/auth/cloud-platform" }; // //var cts = new CancellationTokenSource(); // //StorageService service = new StorageService(); // //UserCredential credential; // //using (var stream = new FileStream(googleCredentialpathFile, FileMode.Open, FileAccess.Read)) // //{ // // credential = await GoogleWebAuthorizationBroker.AuthorizeAsync( // // GoogleClientSecrets.Load(stream).Secrets, // // scopes, // // googleCredential.EmailId, CancellationToken.None); // //} // //// Create the service. // //service = new StorageService(new BaseClientService.Initializer() // //{ // // HttpClientInitializer = credential, // // ApplicationName = googleCredential.ApplicationName, // //}); // var clientSecrets = new ClientSecrets(); // clientSecrets.ClientId = "160690498129-pg9hh4gr2ucta6neiik97tv1sla1qkec.apps.googleusercontent.com"; // clientSecrets.ClientSecret = "W0Zn9o2KmJFRXDnCbmq9z5m6"; // //there are different scopes, which you can find here https://cloud.google.com/storage/docs/authentication // var scopes = new[] { @"https://www.googleapis.com/auth/devstorage.full_control" }; // var cts = new CancellationTokenSource(); // var userCredential = await GoogleWebAuthorizationBroker.AuthorizeAsync(clientSecrets, scopes, "*****@*****.**", cts.Token); // await userCredential.RefreshTokenAsync(cts.Token); // var service = new Google.Apis.Storage.v1.StorageService(); // Console.WriteLine($"---- credential service----: {service}"); // var bucketsQuery = service.Buckets.List(StaticResource.ProjectId); // bucketsQuery.OauthToken = userCredential.Token.AccessToken; // var buckets = bucketsQuery.Execute(); // var dir = Directory.GetCurrentDirectory(); // // var pathCombine = Path.Combine(dir, @"/Documents/Proposal/Proposal.docx"); // var pathCombine = Directory.GetCurrentDirectory() + @"/Documents/Proposal/Proposal.docx"; // pathCombine = pathCombine.Replace(@"\", "/"); // Console.WriteLine($"--------File local folder Path ----------: {pathCombine}"); // fileStream = new FileStream(pathCombine, FileMode.Open); // ApiResponse response = new ApiResponse(); // Console.WriteLine($"--------convert to linux window File local folder Path ----------: {fileStream}"); // var newObject = new Google.Apis.Storage.v1.Data.Object() // { // Bucket = googleCredential.BucketName, // Name = googleCredential.Projects + "/" + folderName + "/" + projectProposalfilename + "xyz" + ".docx", // }; // if (fileStream != null) // { // var uploadRequest = new ObjectsResource.InsertMediaUpload(service, newObject, googleCredential.BucketName, fileStream, "application/vnd.google-apps.document"); // uploadRequest.OauthToken = userCredential.Token.AccessToken; // var fileResponse = uploadRequest.Upload(); // Console.WriteLine($"File upload on bucket status: {fileResponse.Status}"); // var bucketFolderWithFilePath = newObject.Bucket + "/" + newObject.Name; // if (fileResponse.Status.ToString() == "Completed" && fileResponse.Exception == null) // { // model.FolderName = folderName; // model.ProposalFileName = projectProposalfilename; // model.ProposalWebLink = bucketFolderWithFilePath; // model.ProjectId = Projectid; // model.IsDeleted = false; // model.CreatedById = userid; // model.CreatedDate = DateTime.Now; // model.ProposalExtType = ".docx"; // } // } // else // { // throw new Exception(StaticResource.UnableToUploadFile); // } // } // catch (Exception ex) // { // Console.WriteLine(ex.Message); // } // finally // { // if (fileStream != null) // { // fileStream.Dispose(); // } // } // return model; //} //************************************read credential from directory using auth credentail and using env as well as directory 20/03/2019 public static async Task <ProjectProposalDetail> StartProposalByDirectory(string projectProposalfilename, string googleCredentialpathFile, string folderName, GoogleCredentialModel googleCredential, long Projectid, string userid) { FileStream fileStream = null; ProjectProposalDetail model = new ProjectProposalDetail(); try { ProjectProposalModel res = new ProjectProposalModel(); var scopes = new[] { @"https://www.googleapis.com/auth/cloud-platform" }; var cts = new CancellationTokenSource(); StorageService service = new StorageService(); UserCredential credential; using (var stream = new FileStream(googleCredentialpathFile, FileMode.Open, FileAccess.Read)) { credential = await GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load(stream).Secrets, scopes, googleCredential.EmailId, CancellationToken.None); } // Create the service. service = new StorageService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = googleCredential.ApplicationName, }); Console.WriteLine($"---- credential service----: {service}"); var bucketsQuery = service.Buckets.List(StaticResource.ProjectId); bucketsQuery.OauthToken = credential.Token.AccessToken; var buckets = bucketsQuery.Execute(); //enter bucket name to which you want to upload file ApiResponse response = new ApiResponse(); var path = Directory.GetCurrentDirectory() + @"/Documents/Proposal/Proposal.docx"; Console.WriteLine($"--------File local folder Path ----------: {path}"); path = path.Replace(@"\", "/"); Console.WriteLine($"--------convert to linux window File local folder Path ----------: {path}"); var newObject = new Google.Apis.Storage.v1.Data.Object() { Bucket = googleCredential.BucketName, Name = googleCredential.Projects + "/" + folderName + "/" + projectProposalfilename + "xyz" + ".docx", }; fileStream = new FileStream(path, FileMode.Open); if (fileStream != null) { var uploadRequest = new ObjectsResource.InsertMediaUpload(service, newObject, googleCredential.BucketName, fileStream, "application/vnd.google-apps.document"); uploadRequest.OauthToken = credential.Token.AccessToken; var fileResponse = uploadRequest.Upload(); Console.WriteLine($"File upload on bucket status: {fileResponse.Status}"); var bucketFolderWithFilePath = newObject.Bucket + "/" + newObject.Name; if (fileResponse.Status.ToString() == "Completed" && fileResponse.Exception == null) { model.FolderName = folderName; model.ProposalFileName = projectProposalfilename; model.ProposalWebLink = bucketFolderWithFilePath; model.ProjectId = Projectid; model.IsDeleted = false; model.CreatedById = userid; model.CreatedDate = DateTime.Now; model.ProposalExtType = ".docx"; } } else { throw new Exception(StaticResource.UnableToUploadFile); } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { if (fileStream != null) { fileStream.Dispose(); } } return(model); }