コード例 #1
0
            internal async Task <Object> ExecuteAsync(CancellationToken cancellationToken)
            {
                await _mediaUpload.UploadAsync(cancellationToken).ConfigureAwait(false);

                CheckFinalProgress();
                var result = _mediaUpload.ResponseBody;
                var hash   = _crc == null ? result.Crc32c : Convert.ToBase64String(_crc.GetHash());

                if (hash != result.Crc32c)
                {
                    AggregateException additionalFailures = null;
                    try
                    {
                        if (_validationFailureAsyncAction != null)
                        {
                            await _validationFailureAsyncAction(result, cancellationToken).ConfigureAwait(false);
                        }
                    }
                    catch (Exception e)
                    {
                        additionalFailures = new AggregateException(e);
                    }
                    throw new UploadValidationException(hash, result, additionalFailures);
                }
                return(result);
            }
コード例 #2
0
        public async Task <bool> UploadToGoogleCloudStorage(string bucketName, string token, string filePath, string contentType)
        {
            var newObject = new Google.Apis.Storage.v1.Data.Object()
            {
                Bucket = bucketName,
                Name   = System.IO.Path.GetFileNameWithoutExtension(filePath)
            };
            var service = new Google.Apis.Storage.v1.StorageService();

            try
            {
                using (var fileStream = new FileStream(filePath, FileMode.Open))
                {
                    var uploadRequest = new ObjectsResource.InsertMediaUpload(service, newObject, bucketName, fileStream, contentType);
                    uploadRequest.OauthToken       = token;
                    uploadRequest.ProgressChanged += UploadProgress;
                    uploadRequest.ChunkSize        = (256 * 1024);
                    await uploadRequest.UploadAsync().ConfigureAwait(false);

                    service.Dispose();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }
            return(true);
        }
コード例 #3
0
        /// <summary>
        /// Gets a writer used to upload data to a Google Cloud Storage object. Used by Set-Content.
        /// </summary>
        /// <param name="path">The path of the object to upload to.</param>
        /// <returns>The writer.</returns>
        public IContentWriter GetContentWriter(string path)
        {
            var    gcsPath = GcsPath.Parse(path);
            Object body    = new Object
            {
                Name   = gcsPath.ObjectPath,
                Bucket = gcsPath.Bucket
            };
            var inputStream  = new AnonymousPipeServerStream(PipeDirection.Out);
            var outputStream = new AnonymousPipeClientStream(PipeDirection.In, inputStream.ClientSafePipeHandle);
            var contentType  = ((GcsGetContentWriterDynamicParameters)DynamicParameters).ContentType ?? GcsCmdlet.UTF8TextMimeType;

            ObjectsResource.InsertMediaUpload request =
                Service.Objects.Insert(body, gcsPath.Bucket, outputStream, contentType);
            request.UploadAsync();
            IContentWriter contentWriter = new GcsContentWriter(inputStream);

            // Force the bucket models to refresh with the potentially new object.
            BucketModels.Clear();
            TelemetryReporter.ReportSuccess(nameof(GoogleCloudStorageProvider), nameof(GetContentWriter));
            return(contentWriter);
        }
コード例 #4
0
        // To upload the file:
        public async Task UploadAsync(string bucketName, string filename)
        {
            Google.Apis.Storage.v1.Data.Object blob          = null;
            ObjectsResource.InsertMediaUpload  uploadRequest = null;

            blob        = new Google.Apis.Storage.v1.Data.Object();
            blob.Bucket = bucketName;
            blob.Name   = "keyName";

            using (var fileStream = new FileStream(filename, FileMode.Open))
            {
                uploadRequest            = new ObjectsResource.InsertMediaUpload(_storageService, blob, bucketName, fileStream, "text/plain");
                uploadRequest.OauthToken = _userCredential.Token.AccessToken;
                await uploadRequest.UploadAsync();
            }

            // This was in comments - to get status of upload
            //    var response = mediaInsertUpload.Upload();
            //    string status = response.Status.ToString();

            // Remember to refresh the tokens if you do lot of operations and before you go to the next batch.
        }