コード例 #1
0
        /// <summary>
        /// Starts a delete operation, sending the notifications to the given <paramref name="operation"/>.
        /// </summary>
        /// <param name="bucket">The bucket that owns the file.</param>
        /// <param name="name">The name of the file.</param>
        /// <param name="operation">The operation that will receive the status and progress notifications.</param>
        /// <param name="token">The cancellation token to cancel the operation.</param>
        public async void StartDeleteOperation(string bucket, string name, IGcsFileOperationCallback operation, CancellationToken token)
        {
            try
            {
                var response = await Service.Objects.Delete(bucket, name).ExecuteAsync(token);

                operation.Completed();
            }
            catch (GoogleApiException ex)
            {
                operation.Error(new DataSourceException(ex.Message, ex));
            }
            catch (OperationCanceledException)
            {
                operation.Cancelled();
            }
        }
コード例 #2
0
        /// <summary>
        /// Starts a file upload operation reporting the status and progress to the given <paramref name="operation"/>.
        /// </summary>
        /// <param name="sourcePath">The path to the file to open, should be a full path.</param>
        /// <param name="bucket">The bucket that will own the file.</param>
        /// <param name="name">The name to use.</param>
        /// <param name="operation">The operation that will receive the status and progress notifications.</param>
        /// <param name="token">The cancellation token to cancel the operation.</param>
        public async void StartFileUploadOperation(
            string sourcePath,
            string bucket,
            string name,
            IGcsFileOperationCallback operation,
            CancellationToken token)
        {
            try
            {
                using (var stream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read))
                {
                    var totalSize = (ulong)stream.Length;
                    var request   = Service.Objects.Insert(
                        new Google.Apis.Storage.v1.Data.Object
                    {
                        Name = name,
                        Size = totalSize,
                    },
                        bucket,
                        stream,
                        null);
                    request.ProgressChanged += (p) => OnUploadProgress(p, totalSize, operation);
                    var response = await request.UploadAsync(token);

                    operation.Completed();
                }
            }
            catch (IOException ex)
            {
                operation.Error(new DataSourceException(ex.Message, ex));
            }
            catch (GoogleApiException ex)
            {
                operation.Error(new DataSourceException(ex.Message, ex));
            }
            catch (OperationCanceledException)
            {
                operation.Cancelled();
            }
        }
コード例 #3
0
        /// <summary>
        /// Starts a file download operation, reporting the status and progress to the given <paramref name="operation"/>.
        /// </summary>
        /// <param name="bucket">The bucket that owns the file.</param>
        /// <param name="name">The file name.</param>
        /// <param name="destPath">Where to save the file, this should be a full path.</param>
        /// <param name="operation">The operation that will receive the status and progress notifications.</param>
        /// <param name="token">The cancellation token to cancel the operation.</param>
        public async void StartFileDownloadOperation(
            string bucket,
            string name,
            string destPath,
            IGcsFileOperationCallback operation,
            CancellationToken token)
        {
            try
            {
                using (var stream = new FileStream(destPath, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    // Find out the total size of the file to download.
                    var request = Service.Objects.Get(bucket, name);
                    var obj     = await request.ExecuteAsync(token);

                    ulong totalSize = obj.Size ?? 0;

                    // Hookup the progress indicator.
                    request.MediaDownloader.ProgressChanged += (p) => OnDownloadProgress(p, totalSize, operation);
                    var response = await request.DownloadAsync(stream, token);

                    operation.Completed();
                }
            }
            catch (IOException ex)
            {
                operation.Error(new DataSourceException(ex.Message, ex));
            }
            catch (GoogleApiException ex)
            {
                operation.Error(new DataSourceException(ex.Message, ex));
            }
            catch (OperationCanceledException)
            {
                operation.Cancelled();
            }
        }
コード例 #4
0
        /// <summary>
        /// Starts a move operation, sending the notifications to the given <paramref name="operation"/>.
        /// </summary>
        /// <param name="bucket">The bucket where the files reside.</param>
        /// <param name="fromName">The file to move.</param>
        /// <param name="toName">The new file name.</param>
        /// <param name="operation">The operation to callback.</param>
        /// <param name="cancellationToken">The cancellation token for the operation.</param>
        public async void StartMoveOperation(
            string bucket,
            string fromName,
            string toName,
            IGcsFileOperationCallback operation,
            CancellationToken cancellationToken)
        {
            try
            {
                await Service.Objects.Copy(null, bucket, fromName, bucket, toName).ExecuteAsync(cancellationToken);

                await Service.Objects.Delete(bucket, fromName).ExecuteAsync(cancellationToken);

                operation.Completed();
            }
            catch (GoogleApiException ex)
            {
                operation.Error(new DataSourceException(ex.Message, ex));
            }
            catch (OperationCanceledException)
            {
                operation.Cancelled();
            }
        }