Exemple #1
0
        public static void AsynCompleteMultiUpload(COSXML.CosXml cosXml, string bucket, string key, string uploadId, Dictionary <int, string> partNumberAndEtags)
        {
            CompleteMultipartUploadRequest request = new CompleteMultipartUploadRequest(bucket, key, uploadId);

            //设置签名有效时长
            request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);

            //设置已上传的parts
            request.SetPartNumberAndETag(partNumberAndEtags);

            //执行请求
            cosXml.CompleteMultiUpload(request, delegate(CosResult result)
            {
                CompleteMultipartUploadResult getObjectResult = result as CompleteMultipartUploadResult;
                Console.WriteLine(getObjectResult.GetResultInfo());
                QLog.D("XIAO", result.GetResultInfo());
            }, delegate(CosClientException clientEx, CosServerException serverEx)
            {
                if (clientEx != null)
                {
                    QLog.D("XIAO", clientEx.Message);
                    Console.WriteLine("CosClientException: " + clientEx.StackTrace);
                }
                if (serverEx != null)
                {
                    QLog.D("XIAO", serverEx.Message);
                    Console.WriteLine("CosServerException: " + serverEx.GetInfo());
                }
            });
        }
Exemple #2
0
 public void SetResult(CompleteMultipartUploadResult result)
 {
     this.eTag            = result.completeResult.eTag;
     this.httpCode        = result.httpCode;
     this.httpMessage     = result.httpMessage;
     this.responseHeaders = result.responseHeaders;
 }
        public void MarshalResponse(IConfig config, CompleteMultipartUploadResponse response, IDictionary <string, string> headers, Stream responseStream)
        {
            response.VersionId            = headers.GetHeader(AmzHeaders.XAmzVersionId);
            response.SseAlgorithm         = headers.GetHeaderEnum <SseAlgorithm>(AmzHeaders.XAmzSse);
            response.SseKmsKeyId          = headers.GetHeader(AmzHeaders.XAmzSseAwsKmsKeyId);
            response.SseCustomerAlgorithm = headers.GetHeaderEnum <SseCustomerAlgorithm>(AmzHeaders.XAmzSseCustomerAlgorithm);
            response.RequestCharged       = headers.ContainsKey(AmzHeaders.XAmzRequestCharged);

            if (HeaderParserHelper.TryParseExpiration(headers, out (DateTimeOffset expiresOn, string ruleId)data))
            {
                response.LifeCycleExpiresOn = data.expiresOn;
                response.LifeCycleRuleId    = data.ruleId;
            }

            using (responseStream)
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(CompleteMultipartUploadResult));

                using (XmlTextReader xmlReader = new XmlTextReader(responseStream))
                {
                    xmlReader.Namespaces = false;

                    CompleteMultipartUploadResult resp = (CompleteMultipartUploadResult)xmlSerializer.Deserialize(xmlReader);
                    response.Location   = resp.Location;
                    response.BucketName = resp.Bucket;
                    response.ObjectKey  = resp.Key;
                    response.ETag       = resp.ETag;
                }
            }
        }
Exemple #4
0
        public static void CompleteMultiUpload(COSXML.CosXml cosXml, string bucket, string key, string uploadId, Dictionary <int, string> partNumberAndEtags)
        {
            try
            {
                CompleteMultipartUploadRequest request = new CompleteMultipartUploadRequest(bucket, key, uploadId);
                //设置签名有效时长
                request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);

                //设置已上传的parts
                request.SetPartNumberAndETag(partNumberAndEtags);

                //执行请求
                CompleteMultipartUploadResult result = cosXml.CompleteMultiUpload(request);

                Console.WriteLine(result.GetResultInfo());
            }
            catch (COSXML.CosException.CosClientException clientEx)
            {
                QLog.D("XIAO", clientEx.Message);
                Console.WriteLine("CosClientException: " + clientEx.StackTrace);
            }
            catch (COSXML.CosException.CosServerException serverEx)
            {
                QLog.D("XIAO", serverEx.Message);
                Console.WriteLine("CosServerException: " + serverEx.GetInfo());
            }
        }
