コード例 #1
0
        private void ResumableUploadResumeTest_InitDelayTest()
        {
            var    bucket = _fixture.SingleVersionBucket;
            var    name   = GenerateName();
            var    data   = _fixture.SmallContent;
            string url    = null;

            _fixture.RegisterDelayTest(_duration,
                                       beforeDelay: async duration =>
            {
                url            = _fixture.UrlSigner.Sign(bucket, name, duration, UrlSigner.ResumableHttpMethod);
                var sessionUri = await SignedUrlResumableUpload.InitiateSessionAsync(url);

                // Verify that the URL works initially.
                var uploader = ResumableUpload.CreateFromUploadUri(sessionUri, new MemoryStream(data));
                var progress = await uploader.ResumeAsync(sessionUri);
                Assert.Null(progress.Exception);
                Assert.Equal(UploadStatus.Completed, progress.Status);

                var result = new MemoryStream();
                await _fixture.Client.DownloadObjectAsync(bucket, name, result);
                Assert.Equal(result.ToArray(), data);

                // Reset the state.
                await _fixture.Client.DeleteObjectAsync(bucket, name);
            },
                                       afterDelay: async() =>
            {
                // Verify that the URL no longer works.
                await Assert.ThrowsAsync <GoogleApiException>(() => SignedUrlResumableUpload.InitiateSessionAsync(url));

                var obj = await _fixture.Client.ListObjectsAsync(bucket, name).FirstOrDefault(o => o.Name == name);
                Assert.Null(obj);
            });
        }
コード例 #2
0
        public async Task TestInitiatedResumableUpload(
            [CombinatorialValues("", "text/plain")] string contentType)
        {
            using (var server = new SingleChunkServer(_server))
                using (var service = new MockClientService(server.HttpPrefix))
                {
                    var content     = new MemoryStream(uploadTestBytes);
                    var tmpUploader = new TestResumableUpload(
                        service, "SingleChunk", "POST", content, contentType, uploadLength);
                    var uploadUri = await tmpUploader.InitiateSessionAsync();

                    var uploader = ResumableUpload.CreateFromUploadUri(uploadUri, content);
                    var progress = uploader.Upload();

                    Assert.Equal(2, server.Requests.Count);
                    var r0 = server.Requests[0];
                    Assert.Equal(contentType, r0.Headers["X-Upload-Content-Type"]);
                    Assert.Equal(uploadTestBytes.Length.ToString(), r0.Headers["X-Upload-Content-Length"]);
                    var r1 = server.Requests[1];
                    Assert.Equal(uploadPath, server.RemovePrefix(r1.Url.AbsolutePath));
                    Assert.Equal($"bytes 0-{uploadLength - 1}/{uploadLength}", r1.Headers["Content-Range"]);
                    Assert.Equal(UploadStatus.Completed, progress.Status);
                    Assert.Equal(uploadTestBytes.Length, progress.BytesSent);
                }
        }
コード例 #3
0
        public async Task upload(System.IO.Stream streamFile, string tokenId, string tokenKey, string name, string parent, long?length = null, string[] pathComponents = null, Action <long> onProgress = null)
        {
            var tokenHandler = new MessageTokenHandler(tokenId, tokenKey)
            {
                driveService = this
            };
            var client = new HttpClient(tokenHandler);

            // create path components
            if (pathComponents != null && pathComponents.Length > 0)
            {
                foreach (var cp in pathComponents)
                {
                    parent = await getOrCreateFolder(parent, cp, tokenId, tokenKey);
                }
            }

            var driveFile = new DriveFile()
            {
                Name = name, MimeType = "video/x-matroska", parents = new string[] { parent }
            };
            var response = await client.PostAsJsonAsync("https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable", driveFile, jsonOptions);

            var location        = response.Headers.GetValues("Location").FirstOrDefault();
            var resumableUpload = ResumableUpload.CreateFromUploadUri(new Uri(location), streamFile);

            if (onProgress != null)
            {
                resumableUpload.ProgressChanged += (Google.Apis.Upload.IUploadProgress obj) =>
                                                   onProgress(obj.BytesSent);
            }

            await resumableUpload.UploadAsync();
        }
