public static void AsynUploadParts(COSXML.CosXml cosXml, string bucket, string key, string uploadId, int partNumber, string srcPath) { UploadPartRequest request = new UploadPartRequest(bucket, key, partNumber, uploadId, srcPath); //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); request.SetCosProgressCallback(delegate(long completed, long total) { Console.WriteLine(String.Format("progress = {0} / {1} : {2:##.##}%", completed, total, completed * 100.0 / total)); }); //执行请求 cosXml.UploadPart(request, delegate(CosResult result) { UploadPartResult getObjectResult = result as UploadPartResult; 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()); } }); }
public static void UploadParts(COSXML.CosXml cosXml, string bucket, string key, string uploadId, int partNumber, string srcPath) { try { UploadPartRequest request = new UploadPartRequest(bucket, key, partNumber, uploadId, srcPath); //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); //设置进度回调 request.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 result = cosXml.UploadPart(request); Console.WriteLine(result.GetResultInfo()); QLog.D("XIAO", 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()); } }
/// 上传一个分片 public void UploadPart() { //.cssg-snippet-body-start:[upload-part] try { string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID string key = "exampleobject"; //对象键 string uploadId = "exampleUploadId"; //初始化分块上传返回的uploadId int partNumber = 1; //分块编号,必须从1开始递增 string srcPath = @"temp-source-file"; //本地文件绝对路径 UploadPartRequest request = new UploadPartRequest(bucket, key, partNumber, uploadId, srcPath); //设置进度回调 request.SetCosProgressCallback(delegate(long completed, long total) { Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total)); }); //执行请求 UploadPartResult result = cosXml.UploadPart(request); //请求成功 //获取返回分块的eTag,用于后续CompleteMultiUploads this.eTag = result.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 UploadPart() { activieTasks = 0; int size = sliceList.Count; sliceCount = size; uploadPartRequestMap = new Dictionary <UploadPartRequest, long>(size); uploadPartRequestList = new List <UploadPartRequest>(size); AutoResetEvent resetEvent = new AutoResetEvent(false); for (int i = 0; i < size; i++) { if (activieTasks > MaxConcurrent) { resetEvent.WaitOne(); } lock (syncExit) { if (isExit) { return; } } SliceStruct sliceStruct = sliceList[i]; if (!sliceStruct.isAlreadyUpload) { UploadPartRequest uploadPartRequest = new UploadPartRequest(bucket, key, sliceStruct.partNumber, uploadId, srcPath, sliceStruct.sliceStart, sliceStruct.sliceLength); if (customHeaders != null && customHeaders.ContainsKey(CosRequestHeaderKey.X_COS_TRAFFIC_LIMIT)) { string trafficLimit = customHeaders[CosRequestHeaderKey.X_COS_TRAFFIC_LIMIT]; uploadPartRequest.LimitTraffic(Convert.ToInt64(trafficLimit)); } //打印进度 uploadPartRequest.SetCosProgressCallback( delegate(long completed, long total) { lock (syncProgress) { long dataLen = hasReceiveDataLength + completed - uploadPartRequestMap[uploadPartRequest]; UpdateProgress(dataLen, sendContentLength, false); hasReceiveDataLength = dataLen; uploadPartRequestMap[uploadPartRequest] = completed; } } ); uploadPartRequestMap.Add(uploadPartRequest, 0); uploadPartRequestList.Add(uploadPartRequest); Interlocked.Increment(ref activieTasks); cosXmlServer.UploadPart(uploadPartRequest, delegate(CosResult result) { Interlocked.Decrement(ref activieTasks); UploadPartResult uploadPartResult = result as UploadPartResult; sliceStruct.eTag = uploadPartResult.eTag; lock (syncPartCopyCount) { sliceCount--; if (sliceCount == 0) { OnPart(); } } resetEvent.Set(); }, delegate(CosClientException clientEx, CosServerException serverEx) { Interlocked.Decrement(ref activieTasks); if (UpdateTaskState(TaskState.Failed)) { OnFailed(clientEx, serverEx); } resetEvent.Set(); }); } else { lock (syncPartCopyCount) { sliceCount--; if (sliceCount == 0) { OnPart(); return; } } } } }
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); } }
private void UploadPart() { activieTasks = 0; int size = sliceList.Count; sliceCount = size; uploadPartRequestMap = new Dictionary <UploadPartRequest, long>(size); uploadPartRequestList = new List <UploadPartRequest>(size); for (int i = 0; i < size; i++) { lock (syncExit) { if (isExit) { return; } } SliceStruct sliceStruct = sliceList[i]; while (activieTasks > MAX_ACTIVIE_TASKS) { Thread.Sleep(5); } if (!sliceStruct.isAlreadyUpload) { UploadPartRequest uploadPartRequest = new UploadPartRequest(bucket, key, sliceStruct.partNumber, uploadId, srcPath, sliceStruct.sliceStart, sliceStruct.sliceLength); //打印进度 uploadPartRequest.SetCosProgressCallback(delegate(long completed, long total) { lock (syncProgress) { long dataLen = hasReceiveDataLength + completed - uploadPartRequestMap[uploadPartRequest]; UpdateProgress(dataLen, sendContentLength, false); hasReceiveDataLength = dataLen; uploadPartRequestMap[uploadPartRequest] = completed; } }); uploadPartRequestMap.Add(uploadPartRequest, 0); uploadPartRequestList.Add(uploadPartRequest); activieTasks++; cosXmlServer.UploadPart(uploadPartRequest, delegate(CosResult result) { lock (syncExit) { if (isExit) { return; } } activieTasks--; UploadPartResult uploadPartResult = result as UploadPartResult; sliceStruct.eTag = uploadPartResult.eTag; lock (syncPartCopyCount) { sliceCount--; if (sliceCount == 0) { OnPart(); return; } } }, delegate(CosClientException clientEx, CosServerException serverEx) { lock (syncExit) { if (isExit) { return; } } activieTasks--; if (UpdateTaskState(TaskState.FAILED)) { OnFailed(clientEx, serverEx); } return; }); } else { lock (syncPartCopyCount) { sliceCount--; if (sliceCount == 0) { OnPart(); return; } } } } }