Exemple #5
0
 /// 完成分片上传任务
 private void CompleteMultiUpload()
 {
     try
     {
         CompleteMultipartUploadRequest request = new CompleteMultipartUploadRequest(bucket, key, uploadId);
         //设置已上传的parts,必须有序,按照partNumber递增
         Dictionary <int, string> partNumberAndETags_SortedByKey = partNumberAndETags.OrderBy(p => p.Key).ToDictionary(p => p.Key, o => o.Value);
         request.SetPartNumberAndETag(partNumberAndETags_SortedByKey);
         //执行请求
         CompleteMultipartUploadResult result = cosXml.CompleteMultiUpload(request);
         //请求成功
         //Console.WriteLine(result.GetResultInfo());
     }
     catch (ArgumentNullException e)
     {
         InfoLog.InfoPrintf($"Cos: ArgumentNullException: {e}", InfoLog.InfoClass.系统错误信息);
     }
     catch (COSXML.CosException.CosClientException clientEx)
     {
         //请求失败
         InfoLog.InfoPrintf($"Cos: CosClientException: {clientEx}", InfoLog.InfoClass.系统错误信息);
     }
     catch (COSXML.CosException.CosServerException serverEx)
     {
         //请求失败
         InfoLog.InfoPrintf($"Cos: CosServerException: {serverEx.GetInfo()}", InfoLog.InfoClass.系统错误信息);
     }
 }
        /// 完成分片拷贝任务
        public void CompleteMultiUpload()
        {
            //.cssg-snippet-body-start:[complete-multi-upload]
            try
            {
                string bucket   = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID
                string key      = "exampleobject";            //对象键
                string uploadId = "exampleUploadId";          //初始化分块上传返回的uploadId
                CompleteMultipartUploadRequest request = new CompleteMultipartUploadRequest(bucket,
                                                                                            key, uploadId);
                //设置已上传的parts,必须有序,按照partNumber递增
                request.SetPartNumberAndETag(1, this.eTag);
                //执行请求
                CompleteMultipartUploadResult result = cosXml.CompleteMultiUpload(request);
                //请求成功
                Console.WriteLine(result.GetResultInfo());
            }
            catch (COSXML.CosException.CosClientException clientEx)
            {
                //请求失败
                Console.WriteLine("CosClientException: " + clientEx);
            }
            catch (COSXML.CosException.CosServerException serverEx)
            {
                //请求失败
                Console.WriteLine("CosServerException: " + serverEx.GetInfo());
            }

            //.cssg-snippet-body-end
        }
Exemple #7
0
        public void PartCopyObject(COSXML.CosXml cosXml, string bucket, string key,
                                   COSXML.Model.Tag.CopySourceStruct copySource)
        {
            try
            {
                InitMultipartUploadRequest initMultipartUploadRequest = new InitMultipartUploadRequest(bucket, key);
                //设置签名有效时长
                initMultipartUploadRequest.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);

                //执行请求
                InitMultipartUploadResult initMultipartUploadResult = cosXml.InitMultipartUpload(initMultipartUploadRequest);

                Console.WriteLine(initMultipartUploadResult.GetResultInfo());

                string uploadId = initMultipartUploadResult.initMultipartUpload.uploadId;

                int partNumber = 1;

                UploadPartCopyRequest uploadPartCopyRequest = new UploadPartCopyRequest(bucket, key, partNumber, uploadId);
                //设置签名有效时长
                uploadPartCopyRequest.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);

                //设置拷贝源
                uploadPartCopyRequest.SetCopySource(copySource);

                //设置拷贝范围
                uploadPartCopyRequest.SetCopyRange(0, 10);

                //执行请求
                UploadPartCopyResult uploadPartCopyResult = cosXml.PartCopy(uploadPartCopyRequest);

                Console.WriteLine(uploadPartCopyResult.GetResultInfo());

                string eTag = uploadPartCopyResult.copyObject.eTag;

                CompleteMultipartUploadRequest completeMultiUploadRequest = new CompleteMultipartUploadRequest(bucket, key, uploadId);
                //设置签名有效时长
                completeMultiUploadRequest.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);

                //设置已上传的parts
                completeMultiUploadRequest.SetPartNumberAndETag(partNumber, eTag);

                //执行请求
                CompleteMultipartUploadResult completeMultiUploadResult = cosXml.CompleteMultiUpload(completeMultiUploadRequest);

                Console.WriteLine(completeMultiUploadResult.GetResultInfo());
            }
            catch (COSXML.CosException.CosClientException clientEx)
            {
                Console.WriteLine("CosClientException: " + clientEx.Message);
                Assert.True(false);
            }
            catch (COSXML.CosException.CosServerException serverEx)
            {
                Console.WriteLine("CosServerException: " + serverEx.GetInfo());
                Assert.True(false);
            }
        }
        public void PartCopyObject()
        {
            string           key        = commonKey;
            CopySourceStruct copySource = new CopySourceStruct(QCloudServer.Instance().appid,
                                                               bucket, QCloudServer.Instance().region, copykey);

            try
            {
                InitMultipartUploadRequest initMultipartUploadRequest = new InitMultipartUploadRequest(bucket, key);

                //执行请求
                InitMultipartUploadResult initMultipartUploadResult = cosXml.InitMultipartUpload(initMultipartUploadRequest);

                Console.WriteLine(initMultipartUploadResult.GetResultInfo());

                string uploadId = initMultipartUploadResult.initMultipartUpload.uploadId;

                int partNumber = 1;

                UploadPartCopyRequest uploadPartCopyRequest = new UploadPartCopyRequest(bucket, key, partNumber, uploadId);

                //设置拷贝源
                uploadPartCopyRequest.SetCopySource(copySource);

                //设置拷贝范围
                uploadPartCopyRequest.SetCopyRange(0, 10);

                //执行请求
                UploadPartCopyResult uploadPartCopyResult = cosXml.PartCopy(uploadPartCopyRequest);

                Console.WriteLine(uploadPartCopyResult.GetResultInfo());

                string eTag = uploadPartCopyResult.copyObject.eTag;

                CompleteMultipartUploadRequest completeMultiUploadRequest = new CompleteMultipartUploadRequest(bucket, key, uploadId);

                //设置已上传的parts
                completeMultiUploadRequest.SetPartNumberAndETag(partNumber, eTag);

                //执行请求
                CompleteMultipartUploadResult completeMultiUploadResult = cosXml.CompleteMultiUpload(completeMultiUploadRequest);

                Console.WriteLine(completeMultiUploadResult.GetResultInfo());
            }
            catch (COSXML.CosException.CosClientException clientEx)
            {
                Console.WriteLine("CosClientException: " + clientEx.Message);
                Assert.True(false);
            }
            catch (COSXML.CosException.CosServerException serverEx)
            {
                Console.WriteLine("CosServerException: " + serverEx.GetInfo());
                Assert.True(false);
            }
        }
