public static void AsyncGetObject(COSXML.CosXml cosXml, string bucket, string key, string localDir, string localFileName) { GetObjectRequest request = new GetObjectRequest(bucket, key, localDir, localFileName); //设置签名有效时长 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.GetObject(request, delegate(CosResult result) { GetObjectResult getObjectResult = result as GetObjectResult; 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()); } }); }
public void GetObject(COSXML.CosXml cosXml, string bucket, string key, string localDir, string localFileName) { try { GetObjectRequest request = new GetObjectRequest(bucket, key, localDir, localFileName); //设置签名有效时长 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)); }); //执行请求 GetObjectResult result = cosXml.GetObject(request); Console.WriteLine(result.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 async Task <ResponseModel> DownObject(string key, string localDir, string localFileName) { try { string bucket = _buketName + "-" + _appid; //存储桶名称 格式:BucketName-APPID GetObjectRequest request = new GetObjectRequest(bucket, key, localDir, localFileName); //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); //设置进度回调 request.SetCosProgressCallback(delegate(long completed, long total) { Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total)); }); //执行请求 GetObjectResult result = await Task.FromResult(_cosXml.GetObject(request)); return(new ResponseModel { Code = 200, Message = result.GetResultInfo() }); } catch (CosClientException clientEx) { return(new ResponseModel { Code = 0, Message = "CosClientException: " + clientEx.Message }); } catch (CosServerException serverEx) { return(new ResponseModel { Code = 0, Message = serverEx.GetInfo() }); } }
/// <summary> /// 分段下载 /// </summary> /// <param name="key"></param> /// <param name="targetFolder"></param> /// <param name="start"></param> /// <param name="end"></param> /// <returns></returns> public static DownloadResult DownloadObject(DownloadRecord record, string targetFolder, long start, long end) { DownloadResult downloadResult = new DownloadResult(); Random reum = new Random(); int randomdata = reum.Next(100000); string tempFileName = DateUtil.currentTimeMillis(new DateTime()) + randomdata + ".temp"; try { GetObjectRequest request = new GetObjectRequest(BosConfig.BucketName, record.CloudFile.Key, targetFolder, tempFileName); request.SetRange(start, end); //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); //执行请求 GetObjectResult result = getCosXmlServer().GetObject(request); Console.WriteLine("msg" + result.GetResultInfo()); if (String.Equals("OK", result.httpMessage)) { } else if (String.Equals("Requested Range Not Satisfiable", result.httpMessage)) { downloadResult.state = 1; File.Delete(targetFolder + @"\" + tempFileName); return(downloadResult); } else if (String.Equals("Not Found", result.httpMessage)) { downloadResult.state = 2; File.Delete(targetFolder + @"\" + tempFileName); return(downloadResult); } Stream stream = new FileStream(targetFolder + @"\" + tempFileName, FileMode.Open); downloadResult = FileUtil.saveFileContent(stream, record.FileName, targetFolder); File.Delete(targetFolder + @"\" + tempFileName); } catch (COSXML.CosException.CosClientException clientEx) { //请求失败 Console.WriteLine("CosClientException: " + clientEx.Message); downloadResult.state = 2; } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 Console.WriteLine("CosServerException: " + serverEx.GetInfo()); downloadResult.state = 2; } return(downloadResult); }
private void GetObject() { getObjectRequest = new GetObjectRequest(bucket, key, localDir, localFileName); getObjectRequest.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); if (progressCallback != null) { getObjectRequest.SetCosProgressCallback(progressCallback); } getObjectRequest.SetRange(rangeStart, rangeEnd); getObjectRequest.SetLocalFileOffset(localFileOffset); cosXmlServer.GetObject(getObjectRequest, delegate(CosResult result) { lock (syncExit) { if (isExit) { return; } } if (UpdateTaskState(TaskState.COMPLETED)) { GetObjectResult getObjectResult = result as GetObjectResult; DownloadTaskResult downloadTaskResult = new DownloadTaskResult(); downloadTaskResult.SetResult(getObjectResult); if (successCallback != null) { successCallback(downloadTaskResult); } } }, delegate(CosClientException clientEx, CosServerException serverEx) { lock (syncExit) { if (isExit) { return; } } if (UpdateTaskState(TaskState.FAILED)) { if (failCallback != null) { failCallback(clientEx, serverEx); } } }); }
public void GetFileDow(string keyw, string name, OnProgressCallback Dwjindu, string path) { CosXmlConfig config = new CosXmlConfig.Builder() .SetConnectionTimeoutMs(SetConnectionTimeoutMs) //设置连接超时时间,单位毫秒,默认45000ms .SetReadWriteTimeoutMs(SetReadWriteTimeoutMs) //设置读写超时时间,单位毫秒,默认45000ms .IsHttps(IsHttps) //设置默认 HTTPS 请求 .SetAppid(SetAppid) //设置腾讯云账户的账户标识 APPID .SetRegion(SetRegion) //设置一个默认的存储桶地域 .Build(); string secretId = SecretId; //云 API 密钥 SecretId string secretKey = SecretKey; //云 API 密钥 SecretKey long durationSecond = 600; //每次请求签名有效时长,单位为秒 QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); CosXml cosXml = new CosXmlServer(config, qCloudCredentialProvider); try { string bucket = "yuanguhl"; //存储桶,格式:BucketName-APPID string key = keyw; //对象在存储桶中的位置,即称对象键 string localDir = path; //本地文件夹 string localFileName = name; //指定本地保存的文件名 GetObjectRequest request = new GetObjectRequest(bucket, key, localDir, localFileName); request.Region = "ap-beijing"; //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); //设置进度回调 request.SetCosProgressCallback(Dwjindu); //执行请求 GetObjectResult result = cosXml.GetObject(request); //请求成功 } catch (COSXML.CosException.CosClientException clientEx) { //请求失败 throw clientEx; } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 throw serverEx; } }
private void syncDownloadFile(string bucket, string fileKey, string localDir, string localFileName, OnProgressCallback progressCb, OnSuccessCallback <CosResult> successCb, OnFailedCallback failedCb) { try { GetObjectRequest request = new GetObjectRequest(bucket, fileKey, localDir, localFileName); request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); request.SetCosProgressCallback(progressCb); GetObjectResult result = cosXml.GetObject(request); } catch (CosClientException clientEx) { failedCb(clientEx, null); } catch (CosServerException serverEx) { failedCb(null, serverEx); } }
/// <summary> /// 获取图片 /// </summary> /// <param name="fileKey">图片 key</param> /// <param name="localDir">本地文件夹</param> /// <param name="localFileName">本地文件名</param> /// <returns></returns> public async Task GetImage(string fileKey, string localDir, string localFileName) { var tmpFile = "tmp_" + localFileName; var tmpFilePath = localDir + "/" + tmpFile; var localFilePath = localDir + "/" + localFileName; var cosService = await buildCosService(getImageCredential, fileKey); var request = new GetObjectRequest(getImageCredential.TmpCred.Bucket, fileKey, localDir, tmpFile); request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), signValidSec); request.SetCosProgressCallback((c, t) => { double percent = (double)c / t * 100; logger.Trace("下载进度 {}%", percent.ToString("0.00")); }); try { var result = cosService.GetObject(request); //logger.Trace(result.GetResultInfo()); // 下载成功,将临时文件移动到指定位置 if (result.httpCode == 200) { File.Move(tmpFilePath, localFilePath); // 下载失败也会出现残留文件,这里将临时下载残留文件删除 } else { if (File.Exists(tmpFilePath)) { File.Delete(tmpFilePath); } throw new StorageException(result.httpMessage); } } catch (Exception e) { // 下载失败也会出现残留文件,这里将它删除 if (File.Exists(tmpFilePath)) { File.Delete(tmpFilePath); } throw e; } }