/// 下载对象 public void GetObject() { //.cssg-snippet-body-start:[get-object] try { string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID string key = "exampleobject"; //对象键 string localDir = System.IO.Path.GetTempPath(); //本地文件夹 string localFileName = "my-local-temp-file"; //指定本地保存的文件名 GetObjectRequest request = new GetObjectRequest(bucket, key, localDir, localFileName); //设置进度回调 request.SetCosProgressCallback(delegate(long completed, long total) { Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total)); }); //执行请求 GetObjectResult result = cosXml.GetObject(request); //请求成功 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 }
public async Task GetObjectAsync(string bucketName, string objectName, Action <Stream> callback, CancellationToken cancellationToken = default) { if (string.IsNullOrEmpty(bucketName)) { throw new ArgumentNullException(nameof(bucketName)); } if (string.IsNullOrEmpty(objectName)) { throw new ArgumentNullException(nameof(objectName)); } if (!await ObjectsExistsAsync(bucketName, objectName)) { throw new Exception($"Object '{objectName}' not in bucket '{bucketName}'"); } bucketName = ConvertBucketName(bucketName); await Task.Run(() => { GetObjectBytesRequest request = new GetObjectBytesRequest(bucketName, objectName); //执行请求 GetObjectBytesResult result = _client.GetObject(request); //获取内容 byte[] content = result.content; if (content != null && content.Length > 0) { MemoryStream ms = new MemoryStream(content); callback(ms); } else { throw new Exception("Get object bytes is null."); } }, cancellationToken); }
public void Get(string remotename, Stream stream) { try { cosXml = GetCosXml(); string bucket = _cosOptions.Bucket; string key = GetFullKey(remotename); GetObjectBytesRequest request = new GetObjectBytesRequest(bucket, key); request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), KEY_DURATION_SECOND); GetObjectBytesResult result = cosXml.GetObject(request); byte[] bytes = result.content; Stream ms = new MemoryStream(bytes); Utility.Utility.CopyStream(ms, stream); } catch (COSXML.CosException.CosClientException clientEx) { Logging.Log.WriteErrorMessage(LOGTAG, "Get", clientEx, "Get failed: {0}", remotename); throw; } catch (COSXML.CosException.CosServerException serverEx) { Logging.Log.WriteErrorMessage(LOGTAG, "Get", serverEx, "Get failed: {0}, {1}", remotename, serverEx.GetInfo()); throw; } }
/// 下载时添加盲水印 public void DownloadObjectWithWatermark() { string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID string key = "exampleobject"; //对象键 string localDir = System.IO.Path.GetTempPath(); //本地文件夹 string localFileName = "my-local-temp-file"; //指定本地保存的文件名 //.cssg-snippet-body-start:[download-object-with-watermark] GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key, localDir, localFileName); //处理参数,规则参见:https://cloud.tencent.com/document/product/460/19017 getObjectRequest.SetQueryParameter("watermark/3/type/<type>/image/<imageUrl>/text/<text>", null); GetObjectResult result = cosXml.GetObject(getObjectRequest); //.cssg-snippet-body-end }
/// 获取预签名下载链接 public void GetPresignDownloadUrl() { //.cssg-snippet-body-start:[get-presign-download-url] try { PreSignatureStruct preSignatureStruct = new PreSignatureStruct(); preSignatureStruct.appid = "1250000000"; //腾讯云账号 APPID preSignatureStruct.region = "COS_REGION"; //存储桶地域 preSignatureStruct.bucket = "examplebucket-1250000000"; //存储桶 preSignatureStruct.key = "exampleobject"; //对象键 preSignatureStruct.httpMethod = "GET"; //HTTP 请求方法 preSignatureStruct.isHttps = true; //生成 HTTPS 请求 URL preSignatureStruct.signDurationSecond = 600; //请求签名时间为600s preSignatureStruct.headers = null; //签名中需要校验的 header preSignatureStruct.queryParameters = null; //签名中需要校验的 URL 中请求参数 string requestSignURL = cosXml.GenerateSignURL(preSignatureStruct); //下载请求预签名 URL (使用永久密钥方式计算的签名 URL) string localDir = System.IO.Path.GetTempPath(); //本地文件夹 string localFileName = "my-local-temp-file"; //指定本地保存的文件名 GetObjectRequest request = new GetObjectRequest(null, null, localDir, localFileName); //设置下载请求预签名 URL request.RequestURLWithSign = requestSignURL; //设置进度回调 request.SetCosProgressCallback(delegate(long completed, long total) { Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total)); }); //执行请求 GetObjectResult result = cosXml.GetObject(request); //请求成功 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 }