Exemple #9
0
        public async Task <CompleteMultipartUploadResult> CompleteMultipartUpload(CompleteMultipartUploadModel completeMultipartUploadModel)
        {
            CompleteMultipartUploadResult result             = null;
            OssHttpRequestMessage         httpRequestMessage = null;
            HttpResponseMessage           response           = null;

            try
            {
                Dictionary <string, string> parameters = new Dictionary <string, string>();
                parameters.Add("uploadId", completeMultipartUploadModel.UploadId);


                httpRequestMessage = new OssHttpRequestMessage(completeMultipartUploadModel.Bucket, completeMultipartUploadModel.Key, parameters);

                httpRequestMessage.Method       = HttpMethod.Post;
                httpRequestMessage.Headers.Date = DateTime.UtcNow;

                XmlStreamSerializer <CompleteMultipartUploadModel> serializer = new XmlStreamSerializer <CompleteMultipartUploadModel>();
                //  FileStream fileStream = new FileStream("1.xml", FileMode.Open);

                //httpRequestMessage.Content = new StreamContent(fileStream);
                httpRequestMessage.Content = new StreamContent(serializer.Serialize(completeMultipartUploadModel));

                OssRequestSigner.Sign(httpRequestMessage, networkCredential);
                response = await httpClient.SendAsync(httpRequestMessage);

                if (response.IsSuccessStatusCode == false)
                {
                    await ErrorResponseHandler.Handle(response);
                }
                var temp = DeserializerFactory.GetFactory().CreateCompMultiUploadDeserializer();
                result = await temp.Deserialize(response);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (httpRequestMessage != null)
                {
                    httpRequestMessage.Dispose();
                }

                if (response != null)
                {
                    response.Dispose();
                }
            }
            return(result);
        }
Exemple #10
0
 public void OnCompleted(CompleteMultipartUploadResult result)
 {
     UpdateProgress(sendContentLength, sendContentLength, true);
     //lock (syncExit)
     //{
     //    isExit = true;
     //}
     if (successCallback != null)
     {
         UploadTaskResult uploadTaskResult = new UploadTaskResult();
         uploadTaskResult.SetResult(result);
         successCallback(uploadTaskResult);
     }
 }
 public void OnCompleted(CompleteMultipartUploadResult result)
 {
     uploadId = null;
     //lock (syncExit)
     //{
     //    isExit = true;
     //}
     //success
     if (successCallback != null)
     {
         CopyTaskResult copyTaskResult = new CopyTaskResult();
         copyTaskResult.SetResult(result);
         successCallback(copyTaskResult);
     }
 }
