API | 操作名 | 操作描述 |
下载对象 | 下载一个对象至本地 |
using COSXML.Model.Object;using COSXML.Auth;using COSXML.Transfer;using System;using COSXML;using System.Threading.Tasks;namespace COSSnippet{public class TransferDownloadObjectModel {private CosXml cosXml;TransferDownloadObjectModel() {CosXmlConfig config = new CosXmlConfig.Builder().SetRegion("COS_REGION") // 设置默认的地域, COS 地域的简称请参照 https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.Build();string secretId = "SECRET_ID"; // 云 API 密钥 SecretId, 获取 API 密钥请参照 https://console.tencentcloud.com/cam/capistring secretKey = "SECRET_KEY"; // 云 API 密钥 SecretKey, 获取 API 密钥请参照 https://console.tencentcloud.com/cam/capilong durationSecond = 600; //每次请求签名有效时长,单位为秒QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,secretKey, durationSecond);this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);}/// 高级接口下载对象public async Task TransferDownloadObject(){// 初始化 TransferConfigTransferConfig transferConfig = new TransferConfig();// 手动设置高级下载接口的分块阈值为 20MB(默认为20MB), 从5.4.26版本开始支持!//transferConfig.DivisionForDownload = 20 * 1024 * 1024;// 手动设置高级下载接口的分块大小为 10MB(默认为5MB),不建议此处设置过小的分块值,可能导致频繁重试或下载速度不合预期//transferConfig.SliceSizeForDownload = 10 * 1024 * 1024;// 初始化 TransferManagerTransferManager transferManager = new TransferManager(cosXml, transferConfig);String bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPIDString cosPath = "exampleobject"; //对象在存储桶中的位置标识符,即称对象键string localDir = System.IO.Path.GetTempPath();//本地文件夹string localFileName = "my-local-temp-file"; //指定本地保存的文件名// 下载对象COSXMLDownloadTask downloadTask = new COSXMLDownloadTask(bucket, cosPath,localDir, localFileName);// 手动设置高级下载接口的并发数 (默认为5), 从5.4.26版本开始支持!//downloadTask.SetMaxTasks(10);downloadTask.progressCallback = delegate (long completed, long total){Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));};try {COSXML.Transfer.COSXMLDownloadTask.DownloadTaskResult result = awaittransferManager.DownloadAsync(downloadTask);Console.WriteLine(result.GetResultInfo());string eTag = result.eTag;}catch (COSXML.CosException.CosClientException clientEx){//请求失败Console.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){//请求失败Console.WriteLine("CosServerException: " + serverEx.GetInfo());}}static void Main(string[] args){TransferDownloadObjectModel m = new TransferDownloadObjectModel();/// 高级接口下载对象m.TransferDownloadObject().Wait();}}}
COSXMLDownloadTask downloadTask = new COSXMLDownloadTask(request);//开启断点续传,当本地存在未下载完成文件时,追加下载到文件末尾//本地文件已存在部分不符合本次下载的内容,可能导致下载失败,请删除文件重试downloadTask.SetResumableDownload(true);try {COSXML.Transfer.COSXMLDownloadTask.DownloadTaskResult result = awaittransferManager.DownloadAsync(downloadTask);Console.WriteLine(result.GetResultInfo());string eTag = result.eTag;} catch (Exception e) {Console.WriteLine("CosException: " + e);}
TransferConfig transferConfig = new TransferConfig();// 初始化 TransferManagerTransferManager transferManager = new TransferManager(cosXml, transferConfig);// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";string localDir = System.IO.Path.GetTempPath();//本地文件夹for (int i = 0; i < 5; i++) {// 下载对象string cosPath = "exampleobject" + i; //对象在存储桶中的位置标识符,即称对象键string localFileName = "my-local-temp-file"; //指定本地保存的文件名COSXMLDownloadTask downloadTask = new COSXMLDownloadTask(bucket, cosPath,localDir, localFileName);await transferManager.DownloadAsync(downloadTask);}
TransferConfig transferConfig = new TransferConfig();// 初始化 TransferManagerTransferManager transferManager = new TransferManager(cosXml, transferConfig);String bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPIDString cosPath = "exampleobject"; //对象在存储桶中的位置标识符,即称对象键string localDir = System.IO.Path.GetTempPath();//本地文件夹string localFileName = "my-local-temp-file"; //指定本地保存的文件名GetObjectRequest request = new GetObjectRequest(bucket,cosPath, localDir, localFileName);request.LimitTraffic(8 * 1000 * 1024); // 限制为1MB/sCOSXMLDownloadTask downloadTask = new COSXMLDownloadTask(request);await transferManager.DownloadAsync(downloadTask);
using COSXML.Common;using COSXML.CosException;using COSXML.Model;using COSXML.Model.Object;using COSXML.Model.Tag;using COSXML.Model.Bucket;using COSXML.Model.Service;using COSXML.Utils;using COSXML.Auth;using COSXML.Transfer;using System;using COSXML;using System.Collections.Generic;using System.IO;using System.Linq;using System.Reflection;using System.Text;using System.Threading;using System.Threading.Tasks;namespace COSSnippet{public class TransferDownloadObjectModel {private CosXml cosXml;private string nextMarker;TransferDownloadObjectModel() {CosXmlConfig config = new CosXmlConfig.Builder().SetRegion("COS_REGION") // 设置默认的地域, COS 地域的简称请参照 https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.Build();string secretId = "SECRET_ID"; // 云 API 密钥 SecretId, 获取 API 密钥请参照 https://console.tencentcloud.com/cam/capistring secretKey = "SECRET_KEY"; // 云 API 密钥 SecretKey, 获取 API 密钥请参照 https://console.tencentcloud.com/cam/capilong durationSecond = 600; //每次请求签名有效时长,单位为秒QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,secretKey, durationSecond);this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);}public void GetObjectsFromFolder(){// 注意:COS中实际不存在文件夹下载的接口// 需要通过组合 “指定前缀列出” 和 “遍历列出的对象key做下载” 两种操作,实现类似文件夹下载的操作// 下面的操作,把对象列出到列表里,然后异步下载列表中的对象String nextMarker = null;List<string> downloadList = new List<string>();string bucket = "examplebucket-1250000000";string prefix = "folder1/"; //指定前缀// 循环请求直到没有下一页数据do{// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.tencentcloud.com/developerGetBucketRequest listRequest = new GetBucketRequest(bucket);//获取 folder1/ 下的所有对象以及子目录listRequest.SetPrefix(prefix);listRequest.SetMarker(nextMarker);//执行列出对象请求GetBucketResult listResult = cosXml.GetBucket(listRequest);ListBucket info = listResult.listBucket;// 对象列表List<ListBucket.Contents> objects = info.contentsList;// 下一页的下标nextMarker = info.nextMarker;//对象列表foreach (var content in objects){downloadList.Add(content.key);Console.WriteLine("adding key:" + content.key);}} while (nextMarker != null);Console.WriteLine("download list construct done, " + downloadList.Count + " objects added");TransferConfig transferConfig = new TransferConfig();TransferManager transferManager = new TransferManager(cosXml, transferConfig);string localDir = System.IO.Path.GetTempPath();//本地文件夹List<Task> taskList = new List<Task>();for (int i = 0; i < downloadList.Count; i++){// 遍历待下载列表,下载内容写到 filename_i 文件中COSXMLDownloadTask downloadTask = new COSXMLDownloadTask(bucket, downloadList[i],localDir, "filename_" + i.ToString());// 异步下载, 加入task队列Task task = transferManager.DownloadAsync(downloadTask);taskList.Add(task);}Console.WriteLine("download tasks submitted, total " + taskList.Count + " tasks added");//等待TaskList中所有Task结束foreach(Task task in taskList){task.Wait();Console.WriteLine("download completed");}}static void Main(string[] args){TransferDownloadObjectModel m = new TransferDownloadObjectModel();/// 批量下载文件夹中文件m.GetObjectsFromFolder();}}}
downloadTask.Pause();
downloadTask.Resume();
downloadTask.Cancel();
using COSXML.Model.Object;using COSXML.Auth;using System;using COSXML;namespace COSSnippet{public class GetObjectModel {private CosXml cosXml;GetObjectModel() {CosXmlConfig config = new CosXmlConfig.Builder().SetRegion("COS_REGION") // 设置默认的地域, COS 地域的简称请参照 https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.Build();string secretId = "SECRET_ID"; // 云 API 密钥 SecretId, 获取 API 密钥请参照 https://console.tencentcloud.com/cam/capistring secretKey = "SECRET_KEY"; // 云 API 密钥 SecretKey, 获取 API 密钥请参照 https://console.tencentcloud.com/cam/capilong durationSecond = 600; //每次请求签名有效时长,单位为秒QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,secretKey, durationSecond);this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);}/// 下载对象public void GetObject(){//.cssg-snippet-body-start:[get-object]try{// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";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());}}static void Main(string[] args){GetObjectModel m = new GetObjectModel();/// 下载对象m.GetObject();}}}
using COSXML.Model.Object;using COSXML.Auth;using System;using COSXML;namespace COSSnippet{public class GetObjectModel {private CosXml cosXml;GetObjectModel() {CosXmlConfig config = new CosXmlConfig.Builder().SetRegion("COS_REGION") // 设置默认的地域, COS 地域的简称请参照 https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.Build();string secretId = "SECRET_ID"; // 云 API 密钥 SecretId, 获取 API 密钥请参照 https://console.tencentcloud.com/cam/capistring secretKey = "SECRET_KEY"; // 云 API 密钥 SecretKey, 获取 API 密钥请参照 https://console.tencentcloud.com/cam/capilong durationSecond = 600; //每次请求签名有效时长,单位为秒QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,secretKey, durationSecond);this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);}/// 下载返回 bytes 数据public void downloadToMem() {try{// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";string key = "exampleobject"; //对象键GetObjectBytesRequest request = new GetObjectBytesRequest(bucket, key);//设置进度回调request.SetCosProgressCallback(delegate (long completed, long total){Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));});//执行请求GetObjectBytesResult result = cosXml.GetObject(request);//获取内容到 byte 数组中byte[] content = result.content;//请求成功Console.WriteLine(result.GetResultInfo());}catch (COSXML.CosException.CosClientException clientEx){//请求失败Console.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){//请求失败Console.WriteLine("CosServerException: " + serverEx.GetInfo());}}// .cssg-methods-pragmastatic void Main(string[] args){GetObjectModel m = new GetObjectModel();/// 下载对象到内存中m.downloadToMem();// .cssg-methods-pragma}}}
本页内容是否解决了您的问题?