コード例 #1
0
 /// <summary>
 /// This is only here so it can implement an interface which so it can be mocked.
 /// </summary>
 /// <param name="source"></param>
 /// <param name="destination"></param>
 /// <param name="options"></param>
 /// <param name="progress"></param>
 public override void DownloadObject(Object source,
                                     Stream destination,
                                     DownloadObjectOptions options          = null,
                                     IProgress <IDownloadProgress> progress = null)
 {
     base.DownloadObject(source, destination, options, progress);
 }
コード例 #2
0
        public void InvalidDownloadValidationMode()
        {
            var options = new DownloadObjectOptions {
                DownloadValidationMode = (DownloadValidationMode)12345
            };

            Assert.Throws <ArgumentException>(() => _fixture.Client.DownloadObject(_fixture.ReadBucket, "irrelevant.txt", new MemoryStream(), options));
        }
コード例 #3
0
        public void ModifyDownloader_DefaultOptions()
        {
            var downloader = new MediaDownloader(null);
            var options    = new DownloadObjectOptions();

            options.ModifyDownloader(downloader);
            Assert.Equal(MediaDownloader.MaximumChunkSize, downloader.ChunkSize);
        }
コード例 #4
0
        internal static void ValidateData(string bucketName, string objectName, byte[] expectedData,
                                          StorageClient client = null, DownloadObjectOptions options = null)
        {
            client = client ?? StorageClient.Create();
            var downloaded = new MemoryStream();

            client.DownloadObject(bucketName, objectName, downloaded, options);
            Assert.Equal(expectedData, downloaded.ToArray());
        }
コード例 #5
0
 public override Task DownloadObjectAsync(
     Object source,
     Stream destination,
     DownloadObjectOptions options          = null,
     CancellationToken cancellationToken    = default(CancellationToken),
     IProgress <IDownloadProgress> progress = null)
 {
     return(Task.Run(() => DownloadObject(source, destination, options, progress)));
 }
コード例 #6
0
        public void ModifyDownloader_WithChunkSize()
        {
            var downloader = new MediaDownloader(null);
            var options    = new DownloadObjectOptions {
                ChunkSize = 2048
            };

            options.ModifyDownloader(downloader);
            Assert.Equal(2048, downloader.ChunkSize);
        }
コード例 #7
0
 public Task DownloadRecordAsync(StorageRecord record, Stream destination, int?chunkSize = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(UseStorageClient(client =>
     {
         var options = new DownloadObjectOptions();
         if (chunkSize.HasValue)
         {
             options.ChunkSize = chunkSize.Value;
         }
         return client.DownloadObjectAsync(record.GoogleObject, destination, options, cancellationToken);
     }));
 }
コード例 #8
0
        public void ModifyDownloader_NegativeMatchOptions()
        {
            var options = new DownloadObjectOptions
            {
                IfGenerationNotMatch     = 1L,
                IfMetagenerationNotMatch = 2L,
                Generation = 3L
            };
            var uri = options.GetUri("http://url/foo?x=y");

            Assert.Contains("&ifGenerationNotMatch=1", uri);
            Assert.Contains("&ifMetagenerationNotMatch=2", uri);
            Assert.Contains("&generation=3", uri);
        }
コード例 #9
0
 /// <summary>
 /// This version of download() gets a range of bytes. Since this is a partial download,
 /// this function DOES NOT flush the stream object.
 /// </summary>
 public void download(string bucket_name, string key_name, Stream stream, long start, long length)
 {
     try
     {
         var download_options = new DownloadObjectOptions();
         download_options.Range = new System.Net.Http.Headers.RangeHeaderValue(start, start + length - 1);
         client.DownloadObject(bucket_name, key_name, stream, download_options);
     }
     catch (Exception ex)
     {
         throw new Exception("Error while trying to download from GCP \""
                             + bucket_name + "\\" + key_name + "\". GCP error message: "
                             + ex.Message);
     }
 }
コード例 #10
0
 public void GetUri_MatchNotMatchConflicts()
 {
     Assert.Throws <ArgumentException>(() =>
     {
         var options = new DownloadObjectOptions {
             IfGenerationMatch = 1L, IfGenerationNotMatch = 2L
         };
         options.GetUri("http://url/foo?x=y");
     });
     Assert.Throws <ArgumentException>(() =>
     {
         var options = new DownloadObjectOptions {
             IfMetagenerationMatch = 1L, IfMetagenerationNotMatch = 2L
         };
         options.GetUri("http://url/foo?x=y");
     });
 }
コード例 #11
0
        public void GetUri_PositiveMatchOptions()
        {
            var options = new DownloadObjectOptions
            {
                IfGenerationMatch     = 1L,
                IfMetagenerationMatch = 2L,
                Generation            = 3L,
                UserProject           = "my proj"
            };
            var uri = options.GetUri("http://url/foo?x=y");

            Assert.Contains("&ifGenerationMatch=1", uri);
            Assert.Contains("&ifMetagenerationMatch=2", uri);
            Assert.Contains("&generation=3", uri);
            // I don't expect project IDs to actually need escaping, but let's check that it works.
            Assert.Contains("&userProject=my%20proj", uri);
        }