コード例 #4
0
        private async Task <bool> UploadCaptionAsync(ResumableUpload uploadRequest, CancellationToken cancellationToken)
        {
            _logger.LogTrace($"{GetType()} - BEGIN {nameof(UploadCaptionAsync)}");

            //finally upload the request... and wait.
            //TODO Créer un taskmanager pour suivre l'avancement des taches lancées
            IUploadProgress res = await uploadRequest.UploadAsync(cancellationToken);

            UploadStatus s = res.Status;

            while (s != UploadStatus.Completed && s != UploadStatus.Failed)
            {
                _logger.LogInformation($"Caption upload in progress : {s}");
                await Task.Delay(500);
            }

            if (s == UploadStatus.Completed)
            {
                _logger.LogInformation($"Caption upload completed with success");
                return(true);
            }
            else
            {
                _logger.LogError($"Caption upload failed : {res.Exception?.Message ?? "Unexpected error ¯\\(°_o)/¯"}");
                return(false);
            }
        }
コード例 #5
0
        public void TestInitiatedResumableUpload(
            [Values("", "text/plain")] string contentType)
        {
            using (var server = new SingleChunkServer(_server))
                using (var service = new MockClientService(server.HttpPrefix))
                {
                    var content     = new MemoryStream(uploadTestBytes);
                    var tmpUploader = new TestResumableUpload(
                        service, "SingleChunk", "POST", content, contentType, uploadLength);
                    Uri uploadUri = null;
                    tmpUploader.UploadSessionData += sessionData => {
                        uploadUri = sessionData.UploadUri;
                        // Throw an exception so the upload fails.
                        throw new Exception();
                    };
                    var progress = tmpUploader.Upload();
                    Assert.That(progress.Status, Is.EqualTo(UploadStatus.Failed));

                    var uploader = ResumableUpload.CreateFromUploadUri(uploadUri, content);
                    progress = uploader.Upload();

                    Assert.That(server.Requests.Count, Is.EqualTo(2));
                    var r0 = server.Requests[0];
                    Assert.That(r0.Headers["X-Upload-Content-Type"], Is.EqualTo(contentType));
                    Assert.That(r0.Headers["X-Upload-Content-Length"], Is.EqualTo(uploadTestBytes.Length.ToString()));
                    var r1 = server.Requests[1];
                    Assert.That(server.RemovePrefix(r1.Url.AbsolutePath), Is.EqualTo(uploadPath));
                    Assert.That(r1.Headers["Content-Range"], Is.EqualTo($"bytes 0-{uploadLength - 1}/{uploadLength}"));
                    Assert.That(progress.Status, Is.EqualTo(UploadStatus.Completed));
                    Assert.That(progress.BytesSent, Is.EqualTo(uploadTestBytes.Length));
                }
        }
コード例 #6
0
        private async Task <bool> AddCaptionInternalAsync(Caption caption, Stream captionStream, CancellationToken cancellationToken)
        {
            try
            {
                _logger.LogTrace($"{GetType()} - BEGIN {nameof(AddCaptionInternalAsync)} for video {caption.Snippet.VideoId}");
                YouTubeService ytService = await _ytServiceProvider.CreateServiceAsync(cancellationToken);

                //create the request now and insert our params...
                ResumableUpload captionRequest = ytService.Captions.Insert(caption, SNIPPET_PART_PARAM, captionStream, "application/atom+xml");

                return(await UploadCaptionAsync(captionRequest, cancellationToken));
            }
            catch (Exception e)
            {
                _logger.LogError("An error occured while uploading caption", e);
                return(false);
            }
        }
コード例 #7
0
        private static void ResumableUploadResumeTest_Common(StorageFixture fixture, SigningVersion signingVersion, [CallerMemberName] string caller = null)
        {
            var bucket          = fixture.SingleVersionBucket;
            var name            = IdGenerator.FromGuid();
            var requestTemplate = RequestTemplate
                                  .FromBucket(bucket)
                                  .WithObjectName(name)
                                  .WithHttpMethod(ResumableHttpMethod);
            var    content = fixture.SmallContent;
            string url     = null;

            fixture.RegisterDelayTest(
                s_duration,
                beforeDelay: async duration =>
            {
                url            = fixture.UrlSigner.Sign(requestTemplate, Options.FromDuration(duration).WithSigningVersion(signingVersion));
                var sessionUri = await SignedUrlResumableUpload.InitiateSessionAsync(url);

                // Verify that the URL works initially.
                var uploader = ResumableUpload.CreateFromUploadUri(sessionUri, new MemoryStream(content));
                var progress = await uploader.ResumeAsync(sessionUri);
                Assert.Null(progress.Exception);
                Assert.Equal(UploadStatus.Completed, progress.Status);

                var result = new MemoryStream();
                await fixture.Client.DownloadObjectAsync(bucket, name, result);
                AssertContentEqual(content, result.ToArray());

                // Reset the state.
                await fixture.Client.DeleteObjectAsync(bucket, name);
            },
                afterDelay: async() =>
            {
                // Verify that the URL no longer works.
                await Assert.ThrowsAsync <GoogleApiException>(() => SignedUrlResumableUpload.InitiateSessionAsync(url));

                var obj = await fixture.Client.ListObjectsAsync(bucket, name).FirstOrDefaultAsync(o => o.Name == name);
                Assert.Null(obj);
            },
                caller);
        }
