private async Task Run() { // Enter values here so you don't have to type them every time. string projectId = @""; string bucketName = @""; if (String.IsNullOrWhiteSpace(projectId)) { Console.Write("Enter your project id: "); projectId = Console.ReadLine().Trim(); } if (String.IsNullOrWhiteSpace(bucketName)) { Console.Write("Enter your bucket name: "); bucketName = Console.ReadLine().Trim(); } GoogleCredential credential = await GoogleCredential.GetApplicationDefaultAsync(); if (credential.IsCreateScopedRequired) { credential = credential.CreateScoped(new[] { StorageService.Scope.DevstorageReadWrite }); } StorageService service = new StorageService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "GCS Sample", }); Console.WriteLine("List of buckets in current project"); Buckets buckets = await service.Buckets.List(projectId).ExecuteAsync(); 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"); await service.Objects.Insert(fileobj, bucketName, new MemoryStream(msgtxt), "text/plain").UploadAsync(); 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 = await req.ExecuteAsync(); 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 = await downloader.DownloadAsync(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 DownloadFileAsync(DriveService service, File f, string path) { var downloader = new MediaDownloader(service); System.IO.FileStream fileStream = null; downloader.ChunkSize = DownloadChunkSize; try { fileStream = new System.IO.FileStream(path, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write); var progress = await downloader.DownloadAsync(f.SelfLink, fileStream); // updating the modified date await service.Files.Touch(f.Id).ExecuteAsync(); if (progress.Status == DownloadStatus.Completed) { Console.WriteLine(path + " was downloaded successfully"); } else { Console.WriteLine(Environment.NewLine + "Download {0} was interpreted in the middle. Only {1} were downloaded. ", path, progress.BytesDownloaded); } } catch (GoogleApiException e) { if (e.HttpStatusCode == HttpStatusCode.Unauthorized) { //GoogleWebAuthorizationBroker.Folder = "Drive.Sample"; //credential.RefreshTokenAsync(CancellationToken.None).Wait(); /*using (var stream = new System.IO.FileStream("client_secrets.json", System.IO.FileMode.Open, System.IO.FileAccess.Read)) { GoogleWebAuthorizationBroker.ReauthorizeAsync(credential, CancellationToken.None).Wait(); } // Create the service. service = new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "CloudManagerGD", });*/ /*if(retries <= 4) { Thread.Sleep(retries * 1000); this.DownloadFileAsync(service, f, path).Wait(); retries++; } else { retries = 0; return; }*/ } } catch (System.IO.IOException e) { if (retries <= 4) { Thread.Sleep(retries * 1000); this.DownloadFileAsync(service, f, path).Wait(); retries++; } else { retries = 0; return; } } finally { if (fileStream != null) { fileStream.Dispose(); } } }
/// <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); } } }
/// <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); } } }
private async Task Run() { string projectId = "snowball-1053"; string bucketName = "snowball_test"; //Authentication Option 1: Credential for certificate-based service accounts. string certificateFile = "c:\\snowball-544a05c14e78.p12"; string serviceAccountEmail = "*****@*****.**"; var certificate = new X509Certificate2(certificateFile, "notasecret", X509KeyStorageFlags.Exportable); ServiceAccountCredential credential = new ServiceAccountCredential( new ServiceAccountCredential.Initializer(serviceAccountEmail) { Scopes = new[] { StorageService.Scope.DevstorageReadWrite } }.FromCertificate(certificate)); //Authentication Option 2: Credential when running in Google Compute Engine. //Requires Google API CLient library >=1.9.1 //ComputeCredential credential = new ComputeCredential(new ComputeCredential.Initializer()); //Authentication Option 3: Installed application credentials, user interactive webflow. /* UserCredential credential; string clientId = "YOUR_CLIENT_ID.apps.googleusercontent.com"; string clientSecret = "YOUR_CLIENT_SECRET"; credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets{ClientId= clientId,ClientSecret=clientSecret}, new[] { StorageService.Scope.DevstorageFullControl }, Environment.UserName, CancellationToken.None); //Console.WriteLine("Credential file saved at: " + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)); */ var service = new StorageService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "GCS Sample", }); Console.WriteLine("List of buckets in current project"); Buckets buckets = await service.Buckets.List(projectId).ExecuteAsync(); foreach (var bkt in buckets.Items) { Console.WriteLine(bkt.Name); } Console.WriteLine("Total number of items in bucket: " + buckets.Items.Count); Console.WriteLine("============================="); //var sql ='' var _fileName = string.Format("cflows-{0}", DateTime.Now.ToString("yyyy-MM-dd")); // 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 = _fileName }; Console.WriteLine("Creating " + fileobj.Name + " in bucket " + bucketName); #region prepare data //var strBuilder = new StringBuilder(); //strBuilder.AppendLine("A,B,C"); //strBuilder.AppendLine("E,F,G"); //byte[] msgtxt = Encoding.UTF8.GetBytes(strBuilder.ToString()); //using (var streamOut = new MemoryStream(msgtxt)) //{ // await service.Objects.Insert(fileobj, bucketName, streamOut, "application/vnd.ms-excel").UploadAsync(); //} _fileName = string.Format("cflows-{0}", DateTime.Now.ToString("yyyy-MM-dd")); string strFilePath = string.Format("C:\\{0}.csv", _fileName); using (FileStream fileStream = new FileStream(strFilePath, FileMode.Open, FileAccess.Read)) { await service.Objects.Insert(fileobj, bucketName, fileStream, "application/vnd.ms-excel").UploadAsync(); } #endregion 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 = await req.ExecuteAsync(); 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 += Download_ProgressChanged; using (var fileStream = new System.IO.FileStream(fileName, System.IO.FileMode.Create, System.IO.FileAccess.Write)) { var progress = await downloader.DownloadAsync(readobj.MediaLink, fileStream); if (progress.Status == DownloadStatus.Completed) { Console.WriteLine(readobj.Name + " was downloaded successfully"); } else { Console.WriteLine("Download {0} was interpreted in the middle. Only {1} were downloaded. ", readobj.Name, progress.BytesDownloaded); } } /* // or download as a stream Stream getStream = await service.HttpClient.GetStreamAsync(readobj.MediaLink); Console.WriteLine("Object Content: "); using (var reader = new StreamReader(getStream)) { Console.WriteLine(reader.ReadToEnd()); } */ Console.WriteLine("============================="); }
private async Task Run() { string projectId = "causal-bus-95919"; string bucketName = "threat-detector-main-storage"; //Authentication Option 1: Credential for certificate-based service accounts. string certificateFile = @"C:\Users\Алексей\Documents\Repositories\Course project the 4th\ThreatDetector\ThreatDetector-c02d8385fbd4.p12"; string serviceAccountEmail = "*****@*****.**"; var certificate = new X509Certificate2(certificateFile, "notasecret", X509KeyStorageFlags.Exportable); var credential = new ServiceAccountCredential( new ServiceAccountCredential.Initializer(serviceAccountEmail) { Scopes = new[] { StorageService.Scope.DevstorageReadWrite } }.FromCertificate(certificate)); //Authentication Option 2: Credential when running in Google Compute Engine. //Requires Google API CLient library >=1.9.1 //ComputeCredential credential = new ComputeCredential(new ComputeCredential.Initializer()); //Authentication Option 3: Installed application credentials, user interactive webflow. /* UserCredential credential; string clientId = "YOUR_CLIENT_ID.apps.googleusercontent.com"; string clientSecret = "YOUR_CLIENT_SECRET"; credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets{ClientId= clientId,ClientSecret=clientSecret}, new[] { StorageService.Scope.DevstorageFullControl }, Environment.UserName, CancellationToken.None); //Console.WriteLine("Credential file saved at: " + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)); */ var service = new StorageService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "GCS Sample", }); Console.WriteLine("List of buckets in current project"); Buckets buckets = await service.Buckets.List(projectId).ExecuteAsync(); foreach (var bkt in buckets.Items) { Console.WriteLine(bkt.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 var fileobj = new Google.Apis.Storage.v1.Data.Object() { Name = "t/tt/somefile.txt" }; Console.WriteLine("Creating " + fileobj.Name + " in bucket " + bucketName); byte[] msgtxt = Encoding.UTF8.GetBytes("Lorem Ipsum"); using (var streamOut = new MemoryStream(msgtxt)) { await service.Objects.Insert(fileobj, bucketName, streamOut, "text/plain").UploadAsync(); } 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 = await req.ExecuteAsync(); 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 += Download_ProgressChanged; using (var fileStream = new System.IO.FileStream(fileName, System.IO.FileMode.Create, System.IO.FileAccess.Write)) { var progress = await downloader.DownloadAsync(readobj.MediaLink, fileStream); if (progress.Status == DownloadStatus.Completed) { Console.WriteLine(readobj.Name + " was downloaded successfully"); } else { Console.WriteLine("Download {0} was interpreted in the middle. Only {1} were downloaded. ", readobj.Name, progress.BytesDownloaded); } } /* // or download as a stream Stream getStream = await service.HttpClient.GetStreamAsync(readobj.MediaLink); Console.WriteLine("Object Content: "); using (var reader = new StreamReader(getStream)) { Console.WriteLine(reader.ReadToEnd()); } */ Console.WriteLine("============================="); }
protected override void Start() { try { try { if (Status != StatusType.Queued) { throw new Exception("Stream has not been queued."); } base.Start(); _FileInfo = _DriveService.GetFile(FileId); if (_FileInfo == null) { throw new Exception("File '" + FileId + "' no longer exists."); } if (String.IsNullOrEmpty(_FileInfo.FilePath)) { throw new Exception("FileInfo.FilePath is blank."); } } catch (Exception exception) { Log.Error(exception); } try { if (_FileInfo.IsGoogleDoc) { try { API.DriveService.WriteGoogleDoc(_FileInfo); DriveService_ProgressChanged(DownloadStatus.Completed, 0, null); return; } catch (Exception exception) { Log.Error(exception); } } else if (String.IsNullOrEmpty(_FileInfo.DownloadUrl)) { try { DriveService_ProgressChanged(DownloadStatus.Completed, 0, null); return; } catch (Exception exception) { Log.Error(exception); } } else if (_FileInfo.FileSize == 0) { try { API.DriveService.CreateFile(FileInfo); DriveService_ProgressChanged(DownloadStatus.Completed, 0, null); return; } catch (Exception exception) { Log.Error(exception); } } else { FileInfoStatus fileInfoStatus = API.DriveService.GetFileInfoStatus(_FileInfo); if (fileInfoStatus == FileInfoStatus.ModifiedOnDisk || fileInfoStatus == FileInfoStatus.OnDisk) { if (CheckIfAlreadyDownloaded) { try { DriveService_ProgressChanged(DownloadStatus.Completed, 0, null); return; } catch (Exception exception) { Log.Error(exception); } } } } } catch (Exception exception) { Log.Error(exception); } try { API.DriveService.DeleteFile(_FileInfo.FilePath); _DownloadFilePath = _FileInfo.FilePath + ".download"; Lock(_DownloadFilePath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None); } catch (Exception exception) { Log.Error(exception); } try { _FileSize = API.DriveService.GetLong(_FileInfo.FileSize); _LastWriteTime = API.DriveService.GetFileLastWriteTime(_DownloadFilePath); } catch (Exception exception) { Log.Error(exception); } try { using (API.DriveService.Connection connection = API.DriveService.Connection.Create()) { var request = new MediaDownloader(connection.Service); request.ProgressChanged += DriveService_ProgressChanged; if (ChunkSize <= 0) { request.ChunkSize = API.DriveService.Settings.DownloadFileChunkSize; } else if (ChunkSize > MediaDownloader.MaximumChunkSize) { request.ChunkSize = MediaDownloader.MaximumChunkSize; } else { request.ChunkSize = ChunkSize; } _CancellationTokenSource = new System.Threading.CancellationTokenSource(); System.Threading.Tasks.Task<IDownloadProgress> task = request.DownloadAsync(_FileInfo.DownloadUrl, _FileStream, _CancellationTokenSource.Token); } } catch (Exception exception) { Log.Error(exception); } } catch (Exception exception) { try { _Status = StatusType.Failed; _ExceptionMessage = exception.Message; DriveService_ProgressChanged(DownloadStatus.Failed, 0, exception); } catch { Debugger.Break(); } Log.Error(exception); } }
/// <summary>Downloads the media from the given URL.</summary> private async Task DownloadFile(DriveService service, string url) { var downloader = new MediaDownloader(service); downloader.ChunkSize = DownloadChunkSize; // add a delegate for the progress changed event for writing to console on changes downloader.ProgressChanged += Download_ProgressChanged; // figure out the right file type base on UploadFileName extension var lastDot = UploadFileName.LastIndexOf('.'); var fileName = DownloadDirectoryName + @"\Download" + (lastDot != -1 ? "." + UploadFileName.Substring(lastDot + 1) : ""); using (var fileStream = new System.IO.FileStream(fileName, System.IO.FileMode.Create, System.IO.FileAccess.Write)) { var progress = await downloader.DownloadAsync(url, fileStream); if (progress.Status == DownloadStatus.Completed) { Console.WriteLine(fileName + " was downloaded successfully"); } else { Console.WriteLine("Download {0} was interpreted in the middle. Only {1} were downloaded. ", fileName, progress.BytesDownloaded); } } }