コード例 #12
0
        // *************************************************************************************************************
        // DOWNLOAD OBJECT

        public override void DownloadObject(
            string bucket,
            string objectName,
            Stream destination,
            DownloadObjectOptions options          = null,
            IProgress <IDownloadProgress> progress = null)
        {
            var key = Key(bucket, objectName);

            if (_entries.TryGetValue(key, out var entry))
            {
                destination.Write(entry.Data, 0, entry.Data.Length);
                destination.Flush();
                return;
            }
            // FIXME: Google compatible exception
            throw new InvalidOperationException($"No object found for {key}.");
        }
コード例 #13
0
        public async Task DownloadAsync(string fileName, Stream stream, BytesRange range = default, CancellationToken ct = default)
        {
            Guard.NotNullOrEmpty(fileName);

            try
            {
                var downloadOptions = new DownloadObjectOptions();

                if (range.IsDefined)
                {
                    downloadOptions.Range = new RangeHeaderValue(range.From, range.To);
                }

                await storageClient.DownloadObjectAsync(bucketName, fileName, stream, downloadOptions, ct);
            }
            catch (GoogleApiException ex) when(ex.HttpStatusCode == HttpStatusCode.NotFound)
            {
                throw new AssetNotFoundException(fileName, ex);
            }
        }
コード例 #14
0
        public void ModifyDownloader_NegativeMatchOptions()
        {
            var options = new DownloadObjectOptions
            {
                IfGenerationNotMatch     = 1L,
                IfMetagenerationNotMatch = 2L,
                Generation = 3L
            };
            var builder = new RequestBuilder {
                BaseUri = new Uri("http://url/"), Path = "foo"
            };

            options.ModifyRequestBuilder(builder);
            // The replacement here simplifies the tests, to avoid needing to care which query parameter was first.
            var uri = builder.BuildUri().AbsoluteUri.Replace('?', '&');

            Assert.Contains("&ifGenerationNotMatch=1", uri);
            Assert.Contains("&ifMetagenerationNotMatch=2", uri);
            Assert.Contains("&generation=3", uri);
        }
コード例 #15
0
        public static async System.Threading.Tasks.Task <Stream> DownloadObjectAsync(string bucket, string objectName, string name, string serviceAccountName, string serviceAccountKeyFile, string clientId, Stream objectStream)
        {
            if (objectStream is null)
            {
                throw new ArgumentNullException(nameof(objectStream));
            }
            var credential = GoogleCredential.FromFile(serviceAccountKeyFile);

            using (StorageClient storageClient = StorageClient.Create(credential))
            {
                DownloadObjectOptions options = new DownloadObjectOptions()
                {
                    DownloadValidationMode = DownloadValidationMode.Always
                };

                await storageClient.DownloadObjectAsync(bucket, objectName, objectStream, options);
            }

            return(objectStream);
        }
コード例 #16
0
 public void GetUri_MatchNotMatchConflicts()
 {
     Assert.Throws <ArgumentException>(() =>
     {
         var options = new DownloadObjectOptions {
             IfGenerationMatch = 1L, IfGenerationNotMatch = 2L
         };
         var builder = new RequestBuilder {
             BaseUri = new Uri("http://url/"), Path = "foo"
         };
         options.ModifyRequestBuilder(builder);
     });
     Assert.Throws <ArgumentException>(() =>
     {
         var options = new DownloadObjectOptions {
             IfMetagenerationMatch = 1L, IfMetagenerationNotMatch = 2L
         };
         var builder = new RequestBuilder {
             BaseUri = new Uri("http://url/"), Path = "foo"
         };
         options.ModifyRequestBuilder(builder);
     });
 }
コード例 #17
0
        public void GetUri_PositiveMatchOptions()
        {
            var options = new DownloadObjectOptions
            {
                IfGenerationMatch     = 1L,
                IfMetagenerationMatch = 2L,
                Generation            = 3L,
                UserProject           = "my proj"
            };
            var builder = new RequestBuilder {
                BaseUri = new Uri("http://url/"), Path = "foo"
            };

            options.ModifyRequestBuilder(builder);
            // The replacement here simplifies the tests, to avoid needing to care which query parameter was first.
            var uri = builder.BuildUri().AbsoluteUri.Replace('?', '&');

            Assert.Contains("&ifGenerationMatch=1", uri);
            Assert.Contains("&ifMetagenerationMatch=2", uri);
            Assert.Contains("&generation=3", uri);
            // I don't expect project IDs to actually need escaping, but let's check that it works.
            Assert.Contains("&userProject=my%20proj", uri);
        }
