public string MoveFiles(String fileId, String folderId) { Google.Apis.Drive.v3.DriveService service = GetService(); // Retrieve the existing parents to remove Google.Apis.Drive.v3.FilesResource.GetRequest getRequest = service.Files.Get(fileId); getRequest.Fields = "parents"; Google.Apis.Drive.v3.Data.File file = getRequest.Execute(); string previousParents = String.Join(",", file.Parents); // Move the file to the new folder Google.Apis.Drive.v3.FilesResource.UpdateRequest updateRequest = service.Files.Update(new Google.Apis.Drive.v3.Data.File(), fileId); updateRequest.Fields = "id, parents"; updateRequest.AddParents = folderId; updateRequest.RemoveParents = previousParents; file = updateRequest.Execute(); if (file != null) { return("Success"); } else { return("Fail"); } }
public override Stream GetStream(string fileName) { Google.Apis.Drive.v3.Data.File file = GetFile(fileName); if (file == null) { return(null); } Google.Apis.Drive.v3.FilesResource.GetRequest request = GoogleDriveService.Files.Get(file.Id); MemoryStream memoryStream = new MemoryStream(); request.Download(memoryStream); return(memoryStream); }
//Download file from Google Drive by fileId. public static string DownloadGoogleFile(string fileId) { Google.Apis.Drive.v3.DriveService service = GetService(); string FolderPath = System.Web.HttpContext.Current.Server.MapPath("/GoogleDriveFiles/"); Google.Apis.Drive.v3.FilesResource.GetRequest request = service.Files.Get(fileId); string FileName = request.Execute().Name; string FilePath = System.IO.Path.Combine(FolderPath, FileName); MemoryStream stream1 = new MemoryStream(); // Add a handler which will be notified on progress changes. // It will notify on each chunk download and when the // download is completed or failed. request.MediaDownloader.ProgressChanged += (Google.Apis.Download.IDownloadProgress progress) => { switch (progress.Status) { case DownloadStatus.Downloading: { Console.WriteLine(progress.BytesDownloaded); break; } case DownloadStatus.Completed: { Console.WriteLine("Download complete."); SaveStream(stream1, FilePath); break; } case DownloadStatus.Failed: { Console.WriteLine("Download failed."); break; } } }; request.Download(stream1); return(FilePath); }
private void DownloadFile(string fileId, string dest) { logger.Debug("file downloading...{0}", fileId); Google.Apis.Drive.v3.FilesResource.GetRequest request = service.Files.Get(fileId); var stream = new System.IO.MemoryStream(); // Add a handler which will be notified on progress changes. // It will notify on each chunk download and when the // download is completed or failed. request.MediaDownloader.ProgressChanged += (IDownloadProgress progress) => { switch (progress.Status) { case DownloadStatus.Downloading: { logger.Debug(progress.BytesDownloaded); break; } case DownloadStatus.Completed: { logger.Debug("Download complete."); break; } case DownloadStatus.Failed: { logger.Debug("Download failed."); break; } } }; request.Download(stream); using (var fileStream = System.IO.File.Create(dest)) { stream.Seek(0, SeekOrigin.Begin); stream.CopyTo(fileStream); } logger.Debug("file downloaded:{0}", dest); }
/// <summary> /// Чтение файла из google drive в поток /// </summary> /// <param name="id">ид файла</param> /// <returns>Поток</returns> public MemoryStream FileRead(string id) { if (String.IsNullOrEmpty(id)) { return(null); } if (credential == null) { this.Authorize(); } if (credential != null) { using (var service = new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = ApplicationName, })) { Google.Apis.Drive.v3.FilesResource.GetRequest request = service.Files.Get(id); MemoryStream stream = new MemoryStream(); request.MediaDownloader.ProgressChanged += (Google.Apis.Download.IDownloadProgress progress) => { if (progress.Status == Google.Apis.Download.DownloadStatus.Completed) { } }; request.Download(stream); //if (result) //{ // //int start = 0; // //int count = (int)stream.Length; // //value = Encoding.Default.GetString(stream.GetBuffer(), start, count); //} return(stream); } } return(null); }
private static GoogleDriveBaseResult DownloadRequest(Google.Apis.Drive.v3.FilesResource.GetRequest request, System.IO.Stream output) { var status = request.DownloadWithStatus(output); if (status.Status == DownloadStatus.Completed) { return(new GoogleDriveBaseResult() { IsSucceed = true }); } else { var result = new GoogleDriveBaseResult(); if (result.FillFromException(status.Exception)) { return(result); } throw status.Exception; } }
/// <summary> /// Realiza copy/paste de un archivo hacia otro directorio /// </summary> /// <param name="fileId"></param> /// <param name="folderId"></param> /// <returns></returns> public static string CopyFiles(String fileId, String folderId) { // Retrieve the existing parents to remove Google.Apis.Drive.v3.FilesResource.GetRequest getRequest = service.Files.Get(fileId); getRequest.Fields = "parents"; Google.Apis.Drive.v3.Data.File file = getRequest.Execute(); // Copy the file to the new folder Google.Apis.Drive.v3.FilesResource.UpdateRequest updateRequest = service.Files.Update(new Google.Apis.Drive.v3.Data.File(), fileId); updateRequest.Fields = "id, parents"; updateRequest.AddParents = folderId; file = updateRequest.Execute(); if (file != null) { return("Success"); } else { return("Fail"); } }
public async Task <int> MoveFile(string fileId, string FolderId) { Google.Apis.Drive.v3.FilesResource.GetRequest getRequest = service.Files.Get(fileId); getRequest.Fields = "parents"; Google.Apis.Drive.v3.Data.File file = getRequest.Execute(); string previousParents = string.Join(",", file.Parents); Google.Apis.Drive.v3.FilesResource.UpdateRequest updateRequest = service.Files.Update(new Google.Apis.Drive.v3.Data.File(), fileId); updateRequest.Fields = "parents"; updateRequest.AddParents = FolderId; updateRequest.RemoveParents = previousParents; file = updateRequest.Execute(); if (file != null) { return(1); } else { return(0); } }
private void DoWork(object sender, DoWorkEventArgs e) { CGDTool gdt = new CGDTool(); DriveService driveService = gdt.Authenticate(context); var fileId = id; try { if (export) { var request = driveService.Files.Export(fileId, this.mimetype); var stream = new System.IO.MemoryStream(); request.MediaDownloader.ProgressChanged += (IDownloadProgress progress) => { switch (progress.Status) { case DownloadStatus.Downloading: { UpateStatus("bytes" + progress.BytesDownloaded, false); break; } case DownloadStatus.Completed: { data = stream.ToArray(); UpateStatus("Download complete", true); break; } case DownloadStatus.Failed: { UpateStatus("Download failed: " + progress.Exception.Message, true); break; } } }; request.Download(stream); } else { var stream = new System.IO.MemoryStream(); // https://docs.google.com/forms/d/171SteVTr-P8HtM5N5p8ftGFlEq_1LVQcTQ2p9yMUmDY/downloadresponses?tz_offset=7200000 Google.Apis.Drive.v3.FilesResource.GetRequest request = driveService.Files.Get(fileId); request.MediaDownloader.ProgressChanged += (IDownloadProgress progress) => { switch (progress.Status) { case DownloadStatus.Downloading: { UpateStatus("bytes" + progress.BytesDownloaded, false); break; } case DownloadStatus.Completed: { data = stream.ToArray(); UpateStatus("Download complete", true); break; } case DownloadStatus.Failed: { UpateStatus("Download failed 1: " + progress.Exception.Message, true); break; } } }; request.Alt = Google.Apis.Drive.v3.DriveBaseServiceRequest <Google.Apis.Drive.v3.Data.File> .AltEnum.Json; request.Download(stream); } } catch (Exception err) { UpateStatus("Download failed 2: " + err.Message, true); } }