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);
     }
 }
 //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;
 }
Exemplo n.º 3
0
        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 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();
                }
            }
        }
Exemplo n.º 5
0
            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);
                }
            }
        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);
            }
        }
        /// <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);
                }
            }
        }
        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);
            }
        }
        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));
            }
        }
        /// <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);
            }
        }
Exemplo n.º 11
0
      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("=============================");
      }
Exemplo n.º 12
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");
                }
            }
        }
Exemplo n.º 13
0
        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("=============================");
        }
        /// <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 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));
            }
        }
        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()
        {
            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);
            }
        }
Exemplo n.º 18
0
        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("=============================");
        }
        /// <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);
                }
            }
        }