Пример #1
0
 /// <inheritdoc />
 public override Task <Object> UploadObjectAsync(
     Object destination,
     Stream source,
     UploadObjectOptions options          = null,
     CancellationToken cancellationToken  = default,
     IProgress <IUploadProgress> progress = null) =>
 new UploadHelper(this, destination, source, options, progress).ExecuteAsync(cancellationToken);
Пример #2
0
        public async Task UploadImageAsync()
        {
            string imageHash = HashFile(imageFilename);

            if (FileExistsInCloud(imageHash))
            {
                form.AppendToConsole("Image already exists.");
                return;
            }

            var imageObject = new Google.Apis.Storage.v1.Data.Object
            {
                Bucket      = bucketName,
                Name        = imageHash,
                ContentType = "image/png"
            };

            using (var stream = new System.IO.FileStream(imageFilename, System.IO.FileMode.Open))
            {
                var uploadObjectOptions = new Google.Cloud.Storage.V1.UploadObjectOptions
                {
                    ChunkSize = Google.Cloud.Storage.V1.UploadObjectOptions.MinimumChunkSize
                };

                await storage.UploadObjectAsync(
                    imageObject,
                    stream,
                    uploadObjectOptions
                    );

                form.AppendToConsole("Done!");
            }
        }
Пример #3
0
 /// <summary>
 /// Creates an instance which is capable of starting a resumable upload for an object.
 /// </summary>
 /// <param name="destination">Object to create or update. Must not be null, and must have the name,
 /// bucket and content type populated.</param>
 /// <param name="source">The stream to read the data from. Must not be null.</param>
 /// <param name="options">Additional options for the upload. May be null, in which case appropriate
 /// defaults will be used.</param>
 /// <returns>The <see cref="ObjectsResource.InsertMediaUpload"/> which can be used to upload the object.</returns>
 /// <seealso cref="UploadObject(Object,Stream,UploadObjectOptions,IProgress{IUploadProgress})"/>
 public virtual ObjectsResource.InsertMediaUpload CreateObjectUploader(
     Object destination,
     Stream source,
     UploadObjectOptions options = null)
 {
     throw new NotImplementedException();
 }
        /// <inheritdoc />
        public override async Task <Object> UploadObjectAsync(
            Object destination,
            Stream source,
            UploadObjectOptions options          = null,
            CancellationToken cancellationToken  = default(CancellationToken),
            IProgress <IUploadProgress> progress = null)
        {
            ValidateObject(destination, nameof(destination));
            GaxPreconditions.CheckNotNull(source, nameof(source));
            var mediaUpload = new CustomMediaUpload(Service, destination, destination.Bucket, source, destination.ContentType);

            options?.ModifyMediaUpload(mediaUpload);
            ApplyEncryptionKey(options?.EncryptionKey, mediaUpload);
            if (progress != null)
            {
                mediaUpload.ProgressChanged += progress.Report;
            }
            var finalProgress = await mediaUpload.UploadAsync(cancellationToken).ConfigureAwait(false);

            if (finalProgress.Exception != null)
            {
                throw finalProgress.Exception;
            }
            return(mediaUpload.ResponseBody);
        }
        /// <inheritdoc />
        public override Object UploadObject(
            Object destination,
            Stream source,
            UploadObjectOptions options          = null,
            IProgress <IUploadProgress> progress = null)
        {
            ValidateObject(destination, nameof(destination));
            GaxPreconditions.CheckNotNull(source, nameof(source));
            var mediaUpload = new CustomMediaUpload(Service, destination, destination.Bucket, source, destination.ContentType);

            options?.ModifyMediaUpload(mediaUpload);
            ApplyEncryptionKey(options?.EncryptionKey, mediaUpload);
            if (progress != null)
            {
                mediaUpload.ProgressChanged += progress.Report;
            }
            var finalProgress = mediaUpload.Upload();

            if (finalProgress.Exception != null)
            {
                throw finalProgress.Exception;
            }

            return(mediaUpload.ResponseBody);
        }
