Exemplo n.º 1
0
        /// <summary>
        /// 上传大文件、分块上传
        /// </summary>
        /// <param name="key"></param>
        /// <param name="srcPath"></param>
        /// <param name="progressCallback">委托,可用于显示分块信息</param>
        /// <param name="successCallback">委托,当任务成功时回调</param>
        /// <returns></returns>
        public async Task <ResponseModel> UpBigFile(string key, string srcPath, Action <long, long> progressCallback, Action <CosResult> successCallback)
        {
            ResponseModel responseModel = new ResponseModel();
            string        bucket        = _buketName + "-" + _appid; //存储桶名称 格式:BucketName-APPID

            TransferManager  transferManager = new TransferManager(_cosXml, new TransferConfig());
            COSXMLUploadTask uploadTask      = new COSXMLUploadTask(bucket, null, key);

            uploadTask.SetSrcPath(srcPath);
            uploadTask.progressCallback = delegate(long completed, long total)
            {
                progressCallback(completed, total);
                //Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
            };
            uploadTask.successCallback = delegate(CosResult cosResult)
            {
                COSXMLUploadTask.UploadTaskResult result = cosResult as COSXMLUploadTask.UploadTaskResult;
                successCallback(cosResult);
                responseModel.Code    = 200;
                responseModel.Message = result.GetResultInfo();
            };
            uploadTask.failCallback = delegate(CosClientException clientEx, CosServerException serverEx)
            {
                if (clientEx != null)
                {
                    responseModel.Code    = 0;
                    responseModel.Message = clientEx.Message;
                }
                if (serverEx != null)
                {
                    responseModel.Code    = 0;
                    responseModel.Message = "CosServerException: " + serverEx.GetInfo();
                }
            };
            await Task.Run(() =>
            {
                transferManager.Upload(uploadTask);
            });

            return(responseModel);
        }
Exemplo n.º 2
0
        public void Run()
        {
            string srcPath = File.LocalPath + "\\" + File.FileName;    //本地文件绝对路径

            FileRequest fileRequest = new FileRequest();
            int         status      = fileRequest.Upload(srcPath, File.RemotePath + "\\" + File.FileName, out UploadResponse res);

            switch (status)
            {
            case -20000:
                TaskStatusDetectionThread.Abort();
                UploadTaskList.SetFailure(File.Key, "请求服务器失败,请稍后再试。");
                break;

            case -10000:
                TaskStatusDetectionThread.Abort();
                UploadTaskList.SetFailure(File.Key, res.message);
                break;

            case 101:
                TaskStatusDetectionThread.Abort();
                UploadTaskList.SetProgress(File.Key, 1, 1);
                UploadTaskList.SetSuccess(File.Key);
                break;

            case 100:
            {
                string bucket  = res.data.tencentCos.bucket;          //存储桶,格式:BucketName-APPID
                string cosPath = res.data.file.storageName;           //对象在存储桶中的位置标识符,即称对象键

                CosService       cosService       = new CosService(res.data.tencentCos.region);
                CosXml           cosXml           = cosService.getCosXml(res.data.token.credentials.tmpSecretId, res.data.token.credentials.tmpSecretKey, res.data.token.credentials.token, res.data.token.expiredTime);
                TransferConfig   transferConfig   = new TransferConfig();
                TransferManager  transferManager  = new TransferManager(cosXml, transferConfig);
                PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, cosPath, srcPath);
                uploadTask = new COSXMLUploadTask(putObjectRequest);

                uploadTask.SetSrcPath(srcPath);

                uploadTask.progressCallback = delegate(long completed, long total)
                {
                    UploadTaskList.SetProgress(File.Key, completed, total);
                    Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
                };
                uploadTask.successCallback = delegate(CosResult cosResult)
                {
                    TaskStatusDetectionThread.Abort();
                    fileRequest.ConfirmUpload(res.data.file.id, res.data.file.guid);
                    COSXMLUploadTask.UploadTaskResult result = cosResult as COSXMLUploadTask.UploadTaskResult;
                    Console.WriteLine("successCallback: " + result.GetResultInfo());
                    string eTag = result.eTag;
                    UploadTaskList.SetSuccess(File.Key);
                };
                uploadTask.failCallback = delegate(CosClientException clientEx, CosServerException serverEx)
                {
                    if (clientEx != null)
                    {
                        Console.WriteLine("CosClientException: " + clientEx);
                    }
                    if (serverEx != null)
                    {
                        Console.WriteLine("CosServerException: " + serverEx.GetInfo());
                    }
                    TaskStatusDetectionThread.Abort();
                    UploadTaskList.SetFailure(File.Key, "COS上传出错。");
                };

                transferManager.Upload(uploadTask);
            }
            break;

            default:
                TaskStatusDetectionThread.Abort();
                UploadTaskList.SetFailure(File.Key, "未知原因上传失败。");
                break;
            }
        }