Exemplo n.º 1
0
        public static bool UploadFile(this BosClient bosClient, BOSMultipartUploadRequest bosUploadRequest, IProgress <int> onProgressPercentChanged)
        {
            Debug.Assert(bosUploadRequest.RequestInfo.PartSize > 0);
            var filePath = bosUploadRequest.RequestInfo.FilePath;

            if (!File.Exists(filePath))
            {
                return(false);
            }

            var fileInfo = new FileInfo(filePath);

            long partSize = bosUploadRequest.RequestInfo.PartSize;

            int parts = (int)(fileInfo.Length / partSize);

            if (fileInfo.Length % partSize != 0)
            {
                parts += 1;
            }

            if (bosUploadRequest.RequestInfo.PartETags == null)
            {
                bosUploadRequest.RequestInfo.PartETags = new List <PartETag>();
            }

            if (string.IsNullOrEmpty(bosUploadRequest.RequestInfo.UploadId))
            {
                var initiateMultipartUploadRequest = new InitiateMultipartUploadRequest()
                {
                    BucketName = bosUploadRequest.RequestInfo.Bucket,
                    Key        = bosUploadRequest.RequestInfo.ObjectKey
                };
                var initiateMultipartUploadResponse = bosClient.InitiateMultipartUpload(initiateMultipartUploadRequest);

                bosUploadRequest.RequestInfo.UploadId = initiateMultipartUploadResponse.UploadId;
            }

            var uploadId = bosUploadRequest.RequestInfo.UploadId;

            for (int i = bosUploadRequest.RequestInfo.LastPartNumber; i < parts; ++i)
            {
                if (bosUploadRequest.PauseCancellationToken.IsCancellationRequested)
                {
                    return(false);
                }

                if (bosUploadRequest.AbortCancellationToken.IsCancellationRequested)
                {
                    var abortMultipartUploadRequest = new AbortMultipartUploadRequest()
                    {
                        BucketName = bosUploadRequest.RequestInfo.Bucket,
                        Key        = bosUploadRequest.RequestInfo.ObjectKey,
                        UploadId   = uploadId,
                    };
                    bosClient.AbortMultipartUpload(abortMultipartUploadRequest);

                    return(false);
                }

                using (var stream = fileInfo.OpenRead())
                {
                    var skipBytes = partSize * i;
                    stream.Seek(skipBytes, SeekOrigin.Begin);

                    onProgressPercentChanged?.Report((int)(((double)skipBytes / fileInfo.Length) * 100));

                    var actualPartSize = Math.Min(partSize, fileInfo.Length - skipBytes);

                    var uploadPartRequest = new UploadPartRequest();
                    uploadPartRequest.BucketName  = bosUploadRequest.RequestInfo.Bucket;
                    uploadPartRequest.Key         = bosUploadRequest.RequestInfo.ObjectKey;
                    uploadPartRequest.UploadId    = uploadId;
                    uploadPartRequest.InputStream = stream;
                    uploadPartRequest.PartSize    = actualPartSize;
                    uploadPartRequest.PartNumber  = i + 1;

                    var uploadPartResponse = bosClient.UploadPart(uploadPartRequest);

                    bosUploadRequest.RequestInfo.PartETags.Add(
                        new PartETag()
                    {
                        ETag       = uploadPartResponse.ETag,
                        PartNumber = uploadPartResponse.PartNumber
                    });

                    bosUploadRequest.RequestInfo.LastPartNumber = uploadPartResponse.PartNumber;
                }
            }

            var completeMultipartUploadRequest =
                new CompleteMultipartUploadRequest()
            {
                BucketName = bosUploadRequest.RequestInfo.Bucket,
                Key        = bosUploadRequest.RequestInfo.ObjectKey,
                UploadId   = uploadId,
                PartETags  = bosUploadRequest.RequestInfo.PartETags
            };

            var completeMultipartUploadResponse = bosClient.CompleteMultipartUpload(completeMultipartUploadRequest);

            onProgressPercentChanged?.Report(100);

            return(completeMultipartUploadResponse != null);
        }
Exemplo n.º 2
0
        private async void uploadToolStripButton__Click(object sender, EventArgs e)
        {
            string filePath;

            using (var openFileDialog = new OpenFileDialog())
            {
                var filters = new string[]
                {
                    "All Files (*.*)|*.*",
                };

                openFileDialog.Filter = string.Join("|", filters);
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    filePath = openFileDialog.FileName;
                }
                else
                {
                    return;
                }
            }

            toolStripProgressBar_.Value      = 0;
            uploadToolStripButton_.Enabled   = false;
            downloadToolStripButton_.Enabled = false;
            settingsToolStripButton_.Enabled = false;

            toolStripProgressBar_.Visible = true;
            pauseToolStripButton_.Visible = true;
            abortToolStripButton_.Visible = true;

            var objectKey = settings_.UseFileFullPathAsObjectKey ? filePath : Path.GetFileName(filePath);

            pauseCancellationTokenSource_ = new CancellationTokenSource();
            abortCancellationTokenSource_ = new CancellationTokenSource();

            BOSMultipartUploadRequestInfo bosMultipartUploadRequestInfo;

            var multipartUploadInfoFile = filePath + BOSMultipartUploadRequestInfo.UploadRequestInfoFileExtension;

            if (File.Exists(multipartUploadInfoFile))
            {
                bosMultipartUploadRequestInfo = JsonHelper.Load <BOSMultipartUploadRequestInfo>(multipartUploadInfoFile);
            }
            else
            {
                bosMultipartUploadRequestInfo = new BOSMultipartUploadRequestInfo
                {
                    FilePath  = filePath,
                    Bucket    = CurrentBucket_,
                    ObjectKey = objectKey,
                    PartSize  = settings_.UploadPartSize > 0 ? settings_.UploadPartSize : 1024 * 1024 * 1,
                };
            }

            var bosMultipartUploadRequest = new BOSMultipartUploadRequest
            {
                RequestInfo            = bosMultipartUploadRequestInfo,
                PauseCancellationToken = pauseCancellationTokenSource_.Token,
                AbortCancellationToken = abortCancellationTokenSource_.Token,
            };

            bool ok = false;

            try
            {
                ok = await bosClient_.UploadFileAsync(bosMultipartUploadRequest,
                                                      new ProgressBarReporter(toolStripProgressBar_.ProgressBar, SynchronizationContext.Current));
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    this,
                    "Failed to upload object:\n" + ex.Message,
                    "Error:",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }

            if (ok)
            {
                UpdateObjectList();

                if (File.Exists(multipartUploadInfoFile))
                {
                    File.Delete(multipartUploadInfoFile);
                }
            }
            else if (pauseCancellationTokenSource_.IsCancellationRequested)
            {
                JsonHelper.Store(multipartUploadInfoFile, bosMultipartUploadRequestInfo);
            }

            uploadToolStripButton_.Enabled   = true;
            downloadToolStripButton_.Enabled = true;
            settingsToolStripButton_.Enabled = true;

            toolStripProgressBar_.Visible = false;
            pauseToolStripButton_.Visible = false;
            abortToolStripButton_.Visible = false;

            pauseCancellationTokenSource_ = null;
            abortCancellationTokenSource_ = null;
        }
Exemplo n.º 3
0
 public static Task <bool> UploadFileAsync(this BosClient bosClient, BOSMultipartUploadRequest bosUploadRequest, IProgress <int> onProgressPercentChanged)
 {
     return(Task.Run(() => bosClient.UploadFile(bosUploadRequest, onProgressPercentChanged)));
 }