Пример #6
0
 /// <summary>
 /// Uploads the data for an object in storage synchronously, from a specified stream.
 /// </summary>
 /// <param name="destination">Object to create or update. Must not be null, and must have the name,
 /// bucket and content type populated.</param>
 /// <param name="source">The stream to read the data from. Must not be null.</param>
 /// <param name="options">Additional options for the upload. May be null, in which case appropriate
 /// defaults will be used.</param>
 /// <param name="progress">Progress reporter for the upload. May be null.</param>
 /// <returns>The <see cref="Object"/> representation of the uploaded object.</returns>
 public virtual Object UploadObject(
     Object destination,
     Stream source,
     UploadObjectOptions options          = null,
     IProgress <IUploadProgress> progress = null)
 {
     throw new NotImplementedException();
 }
Пример #7
0
 /// <summary>
 /// Uploads the data for an object in storage asynchronously, from a specified stream.
 /// </summary>
 /// <param name="destination">Object to create or update. Must not be null, and must have the name,
 /// bucket and content type populated.</param>
 /// <param name="source">The stream to read the data from. Must not be null.</param>
 /// <param name="options">Additional options for the upload. May be null, in which case appropriate
 /// defaults will be used.</param>
 /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
 /// <param name="progress">Progress reporter for the upload. May be null.</param>
 /// <returns>A task representing the asynchronous operation, with a result returning the
 /// <see cref="Object"/> representation of the uploaded object.</returns>
 public virtual Task <Object> UploadObjectAsync(
     Object destination,
     Stream source,
     UploadObjectOptions options          = null,
     CancellationToken cancellationToken  = default(CancellationToken),
     IProgress <IUploadProgress> progress = null)
 {
     throw new NotImplementedException();
 }
Пример #8
0
 /// <summary>
 /// Creates an instance which is capable of starting a resumable upload for an object.
 /// </summary>
 /// <param name="bucket">The name of the bucket containing the object. Must not be null.</param>
 /// <param name="objectName">The name of the object within the bucket. Must not be null.</param>
 /// <param name="contentType">The content type of the object. This should be a MIME type
 /// such as "text/html" or "application/octet-stream". May be null.</param>
 /// <param name="source">The stream to read the data from. Must not be null.</param>
 /// <param name="options">Additional options for the upload. May be null, in which case appropriate
 /// defaults will be used.</param>
 /// <returns>The <see cref="ObjectsResource.InsertMediaUpload"/> which can be used to upload the object.</returns>
 /// <seealso cref="UploadObject(Object,Stream,UploadObjectOptions,IProgress{IUploadProgress})"/>
 public virtual ObjectsResource.InsertMediaUpload CreateObjectUploader(
     string bucket,
     string objectName,
     string contentType,
     Stream source,
     UploadObjectOptions options = null)
 {
     throw new NotImplementedException();
 }
Пример #9
0
 /// <summary>
 /// Uploads the data for an object in storage synchronously, from a specified stream.
 /// </summary>
 /// <param name="bucket">The name of the bucket containing the object. Must not be null.</param>
 /// <param name="objectName">The name of the object within the bucket. Must not be null.</param>
 /// <param name="contentType">The content type of the object. This should be a MIME type
 /// such as "text/html" or "application/octet-stream". May be null.</param>
 /// <param name="source">The stream to read the data from. Must not be null.</param>
 /// <param name="options">Additional options for the upload. May be null, in which case appropriate
 /// defaults will be used.</param>
 /// <param name="progress">Progress reporter for the upload. May be null.</param>
 /// <returns>The <see cref="Object"/> representation of the uploaded object.</returns>
 public virtual Object UploadObject(
     string bucket,
     string objectName,
     string contentType,
     Stream source,
     UploadObjectOptions options          = null,
     IProgress <IUploadProgress> progress = null)
 {
     throw new NotImplementedException();
 }