コード例 #8
0
        public async Task UploadObjectWithSessionUri()
        {
            var bucketName = _fixture.BucketName;

            // Sample: UploadObjectWithSessionUri
            var client      = StorageClient.Create();
            var source      = "world.txt";
            var destination = "places/world.txt";
            var contentType = "text/plain";

            // var acl = PredefinedAcl.PublicRead // public
            var acl     = PredefinedObjectAcl.AuthenticatedRead; // private
            var options = new UploadObjectOptions {
                PredefinedAcl = acl
            };
            // Create a temporary uploader so the upload session can be manually initiated without actually uploading.
            var tempUploader = client.CreateObjectUploader(bucketName, destination, contentType, new MemoryStream(), options);
            var uploadUri    = await tempUploader.InitiateSessionAsync();

            // Send uploadUri to (unauthenticated) client application, so it can perform the upload:
            using (var stream = File.OpenRead(source))
            {
                // IUploadProgress defined in Google.Apis.Upload namespace
                IProgress <IUploadProgress> progress = new Progress <IUploadProgress>(
                    p => Console.WriteLine($"bytes: {p.BytesSent}, status: {p.Status}")
                    );

                var actualUploader = ResumableUpload.CreateFromUploadUri(uploadUri, stream);
                actualUploader.ProgressChanged += progress.Report;
                await actualUploader.UploadAsync();
            }
            // End sample

            // want to show the source in the snippet, but also
            // want to make sure it matches the one in the fixture
            Assert.Equal(source, _fixture.WorldLocalFileName);
        }
コード例 #9
0
        /// <summary>
        /// Uploads the file onto Google Storage.
        /// </summary>
        private async Task GSUpload()
        {
            OnGSUploading(EventArgs.Empty);

            try
            {
                var client      = StorageClient.Create();
                var objectName  = System.IO.Path.GetFileName(ReencodedPath);
                var contentType = "text/plain";

                var options = new UploadObjectOptions();
                // Create a temporary uploader so the upload session can be manually initiated without actually uploading.
                var tempUploader = client.CreateObjectUploader(Bucket, objectName, contentType, new MemoryStream(), options);
                var uploadUri    = await tempUploader.InitiateSessionAsync();

                // Send uploadUri to (unauthenticated) client application, so it can perform the upload:
                using (var stream = File.OpenRead(ReencodedPath))
                {
                    IProgress <IUploadProgress> progress = new Progress <IUploadProgress>(
                        p => { GSUploadProgress = (float)p.BytesSent / stream.Length * 100; OnGSUploading(EventArgs.Empty); }
                        );

                    var actualUploader = ResumableUpload.CreateFromUploadUri(uploadUri, stream);
                    actualUploader.ProgressChanged += progress.Report;
                    actualUploader.ChunkSize        = ResumableUpload.MinimumChunkSize * 4;
                    await actualUploader.UploadAsync();
                }

                GSUri = GetGSUriFromObjectName(Bucket, objectName);
                OnGSUploaded(EventArgs.Empty);
            }
            catch (Exception e)
            {
                throw new GSUploadException("An error occured while uploading the file.", e);
            }
        }
コード例 #10
0
ファイル: Class1.cs プロジェクト: bj-rn/vl.youtube
        public static ResumableUpload RemoveResponseEventHandler <T> (this ResumableUpload <T, T> upload, Action <T> callback)
        {
            upload.ResponseReceived -= callback;

            return(upload);
        }
コード例 #11
0
ファイル: Class1.cs プロジェクト: bj-rn/vl.youtube
        public static ResumableUpload RemoveProgressEventHandler <T> (this ResumableUpload <T> upload, Action <IUploadProgress> callback)
        {
            upload.ProgressChanged -= callback;

            return(upload);
        }