예제 #1
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 async Task <Object> UploadObjectAsync(
            Object destination,
            Stream source,
            UploadObjectOptions options          = null,
            CancellationToken cancellationToken  = default(CancellationToken),
            IProgress <IUploadProgress> progress = null)
        {
            ValidateObject(destination, nameof(destination));
            Preconditions.CheckArgument(destination.ContentType != null, nameof(destination), "Object must have a ContentType");
            Preconditions.CheckNotNull(source, nameof(source));
            var mediaUpload = Service.Objects.Insert(destination, destination.Bucket, source, destination.ContentType);

            options?.ModifyMediaUpload(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);
        }
예제 #2
0
        /// <inheritdoc />
        public override Object UploadObject(
            Object destination,
            Stream source,
            UploadObjectOptions options          = null,
            IProgress <IUploadProgress> progress = null)
        {
            ValidateObject(destination, nameof(destination));
            GaxRestPreconditions.CheckNotNull(source, nameof(source));
            var mediaUpload = Service.Objects.Insert(destination, destination.Bucket, source, destination.ContentType);

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

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