Google API .NET 用戶端程式庫自 1.4.0-beta 版起一直是支援續傳媒體下載的功能。Google API 專屬程式庫包含可與此功能互動的便利方法。
續傳媒體下載通訊協定與可續傳媒體上傳通訊協定類似,相關說明請見 Drive API 的媒體上傳頁面。
主要用途為 MediaDownloader
。在這個實作續傳媒體下載的實作中,媒體內容會分成多個區塊下載 (區塊大小可設定)。
程式碼範例
如果 API 專屬程式庫中的方法在探索文件中包含「supportsMediaDownload
」參數,則要求類別中提供 Download
和 DownloadAsync
便利方法。這些方法會將媒體資料下載至您提供的 Stream
物件。例如:
{ // Create the service using the client credentials. var storageService = new StorageService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "APP_NAME_HERE" }); // Get the client request object for the bucket and desired object. var getRequest = storageService.Objects.Get("BUCKET_HERE", "OBJECT_HERE"); using (var fileStream = new System.IO.FileStream( "FILE_PATH_HERE", System.IO.FileMode.Create, System.IO.FileAccess.Write)) { // 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. getRequest.MediaDownloader.ProgressChanged += Download_ProgressChanged; getRequest.Download(fileStream); } } static void Download_ProgressChanged(IDownloadProgress progress) { Console.WriteLine(progress.Status + " " + progress.BytesDownloaded); }