Ejemplo n.º 1
0
        public void RemoveAllMultiUploads(String bucketName)
        {
            BosClient client = this.GenerateBosClient();
            ListMultipartUploadsResponse list = client.ListMultipartUploads(bucketName);

            foreach (MultipartUploadSummary summary in list.Uploads)
            {
                String uploadID = summary.UploadId;
                String key      = summary.Key;
                String name     = summary.Owner.DisplayName;

                client.AbortMultipartUpload(bucketName, key, uploadID);
                String sss = "";
            }
        }
Ejemplo n.º 2
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);
        }