Exemple #12
0
        /**
         * 分块上传例子
         * **/
        private static bool uploadPart()
        {
            string path = @"you file path";//上传文件路径,例如E:\tool\aa.rar
            InitiateMultipartUploadResult result = multipartUp();
            FileInfo file           = new FileInfo(path);
            int      part           = 5 * 1024 * 1024;
            int      numBytesToRead = (int)file.Length;
            int      i    = 0;
            XElement root = new XElement("CompleteMultipartUpload");//初始化一个xml,以备分块上传完成后调用complete方法提交本次上传的文件以通知服务端合并分块

            //开始读取文件
            using (FileStream fs = new FileStream(path, FileMode.Open))
            {
                while (numBytesToRead > 0)
                {
                    UploadPartRequest request = new UploadPartRequest(
                        result.getBucket(), result.getKey(), result.getUploadId(),
                        i + 1);
                    //每次读取5M文件内容,如果最后一次内容不及5M则按实际大小取值
                    int    count = Convert.ToInt32((i * part + part) > file.Length ? file.Length - i * part : part);
                    byte[] data  = new byte[count];
                    int    n     = fs.Read(data, 0, count);
                    request.setInputStream(new MemoryStream(data));
                    ProgressListener sampleListener = new SampleListener(count); //实例一个更新进度的监听类,实际使用中可自己定义实现
                    request.setProgressListener(sampleListener);
                    PartETag tag = ks3Client.uploadPart(request);                //上传本次分块内容
                    Console.WriteLine(tag.ToString());
                    if (n == 0)
                    {
                        break;
                    }
                    numBytesToRead -= n;

                    XElement partE = new XElement("Part");
                    partE.Add(new XElement("PartNumber", i + 1));
                    partE.Add(new XElement("ETag", tag.geteTag()));
                    root.Add(partE);
                    i++;
                }
            }
            //所有分块上传完成后发起complete request,通知服务端合并分块
            CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest(result.getBucket(), result.getKey(), result.getUploadId());

            completeRequest.setContent(new MemoryStream(System.Text.Encoding.Default.GetBytes(root.ToString())));
            CompleteMultipartUploadResult completeResult = ks3Client.completeMultipartUpload(completeRequest);

            return(true);
        }
Exemple #13
0
        public static CompleteMultipartUploadResult UploadMultipart
        (
            IOssClientBuilder clientBuilder,
            string bucketName,
            string objectName,
            string fileToUpload,
            int partSize = DEFAULT_PART_SIZE,
            EventHandler <StreamTransferProgressArgs> streamTransferProgress = null,
            CancellationToken cancellationToken = default(CancellationToken)
        )
        {
            CompleteMultipartUploadResult retVal = null;

            if (clientBuilder == null)
            {
                throw new ArgumentNullException(nameof(clientBuilder));
            }

            long fileSize = new FileInfo(fileToUpload).Length;
            long progressUpdateInterval = (fileSize / 100 + 4096) / 4096 * 4096;

            var client = clientBuilder.Build(o =>
            {
                o.ProgressUpdateInterval = progressUpdateInterval;
            });

            var uploadId = InitiateMultipartUpload(client, bucketName, objectName);

            using (var cleaner = new MultipartCleaner(client, bucketName, objectName, uploadId))
            {
                var partETags = UploadParts(client, bucketName, objectName, fileToUpload, uploadId, partSize,
                                            (s, e) =>
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    streamTransferProgress?.Invoke(s, e);
                });
                retVal = CompleteUploadPart(client, bucketName, objectName, uploadId, partETags);
                cleaner.Complete();
            }
#if DEBUG
            if (OutputDebugInfo)
            {
                Console.WriteLine("Multipart put object:{0} succeeded", objectName);
            }
#endif
            return(retVal);
        }
        public void CompleteXmlTests()
        {
            string xmlText =

                @"<?xml version=""1.0"" encoding=""UTF-8""?>
<CompleteMultipartUploadResult xmlns=""http://s3.amazonaws.com/doc/2006-03-01/"">
  <Location>http://Example-Bucket.s3.amazonaws.com/Example-Object</Location>
  <Bucket>Example-Bucket</Bucket>
  <Key>Example-Object</Key>
  <ETag>""3858f62230ac3c915f300c664312c11f-9""</ETag>
</CompleteMultipartUploadResult>";

            var result = CompleteMultipartUploadResult.ParseXml(xmlText);

            Assert.Equal("http://Example-Bucket.s3.amazonaws.com/Example-Object", result.Location);
            Assert.Equal("Example-Bucket", result.Bucket);
            Assert.Equal("Example-Object", result.Key);
            Assert.Equal(@"""3858f62230ac3c915f300c664312c11f-9""", result.ETag);
        }
