Example #1
0
        public static void AsynPartCopyObject(COSXML.CosXml cosXml, string bucket, string key,
                                              COSXML.Model.Tag.CopySourceStruct copySource, string uploadId, int partNumber)
        {
            UploadPartCopyRequest request = new UploadPartCopyRequest(bucket, key, partNumber, uploadId);

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

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

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

            //执行请求
            cosXml.PartCopy(request, delegate(CosResult result)
            {
                UploadPartCopyResult getObjectResult = result as UploadPartCopyResult;
                Console.WriteLine(getObjectResult.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());
                }
            });
        }
Example #2
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);
            }
        }
Example #3
0
        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);
            }
        }
        /// 拷贝一个分片
        public void UploadPartCopy()
        {
            //.cssg-snippet-body-start:[upload-part-copy]
            try
            {
                string sourceAppid  = "1250000000";              //账号 appid
                string sourceBucket = "sourcebucket-1250000000"; //"源对象所在的存储桶
                string sourceRegion = "COS_REGION";              //源对象的存储桶所在的地域
                string sourceKey    = "sourceObject";            //源对象键
                //构造源对象属性
                COSXML.Model.Tag.CopySourceStruct copySource = new CopySourceStruct(sourceAppid,
                                                                                    sourceBucket, sourceRegion, sourceKey);

                string bucket                 = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID
                string key                    = "exampleobject";            //对象键
                string uploadId               = this.uploadId;              //初始化分块上传返回的uploadId
                int    partNumber             = 1;                          //分块编号,必须从1开始递增
                UploadPartCopyRequest request = new UploadPartCopyRequest(bucket, key,
                                                                          partNumber, uploadId);
                //设置拷贝源
                request.SetCopySource(copySource);
                //设置复制分块(指定块的范围,如 0 ~ 1M)
                request.SetCopyRange(0, 1024 * 1024);
                //执行请求
                UploadPartCopyResult result = cosXml.PartCopy(request);
                //请求成功
                //获取返回分块的eTag,用于后续CompleteMultiUploads
                this.eTag = result.copyObject.eTag;
                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
        }
        private void PartCopy()
        {
            int size = sliceList.Count;

            sliceCount = size;
            uploadCopyCopyRequestList = new List <UploadPartCopyRequest>(size);
            for (int i = 0; i < size; i++)
            {
                if (isExit)
                {
                    return;
                }
                SliceStruct sliceStruct = sliceList[i];
                if (!sliceStruct.isAlreadyUpload)
                {
                    UploadPartCopyRequest uploadPartCopyRequest = new UploadPartCopyRequest(bucket, key, sliceStruct.partNumber, uploadId);
                    uploadPartCopyRequest.SetCopySource(copySource);
                    uploadPartCopyRequest.SetCopyRange(sliceStruct.sliceStart, sliceStruct.sliceEnd);
                    uploadCopyCopyRequestList.Add(uploadPartCopyRequest);
                    cosXmlServer.PartCopy(uploadPartCopyRequest, delegate(CosResult result)
                    {
                        lock (syncExit)
                        {
                            if (isExit)
                            {
                                return;
                            }
                        }
                        UploadPartCopyResult uploadPartCopyResult = result as UploadPartCopyResult;
                        sliceStruct.eTag = uploadPartCopyResult.copyObject.eTag;
                        lock (syncPartCopyCount)
                        {
                            sliceCount--;
                            if (sliceCount == 0)
                            {
                                OnPart();
                                return;
                            }
                        }
                    }, delegate(CosClientException clientEx, CosServerException serverEx)
                    {
                        lock (syncExit)
                        {
                            if (isExit)
                            {
                                return;
                            }
                        }
                        if (UpdateTaskState(TaskState.FAILED))
                        {
                            OnFailed(clientEx, serverEx);
                        }
                        return;
                    });
                }
                else
                {
                    lock (syncPartCopyCount)
                    {
                        sliceCount--;
                        if (sliceCount == 0)
                        {
                            OnPart();
                            return;
                        }
                    }
                }
            }
        }