Пример #10
0
 /// <summary>
 /// Uploads the data for an object in storage asynchronously, from a specified stream.
 /// </summary>
 /// <param name="bucket">The name of the bucket containing the object. Must not be null.</param>
 /// <param name="objectName">The name of the object within the bucket. Must not be null.</param>
 /// <param name="contentType">The content type of the object. This should be a MIME type
 /// such as "text/html" or "application/octet-stream". May be null.</param>
 /// <param name="source">The stream to read the data from. Must not be null.</param>
 /// <param name="options">Additional options for the upload. May be null, in which case appropriate
 /// defaults will be used.</param>
 /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
 /// <param name="progress">Progress reporter for the upload. May be null.</param>
 /// <returns>A task representing the asynchronous operation, with a result returning the
 /// <see cref="Object"/> representation of the uploaded object.</returns>
 public virtual Task <Object> UploadObjectAsync(
     string bucket,
     string objectName,
     string contentType,
     Stream source,
     UploadObjectOptions options          = null,
     CancellationToken cancellationToken  = default(CancellationToken),
     IProgress <IUploadProgress> progress = null)
 {
     throw new NotImplementedException();
 }
        /// <inheritdoc />
        public override ObjectsResource.InsertMediaUpload CreateObjectUploader(
            Object destination,
            Stream source,
            UploadObjectOptions options = null)
        {
            ValidateObject(destination, nameof(destination));
            GaxPreconditions.CheckNotNull(source, nameof(source));
            var mediaUpload = new CustomMediaUpload(Service, destination, destination.Bucket, source, destination.ContentType);

            options?.ModifyMediaUpload(mediaUpload);
            ApplyEncryptionKey(options?.EncryptionKey, options?.KmsKeyName, mediaUpload);
            return(mediaUpload);
        }
Пример #12
0
 /// <inheritdoc />
 public override ObjectsResource.InsertMediaUpload CreateObjectUploader(
     string bucket,
     string objectName,
     string contentType,
     Stream source,
     UploadObjectOptions options = null)
 {
     ValidateBucketName(bucket);
     GaxPreconditions.CheckNotNull(objectName, nameof(objectName));
     return(CreateObjectUploader(
                new Object {
         Bucket = bucket, Name = objectName, ContentType = contentType
     },
                source, options));
 }
Пример #13
0
        public async Task UploadVideoAsync()
        {
            var baseFileName = dashTitle;
            var fileName     = baseFileName;

            int i = 2;

            while (FileExistsInCloud(fileName + outputExt))
            {
                fileName = baseFileName + "-" + i;
                i++;
            }

            var videoObject = new Google.Apis.Storage.v1.Data.Object
            {
                Bucket      = bucketName,
                Name        = fileName + outputExt,
                ContentType = "video/webm"
            };

            var metadataDict = new Dictionary <string, string>
            {
                { "title", titleText },
                { "endpoint", fileName },
                { "image_endpoint", HashFile(imageFilename) },
            };

            videoObject.Metadata = metadataDict;
            endpoint             = fileName;

            using (var stream = new System.IO.FileStream(outputFile, System.IO.FileMode.Open))
            {
                form.UploadProgressBar.Maximum = (int)stream.Length;

                var uploadObjectOptions = new Google.Cloud.Storage.V1.UploadObjectOptions
                {
                    ChunkSize = Google.Cloud.Storage.V1.UploadObjectOptions.MinimumChunkSize
                };

                var progressReporter = new System.Progress <Google.Apis.Upload.IUploadProgress>(OnUploadProgress);
                await storage.UploadObjectAsync(
                    videoObject,
                    stream,
                    uploadObjectOptions,
                    progress : progressReporter
                    );
            }
        }
