private static void DownloadFile(string fileId, string targetDirectory) { FilesResource.GetRequest request = driveService.Files.Get(fileId); request.Fields = "size,name,id,md5Checksum"; GoogleFile fileinfo = request.Execute(); var stream = new MemoryStream(); IDownloadProgress progress = request.DownloadWithStatus(stream); EditorCoroutineUtility.StartCoroutine(DownloadRoutine(progress, stream, fileId, fileinfo.Name, fileinfo.Size.Value, targetDirectory), Instance); WriteTasks.TryAdd(fileId, false); }
public GoogleDriveManagerResult DownloadFile(DriveService service, string fileId) { result = new GoogleDriveManagerResult(); try { FilesResource.GetRequest request = service.Files.Get(fileId); Google.Apis.Drive.v3.Data.File response = request.Execute(); string FileName = response.Name; result.GoogleDriveFilePath = Path.Combine(GoogleDriveConfig.DownloadsFolderPath, FileName); MemoryStream stream = new MemoryStream(); request.MediaDownloader.ProgressChanged += (IDownloadProgress progress) => { switch (progress.Status) { case DownloadStatus.Downloading: { WriteToConsole(GoogleDriveManagementConstants.DownloadInProgressStatus + progress.BytesDownloaded); break; } case DownloadStatus.Completed: { WriteToConsole(GoogleDriveManagementConstants.DownloadCompleteStatus); SaveStream(stream, result.GoogleDriveFilePath); break; } case DownloadStatus.Failed: { WriteToConsole(GoogleDriveManagementConstants.DownloadFailedStatus); break; } } }; ServicePointManager.ServerCertificateValidationCallback = delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return(true); }; request.DownloadWithStatus(stream); return(result); } catch (Exception e) { result.Exceptions.Add(e); WriteToConsole(GoogleDriveManagementConstants.DownloadFileException + e.Message); return(result); } }
public AttemptResult ReadFileIntoStream(string id, Stream stream) { FilesResource.GetRequest request = drive_service.Files.Get(id); try { request.DownloadWithStatus(stream); } catch (Google.GoogleApiException exception) { return(exception.Error.GetAttemptResult()); } return(AttemptResult.Succeeded); }
/// <summary> /// It starts downloading the files. It uses yield for return. /// </summary> public override IEnumerable <string> StartStream() { long byteStatus = 0; while (streamableFiles.Count != 0) { DownloadableFile file = streamableFiles.Dequeue(); FilesResource.GetRequest request = Service.Files.Get(file.GDFile.Id); string filePath = ""; using (MemoryStream stream = new MemoryStream()) { request.MediaDownloader.ProgressChanged += (IDownloadProgress progress) => { switch (progress.Status) { case DownloadStatus.Downloading: { CallStreamStatusEvent(byteStatus + progress.BytesDownloaded); break; } case DownloadStatus.Completed: { StreamdableBytes -= progress.BytesDownloaded; byteStatus += progress.BytesDownloaded; CallStreamStatusEvent(byteStatus); filePath = SaveFile(stream, file); break; } case DownloadStatus.Failed: { throw new Exception("Failed to download file"); } } }; request.DownloadWithStatus(stream); } yield return(filePath); } }
/// <summary> /// Used to download the given file from Google Drive. /// </summary> /// <param name="file">The file to download</param> /// <returns>A stream for the downloaded file or null if the file couldn't be found</returns> public MemoryStream GetGoogleDriveFileStream(GoogleDriveData.File file) { // Get the Google Drive service DriveService driveService = GetDriveService(); // Prepare the download request FilesResource.GetRequest getRequest = driveService.Files.Get(file.Id); getRequest.MediaDownloader.ProgressChanged += GetGoogleDriveFileStream_ProgressChanged; // Download the file MemoryStream downloadStream = new MemoryStream(); getRequest.DownloadWithStatus(downloadStream); // Go back to the start of the stream downloadStream.Seek(0, SeekOrigin.Begin); return(downloadStream); }
//Download file from Google Drive by fileId. public string DownloadGoogleFile(string fileId) { var CSPath = Directory.GetParent(Directory.GetParent(Directory.GetParent(Directory.GetCurrentDirectory()).FullName).FullName).FullName; var FolderPath = Path.Combine(CSPath, @"DAL\GoogleDriveFiles"); FilesResource.GetRequest request = service.Files.Get(fileId); string FileName = request.Execute().Name; string FilePath = Path.Combine(FolderPath, FileName) + @".jpg"; MemoryStream stream = 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 += (IDownloadProgress progress) => { switch (progress.Status) { case DownloadStatus.Downloading: { break; } case DownloadStatus.Completed: { SaveStream(stream, FilePath); break; } case DownloadStatus.Failed: { break; } } }; var a = request.DownloadWithStatus(stream); return(FilePath); }