コード例 #18
0
ファイル: Form1.cs プロジェクト: hungdang3195/GoogleCloud
        private async void btn_download_Click(object sender, EventArgs e)
        {
            var dlg = new SaveFileDialog();

            dlg.FileName = lbl_file.Text;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                var token = new CancellationTokenSource().Token;

                using (var fileStream = File.Create(dlg.FileName))
                {
                    progressBar1.Maximum = int.Parse(lbl_byte.Text);

                    var downloadObjectOptions = new DownloadObjectOptions
                    {
                        ChunkSize = UploadObjectOptions.MinimumChunkSize
                    };
                    var progressReporter = new Progress <IDownloadProgress>(OnDownloadProgress);

                    await storageClient.DownloadObjectAsync(bucketName, Path.GetFileName(dlg.FileName), fileStream, downloadObjectOptions, token, progress : progressReporter).ConfigureAwait(true);
                }
            }
        }
コード例 #19
0
        /// <summary>
        ///
        /// <para>DownloadFile:</para>
        ///
        /// <para>Downloads a file from File Service and stores locally/or to stream, caller thread will be blocked before it is done</para>
        ///
        /// <para>Check <seealso cref="IBFileServiceInterface.DownloadFile"/> for detailed documentation</para>
        ///
        /// </summary>
        public bool DownloadFile(
            string _BucketName,
            string _KeyInBucket,
            BStringOrStream _Destination,
            Action <string> _ErrorMessageAction = null,
            UInt64 _StartIndex = 0,
            UInt64 _Size       = 0)
        {
            if (GSClient == null)
            {
                _ErrorMessageAction?.Invoke("BFileServiceGC->DownloadFile: GSClient is null.");
                return(false);
            }

            if (!CheckFileExistence(
                    _BucketName,
                    _KeyInBucket,
                    out bool bExists,
                    _ErrorMessageAction
                    ))
            {
                _ErrorMessageAction?.Invoke("BFileServiceGC->DownloadFile: CheckFileExistence failed.");
                return(false);
            }

            if (!bExists)
            {
                _ErrorMessageAction?.Invoke("BFileServiceGC->DownloadFile: File does not exist in the File Service.");
                return(false);
            }

            DownloadObjectOptions DOO = null;

            if (_Size > 0)
            {
                DOO = new DownloadObjectOptions()
                {
                    Range = new System.Net.Http.Headers.RangeHeaderValue((long)_StartIndex, (long)(_StartIndex + _Size))
                };
            }

            try
            {
                if (_Destination.Type == EBStringOrStreamEnum.String)
                {
                    using (FileStream FS = File.Create(_Destination.String))
                    {
                        GSClient.DownloadObject(_BucketName, _KeyInBucket, FS, DOO);
                    }

                    if (!BUtility.DoesFileExist(
                            _Destination.String,
                            out bool bLocalFileExists,
                            _ErrorMessageAction))
                    {
                        _ErrorMessageAction?.Invoke("BFileServiceGC->DownloadFile: DoesFileExist failed.");
                        return(false);
                    }

                    if (!bLocalFileExists)
                    {
                        _ErrorMessageAction?.Invoke("BFileServiceGC->DownloadFile: Download finished, but still file does not locally exist.");
                        return(false);
                    }
                }
                else
                {
                    if (_Destination.Stream == null)
                    {
                        _ErrorMessageAction?.Invoke("BFileServiceGC->DownloadFile: Destination stream is null.");
                        return(false);
                    }

                    GSClient.DownloadObject(_BucketName, _KeyInBucket, _Destination.Stream, DOO);
                    try
                    {
                        _Destination.Stream.Position = 0;
                    }
                    catch (Exception) { }
                }
            }
            catch (Exception e)
            {
                _ErrorMessageAction?.Invoke("BFileServiceGC->DownloadFile: " + e.Message + ", Trace: " + e.StackTrace);
                return(false);
            }
            return(true);
        }
コード例 #20
0
 internal static void ValidateData(string bucketName, string objectName, MemoryStream expectedData,
                                   StorageClient client = null, DownloadObjectOptions options = null) =>
 ValidateData(bucketName, objectName, expectedData.ToArray(), client, options);
コード例 #21
0
ファイル: GoogleStorage.cs プロジェクト: mjb501/gcp
 public Task DownloadObjectAsync(string bucket, string objectName, Stream destination, DownloadObjectOptions options = null)
 {
     return(_wrappedClient.DownloadObjectAsync(bucket, objectName, destination, options));
 }
コード例 #22
0
        public virtual void DownloadObject(string bucket, string objectName, Stream destination, DownloadObjectOptions options = null, IProgress <IDownloadProgress> progress = null)
        {
            var client      = StorageClient.Create();
            var source      = "greetings/hello.txt";
            var destination = "hello.txt";

            using (var stream = File.Create(destination))
            {
                // IDownloadProgress defined in Google.Apis.Download namespace
                var progress = new Progress <IDownloadProgress>(
                    p => Console.WriteLine($"bytes: {p.BytesDownloaded}, status: {p.Status}")
                    );

                // Download source object from bucket to local file system
                client.DownloadObject(bucket, source, stream, null, progress);
            }
        }