public static void Upload(string bucket, string key, string srcPath, long offset, long sendContentLength) { COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, null, key); uploadTask.SetSrcPath(srcPath, offset, sendContentLength); uploadTask.progressCallback = delegate(long completed, long total) { Console.WriteLine(String.Format("progress = {0} / {1} : {2:##.##}%", completed, total, completed * 100.0 / total)); }; uploadTask.successCallback = delegate(CosResult cosResult) { COSXML.Transfer.COSXMLUploadTask.UploadTaskResult result = cosResult as COSXML.Transfer.COSXMLUploadTask.UploadTaskResult; QLog.D("XIAO", result.GetResultInfo()); Console.WriteLine(result.GetResultInfo()); Console.WriteLine(String.Format("currentThread id = {0}", Thread.CurrentThread.ManagedThreadId)); }; uploadTask.failCallback = 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()); } Console.WriteLine(String.Format("currentThread id = {0}", Thread.CurrentThread.ManagedThreadId)); }; transferManager.Upload(uploadTask); }
internal static async Task <String> PutObject(CosXmlServer cosXml) { string cosKey = "cosKey"; //.cssg-snippet-body-start:[transfer-upload-file] // 初始化 TransferConfig TransferConfig transferConfig = new TransferConfig(); // 初始化 TransferManager TransferManager transferManager = new TransferManager(cosXml, transferConfig); String cosPath = cosKey; //对象在存储桶中的位置标识符,即称对象键 String srcPath = @"本地绝对路径"; //本地文件绝对路径 // 上传对象 COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, cosPath); uploadTask.SetSrcPath(srcPath); uploadTask.progressCallback = delegate(long completed, long total) { Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total)); }; try { COSXML.Transfer.COSXMLUploadTask.UploadTaskResult result = await transferManager.UploadAsync(uploadTask); Console.WriteLine(result.GetResultInfo()); string eTag = result.eTag; } catch (Exception e) { Console.WriteLine("CosException: " + e); } return(cosKey); }
/// 上传暂停、续传、取消 public async void TransferUploadInteract() { TransferConfig transferConfig = new TransferConfig(); TransferManager transferManager = new TransferManager(cosXml, transferConfig); string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID string cosPath = "exampleobject"; //对象在存储桶中的位置标识符,即称对象键 string srcPath = @"temp-source-file"; //本地文件绝对路径 // 上传对象 COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, cosPath); uploadTask.SetSrcPath(srcPath); await transferManager.UploadAsync(uploadTask); //.cssg-snippet-body-start:[transfer-upload-pause] uploadTask.Pause(); //.cssg-snippet-body-end //.cssg-snippet-body-start:[transfer-upload-resume] uploadTask.Resume(); //.cssg-snippet-body-end //.cssg-snippet-body-start:[transfer-upload-cancel] uploadTask.Cancel(); //.cssg-snippet-body-end }
/// 高级接口上传对象 public async void TransferUploadFile() { //.cssg-snippet-body-start:[transfer-upload-file] // 初始化 TransferConfig TransferConfig transferConfig = new TransferConfig(); // 初始化 TransferManager TransferManager transferManager = new TransferManager(cosXml, transferConfig); String bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID String cosPath = "exampleobject"; //对象在存储桶中的位置标识符,即称对象键 String srcPath = @"temp-source-file"; //本地文件绝对路径 // 上传对象 COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, cosPath); uploadTask.SetSrcPath(srcPath); uploadTask.progressCallback = delegate(long completed, long total) { Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total)); }; try { COSXML.Transfer.COSXMLUploadTask.UploadTaskResult result = await transferManager.UploadAsync(uploadTask); Console.WriteLine(result.GetResultInfo()); string eTag = result.eTag; } catch (Exception e) { Console.WriteLine("CosException: " + e); } //.cssg-snippet-body-end }
private void UploadFile(string bucket, string fileKey, string filePath, OnProgressCallback progressCb, OnSuccessCallback <CosResult> successCb, OnFailedCallback failedCb) { COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, null, fileKey) { progressCallback = progressCb, successCallback = successCb, failCallback = failedCb }; uploadTask.SetSrcPath(filePath); transferManager.Upload(uploadTask); }
/// <summary> /// 高级接口上传对象 /// </summary> /// <returns></returns> public async Task TransferUploadFile(string filePath) { CosXmlConfig config = new CosXmlConfig.Builder() .SetRegion(_cosConfig.Region) //设置一个默认的存储桶地域 .Build(); string secretId = _cosConfig.SecretId; //云 API 密钥 SecretId string secretKey = _cosConfig.SecretKey; //云 API 密钥 SecretKey long durationSecond = 600; //每次请求签名有效时长,单位为秒 QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); CosXml cosXml = new CosXmlServer(config, qCloudCredentialProvider); // 初始化 TransferConfig TransferConfig transferConfig = new TransferConfig(); // 初始化 TransferManager TransferManager transferManager = new TransferManager(cosXml, transferConfig); string bucket = _cosConfig.Bucket; //存储桶,格式:BucketName-APPID string cosPath = _cosConfig.UploadKey; //对象在存储桶中的位置标识符,即称对象键 // 上传对象 COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, cosPath); uploadTask.SetSrcPath(filePath); //本地文件绝对路径 uploadTask.progressCallback = delegate(long completed, long total) { Console.WriteLine(string.Format("progress = {0:##.##}%", completed * 100.0 / total)); }; uploadTask.successCallback = delegate(CosResult cosResult) { COSXML.Transfer.COSXMLUploadTask.UploadTaskResult result = cosResult as COSXML.Transfer.COSXMLUploadTask.UploadTaskResult; Console.WriteLine(result.GetResultInfo()); string eTag = result.eTag; }; uploadTask.failCallback = delegate(CosClientException clientEx, CosServerException serverEx) { if (clientEx != null) { Console.WriteLine("CosClientException: " + clientEx); } if (serverEx != null) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } }; await transferManager.UploadAsync(uploadTask); }
public static void UploadWithUploadId(CosXml cosXml, string bucket, string key, string srcPath, long offset, long sendContentLength) { TransferConfig transferConfig = new TransferConfig(); transferManager = new TransferManager(cosXml, new TransferConfig()); COSXML.Model.Object.InitMultipartUploadRequest initMultipartUploadRequest = new COSXML.Model.Object.InitMultipartUploadRequest(bucket, key); COSXML.Model.Object.InitMultipartUploadResult initMultipartUploadResult = cosXml.InitMultipartUpload(initMultipartUploadRequest); string upLoadId = initMultipartUploadResult.initMultipartUpload.uploadId; //string content = "this is partNumber 1"; byte[] data = new byte[transferConfig.SliceSizeForUpload]; COSXML.Model.Object.UploadPartRequest uploadPartRequest = new COSXML.Model.Object.UploadPartRequest(bucket, key, 1, upLoadId, data); COSXML.Model.Object.UploadPartResult uploadPartResult = cosXml.UploadPart(uploadPartRequest); string eTag = uploadPartResult.eTag; COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, null, key) { progressCallback = delegate(long completed, long total) { Console.WriteLine(String.Format("progress = {0} / {1} : {2:##.##}%", completed, total, completed * 100.0 / total)); }, successCallback = delegate(CosResult cosResult) { COSXML.Transfer.COSXMLUploadTask.UploadTaskResult result = cosResult as COSXML.Transfer.COSXMLUploadTask.UploadTaskResult; QLog.D("XIAO", result.GetResultInfo()); Console.WriteLine(result.GetResultInfo()); Console.WriteLine(String.Format("currentThread id = {0}", Thread.CurrentThread.ManagedThreadId)); }, failCallback = 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()); } Console.WriteLine(String.Format("currentThread id = {0}", Thread.CurrentThread.ManagedThreadId)); } }; uploadTask.SetSrcPath(srcPath, offset, sendContentLength); uploadTask.SetUploadId(upLoadId); transferManager.Upload(uploadTask); }
/// 设置自定义头部 public void SetCustomHeaders() { string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID string key = "exampleobject"; //对象键 string srcPath = @"temp-source-file"; //本地文件绝对路径 //.cssg-snippet-body-start:[set-custom-headers] PutObjectRequest request = new PutObjectRequest(bucket, key, srcPath); request.SetRequestHeader("x-cos-meta-key", "value"); request.SetRequestHeader("Content-Disposition", "attachment"); COSXMLUploadTask uploadTask = new COSXMLUploadTask(request); uploadTask.SetSrcPath(srcPath); //.cssg-snippet-body-end }
internal static void UploadDirectory(CosXmlServer cosXml) { //.cssg-snippet-body-start:[transfer-upload-file] // 初始化 TransferConfig TransferConfig transferConfig = new TransferConfig(); // 初始化 TransferManager TransferManager transferManager = new TransferManager(cosXml, transferConfig); //本地文件夹绝对路径 String dir = @"本地文件夹绝对路径"; var files = System.IO.Directory.GetFiles(dir); var tasks = new List <Task>(); foreach (var file in files) { Console.WriteLine("Enqueue Upload: " + file); //对象在存储桶中的位置标识符,即称对象键 String cosPath = new FileInfo(file).Name; // 上传对象 COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, cosPath); uploadTask.SetSrcPath(file); tasks.Add(transferManager.UploadAsync(uploadTask)); } try { // Wait for all the tasks to finish. Task.WaitAll(tasks.ToArray()); // We should never get to this point Console.WriteLine("Upload Directory Complete"); } catch (AggregateException e) { Console.WriteLine("\nThe following exceptions have been thrown by WaitAll(): (THIS WAS EXPECTED)"); for (int j = 0; j < e.InnerExceptions.Count; j++) { Console.WriteLine("\n-------------------------------------------------\n{0}", e.InnerExceptions[j].ToString()); } } }
private async Task <COSXML.Transfer.COSXMLUploadTask.UploadTaskResult> MultiUpload(COSXML.CosXml cosXml, string bucket, string key, string srcPath) { TransferConfig transferConfig = new TransferConfig(); TransferManager transferManager = new TransferManager(cosXml, transferConfig); COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, key); uploadTask.SetSrcPath(srcPath); try { return(await transferManager.UploadAsync(uploadTask)); } catch (Exception e) { throw e; } }
/// 高级接口上传对象 public void TransferUploadFile() { //.cssg-snippet-body-start:[transfer-upload-file] // 初始化 TransferConfig TransferConfig transferConfig = new TransferConfig(); // 初始化 TransferManager TransferManager transferManager = new TransferManager(cosXml, transferConfig); String bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID String cosPath = "exampleobject"; //对象在存储桶中的位置标识符,即称对象键 String srcPath = @"temp-source-file"; //本地文件绝对路径 // 上传对象 COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, cosPath); uploadTask.SetSrcPath(srcPath); uploadTask.progressCallback = delegate(long completed, long total) { Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total)); }; uploadTask.successCallback = delegate(CosResult cosResult) { COSXML.Transfer.COSXMLUploadTask.UploadTaskResult result = cosResult as COSXML.Transfer.COSXMLUploadTask.UploadTaskResult; Console.WriteLine(result.GetResultInfo()); string eTag = result.eTag; }; uploadTask.failCallback = delegate(CosClientException clientEx, CosServerException serverEx) { if (clientEx != null) { Console.WriteLine("CosClientException: " + clientEx); } if (serverEx != null) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } }; transferManager.Upload(uploadTask); //.cssg-snippet-body-end }
public void testUploadTask() { string key = multiKey; PutObjectRequest request = new PutObjectRequest(bucket, key, bigFileSrcPath); request.SetRequestHeader("Content-Type", "image/png"); COSXMLUploadTask uploadTask = new COSXMLUploadTask(request); uploadTask.SetSrcPath(bigFileSrcPath); var autoEvent = new AutoResetEvent(false); string eTag = null; uploadTask.progressCallback = delegate(long completed, long total) { Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total)); }; uploadTask.successCallback = delegate(CosResult cosResult) { COSXML.Transfer.COSXMLUploadTask.UploadTaskResult result = cosResult as COSXML.Transfer.COSXMLUploadTask.UploadTaskResult; Console.WriteLine(result.GetResultInfo()); autoEvent.Set(); eTag = result.eTag; }; uploadTask.failCallback = delegate(CosClientException clientEx, CosServerException serverEx) { if (clientEx != null) { Console.WriteLine("CosClientException: " + clientEx); } if (serverEx != null) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } autoEvent.Set(); }; transferManager.Upload(uploadTask); autoEvent.WaitOne(); Assert.NotNull(eTag); }
/// <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); }
/// 批量上传 public async void TransferBatchUploadObjects() { //.cssg-snippet-body-start:[transfer-batch-upload-objects] TransferConfig transferConfig = new TransferConfig(); // 初始化 TransferManager TransferManager transferManager = new TransferManager(cosXml, transferConfig); string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID for (int i = 0; i < 5; i++) { // 上传对象 string cosPath = "exampleobject" + i; //对象在存储桶中的位置标识符,即称对象键 string srcPath = @"temp-source-file"; //本地文件绝对路径 COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, cosPath); uploadTask.SetSrcPath(srcPath); await transferManager.UploadAsync(uploadTask); } //.cssg-snippet-body-end }
/// 上传时对单链接限速 public void UploadObjectTrafficLimit() { //.cssg-snippet-body-start:[upload-object-traffic-limit] TransferConfig transferConfig = new TransferConfig(); // 初始化 TransferManager TransferManager transferManager = new TransferManager(cosXml, transferConfig); string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID string cosPath = "dir/exampleObject"; // 对象键 string srcPath = @"temp-source-file"; //本地文件绝对路径 PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, cosPath, srcPath); putObjectRequest.LimitTraffic(8 * 1000 * 1000); // 限制为1MB/s COSXMLUploadTask uploadTask = new COSXMLUploadTask(putObjectRequest); uploadTask.SetSrcPath(srcPath); transferManager.Upload(uploadTask); //.cssg-snippet-body-end }
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; } }