public void Download_Error_JsonResponse() { var downloadUri = "http://www.sample.com"; var chunkSize = 100; var error = new RequestError { Code = 12345, Message = "Text", Errors = new[] { new SingleError { Message = "Nested error" } } }; var response = new StandardResponse<object> { Error = error }; var responseText = new NewtonsoftJsonSerializer().Serialize(response); var handler = new MultipleChunksMessageHandler { ErrorResponse = responseText }; handler.StatusCode = HttpStatusCode.BadRequest; handler.ChunkSize = chunkSize; // The media downloader adds the parameter... handler.DownloadUri = new Uri(downloadUri + "?alt=media"); using (var service = CreateMockClientService(handler)) { var downloader = new MediaDownloader(service); downloader.ChunkSize = chunkSize; IList<IDownloadProgress> progressList = new List<IDownloadProgress>(); downloader.ProgressChanged += (p) => { progressList.Add(p); }; var outputStream = new MemoryStream(); downloader.Download(downloadUri, outputStream); var lastProgress = progressList.LastOrDefault(); Assert.That(lastProgress.Status, Is.EqualTo(DownloadStatus.Failed)); GoogleApiException exception = (GoogleApiException) lastProgress.Exception; Assert.That(exception.HttpStatusCode, Is.EqualTo(handler.StatusCode)); // Just a smattering of checks - if these two pass, it's surely okay. Assert.That(exception.Error.Code, Is.EqualTo(error.Code)); Assert.That(exception.Error.Errors[0].Message, Is.EqualTo(error.Errors[0].Message)); } }
public void Download_Error_PlaintextResponse() { var downloadUri = "http://www.sample.com"; var chunkSize = 100; var responseText = "Not Found"; var handler = new MultipleChunksMessageHandler { ErrorResponse = responseText }; handler.StatusCode = HttpStatusCode.NotFound; handler.ChunkSize = chunkSize; // The media downloader adds the parameter... handler.DownloadUri = new Uri(downloadUri + "?alt=media"); using (var service = CreateMockClientService(handler)) { var downloader = new MediaDownloader(service); downloader.ChunkSize = chunkSize; IList<IDownloadProgress> progressList = new List<IDownloadProgress>(); downloader.ProgressChanged += (p) => { progressList.Add(p); }; var outputStream = new MemoryStream(); downloader.Download(downloadUri, outputStream); var lastProgress = progressList.LastOrDefault(); Assert.That(lastProgress.Status, Is.EqualTo(DownloadStatus.Failed)); GoogleApiException exception = (GoogleApiException) lastProgress.Exception; Assert.That(exception.HttpStatusCode, Is.EqualTo(handler.StatusCode)); Assert.That(exception.Message, Is.EqualTo(responseText)); Assert.IsNull(exception.Error); } }
//Refactoring ++OK public async Task<List<DiffStampAdapter>> TryDownloadAllDiffstampsForFile(FileEntityAggregator targetFile) { DriveService driveService = await TryGetAuthorizer(); //Get all files from folder FilesResource.ListRequest filesListRequest = driveService.Files.List(); filesListRequest.Q = String.Format("'{0}' in parents and trashed=false", targetFile.GoogleDriveParentId); FileList filelist = await filesListRequest.ExecuteAsync(); //Get diff stamps descriptors IEnumerable<File> foundDiffStamps = from items in filelist.Items where items.FileExtension == BsdiffStampExtension orderby DateTime.Parse(items.CreatedDate, CultureInfo.InvariantCulture) ascending select items; //Download diff stamps MediaDownloader mediaDownloader = new MediaDownloader(driveService); List<DiffStampAdapter> resultDiffStampAdapters = new List<DiffStampAdapter>(); foreach (File diffStampDescriptor in foundDiffStamps) { using (IO.MemoryStream memoryStream = new IO.MemoryStream()) { mediaDownloader.Download(diffStampDescriptor.DownloadUrl, memoryStream); BinaryFormatter binaryFormatter = new BinaryFormatter(); memoryStream.Position = 0; DiffStampAdapter diffStampAdapter = binaryFormatter.Deserialize(memoryStream) as DiffStampAdapter; resultDiffStampAdapters.Add(diffStampAdapter); } } return resultDiffStampAdapters; }
/// <summary>A helper test to test sync and async downloads.</summary> /// <param name="chunkSize">The chunk size for each part.</param> /// <param name="sync">Indicates if this download should be synchronously or asynchronously.</param> /// <param name="cancelRequest">Defines the request index to cancel the download request.</param> /// <param name="downloadUri">The URI which contains the media to download.</param> private void Subtest_Download_Chunks(int chunkSize, bool sync = true, int cancelRequest = 0, string downloadUri = "http://www.sample.com") { var handler = new MultipleChunksMessageHandler(MediaContent); handler.StatusCode = HttpStatusCode.OK; handler.ChunkSize = chunkSize; handler.DownloadUri = new Uri(downloadUri + (downloadUri.Contains("?") ? "&" : "?") + "alt=media"); // support cancellation if (cancelRequest > 0) { handler.CancelRequestNum = cancelRequest; } handler.CancellationTokenSource = new CancellationTokenSource(); int expectedCalls = (int)Math.Ceiling((double) MediaContent.Length / chunkSize); using (var service = CreateMockClientService(handler)) { var downloader = new MediaDownloader(service); downloader.ChunkSize = chunkSize; IList<IDownloadProgress> progressList = new List<IDownloadProgress>(); downloader.ProgressChanged += (p) => { progressList.Add(p); }; var outputStream = new MemoryStream(); if (sync) { downloader.Download(downloadUri, outputStream); } else { try { var result = downloader.DownloadAsync(downloadUri, outputStream, handler.CancellationTokenSource.Token).Result; Assert.AreEqual(0, handler.CancelRequestNum); } catch (AggregateException ex) { Assert.IsInstanceOf<TaskCanceledException>(ex.InnerException); } } var lastProgress = progressList.LastOrDefault(); if (cancelRequest > 0) { // the download was interrupted in the middle Assert.That(handler.Calls, Is.EqualTo(cancelRequest)); // last request should fail Assert.NotNull(lastProgress); Assert.NotNull(lastProgress.Exception); Assert.That(lastProgress.Status, Is.EqualTo(DownloadStatus.Failed)); Assert.That(lastProgress.BytesDownloaded, Is.EqualTo(chunkSize * cancelRequest)); } else { // the download succeeded Assert.That(handler.Calls, Is.EqualTo(expectedCalls)); Assert.NotNull(lastProgress); Assert.Null(lastProgress.Exception); Assert.That(lastProgress.Status, Is.EqualTo(DownloadStatus.Completed)); Assert.That(lastProgress.BytesDownloaded, Is.EqualTo(MediaContent.Length)); byte[] actual = outputStream.ToArray(); CollectionAssert.AreEqual(MediaContent, actual); } } }
public void Run(IConfigurableHttpClientInitializer credential, string projectId, string bucketName) { StorageService service = new StorageService( new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "GCS Sample", }); Console.WriteLine("List of buckets in current project"); Buckets buckets = service.Buckets.List(projectId).Execute(); foreach (var bucket in buckets.Items) { Console.WriteLine(bucket.Name); } Console.WriteLine("Total number of items in bucket: " + buckets.Items.Count); Console.WriteLine("============================="); // using Google.Apis.Storage.v1.Data.Object to disambiguate from // System.Object Google.Apis.Storage.v1.Data.Object fileobj = new Google.Apis.Storage.v1.Data.Object() { Name = "somefile.txt" }; Console.WriteLine("Creating " + fileobj.Name + " in bucket " + bucketName); byte[] msgtxt = Encoding.UTF8.GetBytes("Lorem Ipsum"); service.Objects.Insert(fileobj, bucketName, new MemoryStream(msgtxt), "text/plain").Upload(); Console.WriteLine("Object created: " + fileobj.Name); Console.WriteLine("============================="); Console.WriteLine("Reading object " + fileobj.Name + " in bucket: " + bucketName); var req = service.Objects.Get(bucketName, fileobj.Name); Google.Apis.Storage.v1.Data.Object readobj = req.Execute(); Console.WriteLine("Object MediaLink: " + readobj.MediaLink); // download using Google.Apis.Download and display the progress string pathUser = Environment.GetFolderPath( Environment.SpecialFolder.UserProfile); var fileName = Path.Combine(pathUser, "Downloads") + "\\" + readobj.Name; Console.WriteLine("Starting download to " + fileName); var downloader = new MediaDownloader(service) { ChunkSize = DownloadChunkSize }; // add a delegate for the progress changed event for writing to // console on changes downloader.ProgressChanged += progress => Console.WriteLine(progress.Status + " " + progress.BytesDownloaded + " bytes"); using (var fileStream = new System.IO.FileStream(fileName, System.IO.FileMode.Create, System.IO.FileAccess.Write)) { var progress = downloader.Download(readobj.MediaLink, fileStream); if (progress.Status == DownloadStatus.Completed) { Console.WriteLine(readobj.Name + " was downloaded successfully"); } else { Console.WriteLine("Download {0} was interrupted. Only {1} " + "were downloaded. ", readobj.Name, progress.BytesDownloaded); } } Console.WriteLine("============================="); }
public async Task<IO.MemoryStream> TryDownloadFileToStream(FileEntityAggregator targetFile) { DriveService driveService = await TryGetAuthorizer(); MediaDownloader mediaDownloader = new MediaDownloader(driveService); IO.MemoryStream memoryStream = new IO.MemoryStream(); try { mediaDownloader.Download(targetFile.GoogleDrivePath, memoryStream); return memoryStream; } catch (Exception) { memoryStream.Dispose(); return default(IO.MemoryStream); } }
public void Download_Error_PlaintextResponse() { using (var service = new MockClientService()) { var downloader = new MediaDownloader(service); IList<IDownloadProgress> progressList = new List<IDownloadProgress>(); downloader.ProgressChanged += (p) => { progressList.Add(p); }; var outputStream = new MemoryStream(); downloader.Download(_httpPrefix + "NotFoundPlainText", outputStream); var lastProgress = progressList.LastOrDefault(); Assert.That(lastProgress.Status, Is.EqualTo(DownloadStatus.Failed)); GoogleApiException exception = (GoogleApiException) lastProgress.Exception; Assert.That(exception.HttpStatusCode, Is.EqualTo(HttpStatusCode.NotFound)); Assert.That(exception.Message, Is.EqualTo(NotFoundError)); Assert.IsNull(exception.Error); } }
protected override void ProcessRecord() { base.ProcessRecord(); if (InputObject != null) { Bucket = InputObject.Bucket; ObjectName = InputObject.Name; } string uri = GetBaseUri(Bucket, ObjectName); var downloader = new MediaDownloader(Service); // Write object contents to the pipeline if no -OutFile is specified. if (string.IsNullOrEmpty(OutFile)) { // Start with a 1MiB buffer. We could get the object's metadata and use its exact // file size, but making a web request << just allocating more memory. using (var memStream = new MemoryStream(1024 * 1024)) { var result = downloader.Download(uri, memStream); CheckForError(result); // Stream cursor is at the end (data just written). memStream.Position = 0; using (var streamReader = new StreamReader(memStream)) { string objectContents = streamReader.ReadToEnd(); WriteObject(objectContents); } } return; } // Write object contents to disk. Fail if the local file exists, unless -Force is specified. string qualifiedPath = GetFullPath(OutFile); bool fileExists = File.Exists(qualifiedPath); if (fileExists && !Force.IsPresent) { throw new PSArgumentException($"File '{qualifiedPath}' already exists. Use -Force to overwrite."); } using (var writer = new FileStream(qualifiedPath, FileMode.Create)) { var result = downloader.Download(uri, writer); CheckForError(result); } }
public void Download_Error_JsonResponse() { using (var service = new MockClientService()) { var downloader = new MediaDownloader(service); IList<IDownloadProgress> progressList = new List<IDownloadProgress>(); downloader.ProgressChanged += (p) => { progressList.Add(p); }; var outputStream = new MemoryStream(); downloader.Download(_httpPrefix + "BadRequestJson", outputStream); var lastProgress = progressList.LastOrDefault(); Assert.That(lastProgress.Status, Is.EqualTo(DownloadStatus.Failed)); GoogleApiException exception = (GoogleApiException) lastProgress.Exception; Assert.That(exception.HttpStatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); // Just a smattering of checks - if these two pass, it's surely okay. Assert.That(exception.Error.Code, Is.EqualTo(BadRequestError.Code)); Assert.That(exception.Error.Errors[0].Message, Is.EqualTo(BadRequestError.Errors[0].Message)); } }
/// <summary>A helper test to test sync and async downloads.</summary> /// <param name="chunkSize">The chunk size for each part.</param> /// <param name="sync">Indicates if this download should be synchronously or asynchronously.</param> /// <param name="cancelChunk">Defines the chunk at which to cancel the download request.</param> /// <param name="target">Last component of the Uri to download</param> private void Subtest_Download_Chunks(int chunkSize, bool sync = true, int cancelChunk = 0, string target = "content") { string downloadUri = _httpPrefix + target; var cts = new CancellationTokenSource(); using (var service = new MockClientService()) { var downloader = new MediaDownloader(service); downloader.ChunkSize = chunkSize; IList<IDownloadProgress> progressList = new List<IDownloadProgress>(); int progressUpdates = 0; long lastBytesDownloaded = 0; downloader.ProgressChanged += (p) => { if (p.Status != DownloadStatus.Failed) { // We shouldn't receive duplicate notifications for the same range. Assert.That(p.BytesDownloaded, Is.GreaterThan(lastBytesDownloaded)); } lastBytesDownloaded = p.BytesDownloaded; progressList.Add(p); if (++progressUpdates == cancelChunk) { cts.Cancel(); } }; var outputStream = new MemoryStream(); if (sync) { downloader.Download(downloadUri, outputStream); } else { try { var result = downloader.DownloadAsync(downloadUri, outputStream, cts.Token).Result; if (result.Exception == null) { Assert.AreEqual(0, cancelChunk); } else { Assert.IsInstanceOf<OperationCanceledException>(result.Exception); } } catch (AggregateException ex) { Assert.IsInstanceOf<TaskCanceledException>(ex.InnerException); } } var lastProgress = progressList.LastOrDefault(); if (cancelChunk > 0) { // last request should fail Assert.NotNull(lastProgress); Assert.NotNull(lastProgress.Exception); Assert.That(lastProgress.Status, Is.EqualTo(DownloadStatus.Failed)); Assert.That(lastProgress.BytesDownloaded, Is.EqualTo(chunkSize * cancelChunk)); } else { Assert.NotNull(lastProgress); Assert.Null(lastProgress.Exception); Assert.That(lastProgress.Status, Is.EqualTo(DownloadStatus.Completed)); Assert.That(lastProgress.BytesDownloaded, Is.EqualTo(MediaContent.Length)); byte[] actual = outputStream.ToArray(); CollectionAssert.AreEqual(MediaContent, actual); } } }
/// <summary> /// Uses MediaDownloader to download the contents of a URI. /// Asserts that the download succeeded and returns the resulting content as a string. /// </summary> /// <param name="uri">Uri to download</param> /// <returns></returns> private string SimpleDownload(string uri) { using (var service = new MockClientService()) { var downloader = new MediaDownloader(service); var outputStream = new MemoryStream(); var result = downloader.Download(uri, outputStream); Assert.AreEqual(result.Status, DownloadStatus.Completed); Assert.IsNull(result.Exception); Assert.AreEqual(result.BytesDownloaded, outputStream.Position); return Encoding.UTF8.GetString(outputStream.GetBuffer(), 0, (int)outputStream.Position); } }
public void Download_NoContent() { using (var service = new MockClientService()) { var downloader = new MediaDownloader(service); IList<IDownloadProgress> progressList = new List<IDownloadProgress>(); downloader.ProgressChanged += (p) => { progressList.Add(p); }; var outputStream = new MemoryStream(); downloader.Download(_httpPrefix + "NoContent", outputStream); // We expect only one event -- "completed". Assert.That(progressList.Count, Is.EqualTo(1)); var progress = progressList[0]; Assert.That(progress.Status, Is.EqualTo(DownloadStatus.Completed)); Assert.That(progress.BytesDownloaded, Is.EqualTo(0)); Assert.That(outputStream.Length, Is.EqualTo(0)); } }
// [END delete_object] // [START media_downloader] public void DownloadToFile(string bucketName) { StorageService storage = CreateStorageClient(); var objectToDownload = storage.Objects.Get(bucketName, "my-file.txt").Execute(); var downloader = new MediaDownloader(storage); downloader.ProgressChanged += progress => { Console.WriteLine($"{progress.Status} {progress.BytesDownloaded} bytes"); }; using (var fileStream = new FileStream("downloaded-file.txt", FileMode.Create)) { var progress = downloader.Download(objectToDownload.MediaLink, fileStream); if (progress.Status == DownloadStatus.Completed) { Console.WriteLine("Downloaded my-file.txt to downloaded-file.txt"); } } }