Пример #14
0
 /// <inheritdoc />
 public override Task <Object> UploadObjectAsync(
     string bucket,
     string objectName,
     string contentType,
     Stream source,
     UploadObjectOptions options          = null,
     CancellationToken cancellationToken  = default(CancellationToken),
     IProgress <IUploadProgress> progress = null)
 {
     ValidateBucketName(bucket);
     GaxPreconditions.CheckNotNull(objectName, nameof(objectName));
     return(UploadObjectAsync(new Object {
         Bucket = bucket, Name = objectName, ContentType = contentType
     },
                              source, options, cancellationToken, progress));
 }
Пример #15
0
 /// <inheritdoc />
 public override Object UploadObject(
     string bucket,
     string objectName,
     string contentType,
     Stream source,
     UploadObjectOptions options          = null,
     IProgress <IUploadProgress> progress = null)
 {
     ValidateBucketName(bucket);
     GaxPreconditions.CheckNotNull(objectName, nameof(objectName));
     return(UploadObject(
                new Object {
         Bucket = bucket, Name = objectName, ContentType = contentType
     },
                source, options, progress));
 }
Пример #16
0
        /// <inheritdoc />
        public override Object UploadObject(
            Object destination,
            Stream source,
            UploadObjectOptions options          = null,
            IProgress <IUploadProgress> progress = null)
        {
            var mediaUpload = CreateObjectUploader(destination, source, options);

            if (progress != null)
            {
                mediaUpload.ProgressChanged += progress.Report;
            }
            var finalProgress = mediaUpload.Upload();

            if (finalProgress.Exception != null)
            {
                throw finalProgress.Exception;
            }

            return(mediaUpload.ResponseBody);
        }
Пример #17
0
        /// <inheritdoc />
        public override async Task <Object> UploadObjectAsync(
            Object destination,
            Stream source,
            UploadObjectOptions options          = null,
            CancellationToken cancellationToken  = default(CancellationToken),
            IProgress <IUploadProgress> progress = null)
        {
            var mediaUpload = CreateObjectUploader(destination, source, options);

            if (progress != null)
            {
                mediaUpload.ProgressChanged += progress.Report;
            }
            var finalProgress = await mediaUpload.UploadAsync(cancellationToken).ConfigureAwait(false);

            if (finalProgress.Exception != null)
            {
                throw finalProgress.Exception;
            }
            return(mediaUpload.ResponseBody);
        }
Пример #18
0
            internal UploadHelper(
                StorageClient client,
                Object destination,
                Stream source,
                UploadObjectOptions options,
                IProgress <IUploadProgress> progress)
            {
                _client      = client;
                _mediaUpload = client.CreateObjectUploader(destination, source, options);
                if (progress != null)
                {
                    _mediaUpload.ProgressChanged += progress.Report;
                }

                var validationMode = options?.UploadValidationMode ?? UploadObjectOptions.DefaultValidationMode;

                GaxPreconditions.CheckEnumValue(validationMode, nameof(UploadObjectOptions.UploadValidationMode));
                switch (validationMode)
                {
                case UploadValidationMode.DeleteAndThrow:
                    _crc = new Crc32c();
                    _mediaUpload.UploadStreamInterceptor += _crc.UpdateHash;
                    _validationFailureAction              = obj => client.DeleteObject(obj, new DeleteObjectOptions {
                        Generation = obj.Generation
                    });
                    _validationFailureAsyncAction = (obj, token) => client.DeleteObjectAsync(obj, new DeleteObjectOptions {
                        Generation = obj.Generation
                    }, token);
                    break;

                case UploadValidationMode.ThrowOnly:
                    _crc = new Crc32c();
                    _mediaUpload.UploadStreamInterceptor += _crc.UpdateHash;
                    break;
                }
            }
Пример #19
0
 /// <inheritdoc />
 public override Object UploadObject(
     Object destination,
     Stream source,
     UploadObjectOptions options          = null,
     IProgress <IUploadProgress> progress = null) =>
 new UploadHelper(this, destination, source, options, progress).Execute();