Exemple #15
0
        private void CompleteMultipartUpload()
        {
            completeMultiUploadRequest = new CompleteMultipartUploadRequest(bucket, key, uploadId);

            foreach (SliceStruct sliceStruct in sliceList)
            {
                // partNumberEtag 有序的
                // partNumberEtag 有序的
                completeMultiUploadRequest.SetPartNumberAndETag(sliceStruct.partNumber, sliceStruct.eTag);
            }

            cosXmlServer.CompleteMultiUpload(completeMultiUploadRequest, delegate(CosResult result)
            {
                lock (syncExit)
                {
                    if (isExit)
                    {
                        return;
                    }
                }

                if (UpdateTaskState(TaskState.Completed))
                {
                    CompleteMultipartUploadResult completeMultiUploadResult = result as CompleteMultipartUploadResult;

                    OnCompleted(completeMultiUploadResult);
                }
            }, delegate(CosClientException clientEx, CosServerException serverEx)
            {
                lock (syncExit)
                {
                    if (isExit)
                    {
                        return;
                    }
                }

                if (UpdateTaskState(TaskState.Failed))
                {
                    OnFailed(clientEx, serverEx);
                }
            });
        }
        private CompleteMultipartUploadResult CompleteUploadPart(string objectKey, string uploadId, List <PartETag> partETags)
        {
            CompleteMultipartUploadResult uploadResult = null;

            try
            {
                var completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, objectKey, uploadId);
                foreach (var partETag in partETags)
                {
                    completeMultipartUploadRequest.PartETags.Add(partETag);
                }
                uploadResult = client.CompleteMultipartUpload(completeMultipartUploadRequest);
            }
            catch (OssException ex)
            {
                lastError = ex;
            }

            return(uploadResult);
        }
Exemple #17
0
        private void CompleteMultipartUpload()
        {
            completeMultiUploadRequest = new CompleteMultipartUploadRequest(bucket, key, uploadId);
            foreach (SliceStruct sliceStruct in sliceList)
            {
                completeMultiUploadRequest.SetPartNumberAndETag(sliceStruct.partNumber, sliceStruct.eTag); // partNumberEtag 有序的
            }
            completeMultiUploadRequest.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
            cosXmlServer.CompleteMultiUpload(completeMultiUploadRequest, delegate(CosResult result)
            {
                lock (syncExit)
                {
                    if (isExit)
                    {
                        return;
                    }
                }
                if (UpdateTaskState(TaskState.COMPLETED))
                {
                    CompleteMultipartUploadResult completeMultiUploadResult = result as CompleteMultipartUploadResult;
                    OnCompleted(completeMultiUploadResult);
                }
            }, delegate(CosClientException clientEx, CosServerException serverEx)
            {
                lock (syncExit)
                {
                    if (isExit)
                    {
                        return;
                    }
                }

                if (UpdateTaskState(TaskState.FAILED))
                {
                    OnFailed(clientEx, serverEx);
                }
            });
        }
        public void MarshalResponse(CompleteMultipartUploadRequest request, CompleteMultipartUploadResponse response, IDictionary <string, string> headers, Stream responseStream)
        {
            response.Expiration           = headers.GetHeaderDate(AmzHeaders.XAmzExpiration, DateTimeFormat.Iso8601DateTimeExt);
            response.SseAlgorithm         = headers.GetHeaderEnum <SseAlgorithm>(AmzHeaders.XAmzSSE);
            response.SseKmsKeyId          = headers.GetHeader(AmzHeaders.XAmzSSEAwsKmsKeyId);
            response.SseCustomerAlgorithm = headers.GetHeaderEnum <SseCustomerAlgorithm>(AmzHeaders.XAmzSSECustomerAlgorithm);
            response.SseCustomerKeyMd5    = headers.GetHeaderByteArray(AmzHeaders.XAmzSSECustomerKeyMD5, BinaryEncoding.Base64);

            using (responseStream)
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(CompleteMultipartUploadResult));

                using (XmlTextReader xmlReader = new XmlTextReader(responseStream))
                {
                    xmlReader.Namespaces = false;

                    CompleteMultipartUploadResult resp = (CompleteMultipartUploadResult)xmlSerializer.Deserialize(xmlReader);
                    response.Location = resp.Location;
                    response.Bucket   = resp.Bucket;
                    response.Key      = resp.Key;
                    response.ETag     = resp.ETag;
                }
            }
        }
        internal override void Execute()
        {
            FileInfo   fileInfo   = new FileInfo(filePath);
            FileStream fileStream = File.OpenRead(filePath);
            string     uploadId   = null;

            try
            {
                this.currentUploadProgressArgs = new StreamTransferProgressArgs(0, 0, fileInfo.Length);

                long partSize = CalculatePartSize(fileInfo.Length);
                InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest
                {
                    AccountId          = this.options.AccountId,
                    ArchiveDescription = archiveDescription,
                    VaultName          = vaultName,
                    PartSize           = partSize
                };
                initiateRequest.BeforeRequestEvent += new UserAgentPostFix("MultiUpload").UserAgentRequestEventHandlerSync;
                InitiateMultipartUploadResult initiateResult = this.manager.GlacierClient.InitiateMultipartUpload(initiateRequest).InitiateMultipartUploadResult;


                uploadId = initiateResult.UploadId;

                List <string> partTreeHashs   = new List <string>();
                long          currentPosition = 0;
                while (currentPosition < fileInfo.Length)
                {
                    long length = partSize;
                    if (currentPosition + partSize > fileInfo.Length)
                    {
                        length = fileInfo.Length - currentPosition;
                    }

                    PartStreamWrapper partStream = new PartStreamWrapper(fileStream, length);

                    string checksum = TreeHashGenerator.CalculateTreeHash(partStream);
                    partTreeHashs.Add(checksum);

                    UploadMultipartPartRequest uploadRequest = new UploadMultipartPartRequest
                    {
                        AccountId = this.options.AccountId,
                        Checksum  = checksum,
                        Body      = partStream,
                        Range     = ("bytes " + currentPosition + "-" + (currentPosition + length - 1) + "/*"),
                        UploadId  = uploadId,
                        VaultName = vaultName
                    };
                    uploadRequest.StreamTransferProgress += this.ProgressCallback;
                    uploadRequest.BeforeRequestEvent     += new UserAgentPostFix("MultiUpload").UserAgentRequestEventHandlerSync;

                    this.manager.GlacierClient.UploadMultipartPart(uploadRequest);
                    currentPosition += partSize;
                }

                string totalFileChecksum = TreeHashGenerator.CalculateTreeHash(partTreeHashs);
                string archiveSize       = fileInfo.Length.ToString();
                CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest
                {
                    AccountId   = this.options.AccountId,
                    ArchiveSize = archiveSize,
                    VaultName   = vaultName,
                    Checksum    = totalFileChecksum,
                    UploadId    = uploadId
                };
                compRequest.BeforeRequestEvent += new UserAgentPostFix("MultiUpload").UserAgentRequestEventHandlerSync;
                CompleteMultipartUploadResult completeMultipartUploadResult = this.manager.GlacierClient.CompleteMultipartUpload(compRequest).CompleteMultipartUploadResult;

                string archiveId = completeMultipartUploadResult.ArchiveId;
                this.UploadResult = new UploadResult(archiveId, totalFileChecksum);
            }
            catch (Exception)
            {
                // If we got an unrecoverable then abort the upload.
                if (!string.IsNullOrEmpty(uploadId))
                {
                    AbortMultipartUploadRequest abortRequest = new AbortMultipartUploadRequest()
                    {
                        AccountId = this.options.AccountId,
                        VaultName = this.vaultName,
                        UploadId  = uploadId
                    };
                    abortRequest.BeforeRequestEvent += new UserAgentPostFix("MultiUpload").UserAgentRequestEventHandlerSync;
                    this.manager.GlacierClient.AbortMultipartUpload(abortRequest);
                }

                throw;
            }
            finally
            {
                try { fileStream.Close(); }
                catch (Exception) { }
            }
        }
Exemple #20
0
        public void MutiPartUpload(string fileName, string key)
        {
            Console.WriteLine("开始上传:" + key);
            InitiateMultipartUploadRequest initRequest =
                new InitiateMultipartUploadRequest(ossConfig.BucketName, key);
            InitiateMultipartUploadResult initResult = ossClient.InitiateMultipartUpload(initRequest);


            // 设置每块为 5M
            int partSize = 1024 * 1024 * 5;

            FileInfo partFile = new FileInfo(fileName);

            // 计算分块数目
            int partCount = (int)(partFile.Length / partSize);

            if (partFile.Length % partSize != 0)
            {
                partCount++;
            }
            Console.WriteLine("数据分块上传,一共:{0}块", partCount);
            // 新建一个List保存每个分块上传后的ETag和PartNumber
            List <PartETag> partETags = new List <PartETag>();

            for (int i = 0; i < partCount; i++)
            {
                // 获取文件流
                FileStream fis = new FileStream(partFile.FullName, FileMode.Open);

                // 跳到每个分块的开头
                long skipBytes = partSize * i;
                fis.Position = skipBytes;
                //fis.skip(skipBytes);

                // 计算每个分块的大小
                long size = partSize < partFile.Length - skipBytes ?
                            partSize : partFile.Length - skipBytes;

                // 创建UploadPartRequest,上传分块
                UploadPartRequest uploadPartRequest = new UploadPartRequest(ossConfig.BucketName, key, initResult.UploadId);
                uploadPartRequest.InputStream = fis;
                uploadPartRequest.PartSize    = size;
                uploadPartRequest.PartNumber  = (i + 1);
                UploadPartResult uploadPartResult = ossClient.UploadPart(uploadPartRequest);

                // 将返回的PartETag保存到List中。
                partETags.Add(uploadPartResult.PartETag);

                // 关闭文件
                fis.Close();
                Console.WriteLine("第{0}块,上传完毕", i + 1);
            }

            CompleteMultipartUploadRequest completeReq = new CompleteMultipartUploadRequest(ossConfig.BucketName, key, initResult.UploadId);

            foreach (PartETag partETag in partETags)
            {
                completeReq.PartETags.Add(partETag);
            }
            //  红色标注的是与JAVA的SDK有区别的地方

            //完成分块上传
            Console.WriteLine("合并数据块开始");
            CompleteMultipartUploadResult completeResult = ossClient.CompleteMultipartUpload(completeReq);

            Console.WriteLine("合并数据块结束");
            // 返回最终文件的MD5,用于用户进行校验

            Console.WriteLine(key + " 上传成功.");
        }
Exemple #21
0
        public static CompleteMultipartUploadResult UploadMultipart
        (
            IOssClientBuilder clientBuilder,
            string bucketName,
            string objectName,
            Stream streamToUpload,
            Action <MultipartUploadOptions> options = null,
            EventHandler <StreamTransferProgressArgs> streamTransferProgress = null,
            CancellationToken cancellationToken = default(CancellationToken)
        )
        {
            CompleteMultipartUploadResult retVal = null;

            if (clientBuilder == null)
            {
                throw new ArgumentNullException(nameof(clientBuilder));
            }
            if (streamToUpload == null)
            {
                throw new ArgumentNullException(nameof(streamToUpload));
            }

            MultipartUploadOptions uploadOptions = new MultipartUploadOptions();

            options?.Invoke(uploadOptions);
            int partSize   = Math.Max(DEFAULT_PART_SIZE, uploadOptions.PartSize);
            var streamMode = uploadOptions.StreamMode;

            long fileSize = streamToUpload.Length;
            long progressUpdateInterval = (fileSize / 100 + 4096) / 4096 * 4096;

            if (streamMode == MultipartStreamMode.Auto || streamMode == MultipartStreamMode.PartialFileStream)
            {
                if (streamToUpload is FileStream && streamToUpload.CanSeek && streamToUpload.CanRead)
                {
                    streamMode = MultipartStreamMode.PartialFileStream;
                }
                else if (streamToUpload.CanRead)
                {
                    streamMode = MultipartStreamMode.SequenceInputStream;
                }
            }
            if (streamMode == MultipartStreamMode.PartialFileStream)
            {
                var client = clientBuilder.Build(o =>
                {
                    o.ProgressUpdateInterval = progressUpdateInterval;
                });

                var uploadId = InitiateMultipartUpload(client, bucketName, objectName);
                using (var cleaner = new MultipartCleaner(client, bucketName, objectName, uploadId))
                {
                    var partETags = UploadParts(client, bucketName, objectName, streamToUpload, uploadId, partSize,
                                                (s, e) =>
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        streamTransferProgress?.Invoke(s, e);
                    });
                    retVal = CompleteUploadPart(client, bucketName, objectName, uploadId, partETags);
                    cleaner.Complete();
                }
            }
            else if (streamMode == MultipartStreamMode.SequenceInputStream)
            {
                var client = clientBuilder.Build(o =>
                {
                    o.ProgressUpdateInterval = progressUpdateInterval;
                });

                var uploadId = InitiateMultipartUpload(client, bucketName, objectName);
                using (var cleaner = new MultipartCleaner(client, bucketName, objectName, uploadId))
                {
                    var partETags = UploadPartsWithCache(client, bucketName, objectName, streamToUpload, uploadId, partSize,
                                                         (s, e) =>
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        streamTransferProgress?.Invoke(s, e);
                    });
                    retVal = CompleteUploadPart(client, bucketName, objectName, uploadId, partETags);
                    cleaner.Complete();
                }
            }
            else
            {
                throw new ArgumentException("Stream Type is not supported", nameof(streamToUpload));
            }
#if DEBUG
            if (OutputDebugInfo)
            {
                Console.WriteLine("Multipart put object:{0} succeeded", objectName);
            }
#endif
            return(retVal);
        }
        public void MultiUpload()
        {
            string key = multiKey;

            try
            {
                InitMultipartUploadRequest initMultipartUploadRequest = new InitMultipartUploadRequest(bucket, multiKey);

                //执行请求
                InitMultipartUploadResult initMultipartUploadResult = cosXml.InitMultipartUpload(initMultipartUploadRequest);

                Console.WriteLine(initMultipartUploadResult.GetResultInfo());

                string uploadId = initMultipartUploadResult.initMultipartUpload.uploadId;

                ListPartsRequest listPartsRequest = new ListPartsRequest(bucket, key, uploadId);

                //执行请求
                ListPartsResult listPartsResult = cosXml.ListParts(listPartsRequest);

                Console.WriteLine(listPartsResult.GetResultInfo());

                int partNumber = 1;

                UploadPartRequest uploadPartRequest = new UploadPartRequest(bucket, key, partNumber, uploadId, bigFileSrcPath);

                //设置进度回调
                uploadPartRequest.SetCosProgressCallback(delegate(long completed, long total)
                {
                    Console.WriteLine(String.Format("{0} progress = {1} / {2} : {3:##.##}%",
                                                    DateTime.Now.ToString(), completed, total, completed * 100.0 / total));
                });

                //执行请求
                UploadPartResult uploadPartResult = cosXml.UploadPart(uploadPartRequest);

                Console.WriteLine(uploadPartResult.GetResultInfo());

                string eTag = uploadPartResult.eTag;

                CompleteMultipartUploadRequest completeMultiUploadRequest = new CompleteMultipartUploadRequest(bucket, key, uploadId);

                //设置已上传的parts
                completeMultiUploadRequest.SetPartNumberAndETag(partNumber, eTag);

                //执行请求
                CompleteMultipartUploadResult completeMultiUploadResult = cosXml.CompleteMultiUpload(completeMultiUploadRequest);

                Console.WriteLine(completeMultiUploadResult.GetResultInfo());
            }
            catch (COSXML.CosException.CosClientException clientEx)
            {
                Console.WriteLine("CosClientException: " + clientEx.StackTrace);
                Assert.True(false);
            }
            catch (COSXML.CosException.CosServerException serverEx)
            {
                Console.WriteLine("CosServerException: " + serverEx.GetInfo());
                Assert.True(false);
            }
        }
Exemple #23
0
        public void mutiPartUpload()
        {
            for (; ;)
            {
                getListItemName();

                if (string.IsNullOrEmpty(path))
                {
                    break;
                }

                key = Guid.NewGuid().ToString() + path.Substring(path.LastIndexOf('.'));

                OssClient ossClient = new OssClient(endPoint, accessKeyID, accessKeySecret);

                InitiateMultipartUploadRequest initRequest =
                    new InitiateMultipartUploadRequest(bucketName, key);
                InitiateMultipartUploadResult initResult = ossClient.InitiateMultipartUpload(initRequest);


                // 设置每块为 5M ,不允许小于5M
                int partSize = 1024 * 100;

                FileInfo partFile = new FileInfo(path);

                // 计算分块数目
                int partCount = (int)(partFile.Length / partSize);
                if (partFile.Length % partSize != 0)
                {
                    partCount++;
                }

                // 新建一个List保存每个分块上传后的ETag和PartNumber
                List <PartETag> partETags = new List <PartETag>();

                for (int i = 0; i < partCount; i++)
                {
                    Bar.Value = (i * 100) / partCount;
                    UploadInfo(Bar.Value.ToString());

                    // 获取文件流
                    FileStream fis = new FileStream(partFile.FullName, FileMode.Open);

                    // 跳到每个分块的开头
                    long skipBytes = partSize * i;
                    fis.Position = skipBytes;
                    //fis.skip(skipBytes);

                    // 计算每个分块的大小
                    long size = partSize < partFile.Length - skipBytes ?
                                partSize : partFile.Length - skipBytes;

                    // 创建UploadPartRequest,上传分块
                    UploadPartRequest uploadPartRequest = new UploadPartRequest(bucketName, key, initResult.UploadId);
                    uploadPartRequest.InputStream = fis;
                    uploadPartRequest.PartSize    = size;
                    uploadPartRequest.PartNumber  = (i + 1);
                    UploadPartResult uploadPartResult = ossClient.UploadPart(uploadPartRequest);

                    // 将返回的PartETag保存到List中。
                    partETags.Add(uploadPartResult.PartETag);

                    // 关闭文件
                    fis.Close();

                    manualReset.WaitOne();//如果ManualResetEvent的初始化为终止状态(true),那么该方法将一直工作,直到收到Reset信号。然后,直到收到Set信号,就继续工作。
                }

                CompleteMultipartUploadRequest completeReq = new CompleteMultipartUploadRequest(bucketName, key, initResult.UploadId);
                foreach (PartETag partETag in partETags)
                {
                    completeReq.PartETags.Add(partETag);
                }
                //  红色标注的是与JAVA的SDK有区别的地方

                //完成分块上传
                CompleteMultipartUploadResult completeResult = ossClient.CompleteMultipartUpload(completeReq);

                Bar.Value = 100;
                UploadInfo(Bar.Value.ToString());

                // 返回最终文件的MD5,用于用户进行校验
                //Console.WriteLine(completeResult.ETag);

                setListItemValue();
            }
        }