API | Operation | Description |
Downloading an object | Downloads an object to the local file system |
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") // Set the default region. For abbreviations of COS regions, visit https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1..Build();string secretId = "SECRET_ID"; // SecretId of the TencentCloud API. For more information about how to obtain the API key, see https://console.tencentcloud.com/cam/capi.string secretKey = "SECRET_KEY"; // SecretKey of the TencentCloud API. For more information about how to obtain the API key, see https://console.tencentcloud.com/cam/capi.long durationSecond = 600; // Validity period of the request signature in secondsQCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,secretKey, durationSecond);this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);}/// Download an object via advanced APIpublic async Task TransferDownloadObject(){// Initialize TransferConfigTransferConfig transferConfig = new TransferConfig();// Manually set the part threshold for advanced APIs to 20 MB (default), which is supported from v5.4.26//transferConfig.DivisionForDownload = 20 * 1024 * 1024;// Manually set the part size of advanced APIs to 10 MB (default: 5 MB). We recommend you not set the size to a very small value because this may result in frequent retries or unacceptable download speeds.//transferConfig.SliceSizeForDownload = 10 * 1024 * 1024;// Initialize TransferManagerTransferManager transferManager = new TransferManager(cosXml, transferConfig);String bucket = "examplebucket-1250000000"; // Bucket name in the format of BucketName-APPIDString cosPath = "exampleobject"; // Location identifier of the object in the bucket, i.e., the object keystring localDir = System.IO.Path.GetTempPath();// Local file directorystring localFileName = "my-local-temp-file"; // Filename of the local file// Download an objectCOSXMLDownloadTask downloadTask = new COSXMLDownloadTask(bucket, cosPath,localDir, localFileName);// Manually set maximum tasks (default: 5) for advanced APIs, which is supported from v5.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){// Request failedConsole.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){// Request failedConsole.WriteLine("CosServerException: " + serverEx.GetInfo());}}static void Main(string[] args){TransferDownloadObjectModel m = new TransferDownloadObjectModel();/// Download an object via advanced APIm.TransferDownloadObject().Wait();}}}
COSXMLDownloadTask downloadTask = new COSXMLDownloadTask(request);// Enable checkpoint restart. If an object is not completely downloaded, the download will be started from the checkpoint.// If the local file contains content that does not meet the requirements of the current download, the download may fail. You can delete the file and try again.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();// Initialize TransferManagerTransferManager transferManager = new TransferManager(cosXml, transferConfig);// Bucket name in the format of `BucketName-APPID`. You can get APPID by referring to https://console.tencentcloud.com/developer.string bucket = "examplebucket-1250000000";string localDir = System.IO.Path.GetTempPath();// Local file directoryfor (int i = 0; i < 5; i++) {// Download an objectstring cosPath = "exampleobject" + i; // Location identifier of an object in the bucket, i.e. the object keystring localFileName = "my-local-temp-file"; // Filename of the local fileCOSXMLDownloadTask downloadTask = new COSXMLDownloadTask(bucket, cosPath,localDir, localFileName);await transferManager.DownloadAsync(downloadTask);}
TransferConfig transferConfig = new TransferConfig();// Initialize TransferManagerTransferManager transferManager = new TransferManager(cosXml, transferConfig);String bucket = "examplebucket-1250000000"; // Bucket name in the format of BucketName-APPIDString cosPath = "exampleobject"; // Location identifier of the object in the bucket, i.e., the object keystring localDir = System.IO.Path.GetTempPath();// Local file directorystring localFileName = "my-local-temp-file"; // Filename of the local fileGetObjectRequest request = new GetObjectRequest(bucket,cosPath, localDir, localFileName);request.LimitTraffic(8 * 1000 * 1024); // Limit the speed to 1 MB/s.COSXMLDownloadTask 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") // Set the default region. For abbreviations of COS regions, visit https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1..Build();string secretId = "SECRET_ID"; // SecretId of the TencentCloud API. For more information about how to obtain the API key, see https://console.tencentcloud.com/cam/capi.string secretKey = "SECRET_KEY"; // SecretKey of the TencentCloud API. For more information about how to obtain the API key, see https://console.tencentcloud.com/cam/capi.long durationSecond = 600; // Validity period of each request signature in secondsQCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,secretKey, durationSecond);this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);}public void GetObjectsFromFolder(){// Note: There is no actual API for downloading objects from a folder in COS// You can download objects from a folder or perform other similar operations by a combination of listing specified prefixes and downloading all the listed object keys.// Next, list objects and download them asynchronously.String nextMarker = null;List<string> downloadList = new List<string>();string bucket = "examplebucket-1250000000";string prefix = "folder1/"; // Specify a prefix.// Request until there is no next page of datado{// Bucket name in the format of `BucketName-APPID`. You can get APPID by referring to https://console.tencentcloud.com/developer.GetBucketRequest listRequest = new GetBucketRequest(bucket);// Obtain all objects and subdirectories in folder1/.listRequest.SetPrefix(prefix);listRequest.SetMarker(nextMarker);// Execute the list object request.GetBucketResult listResult = cosXml.GetBucket(listRequest);ListBucket info = listResult.listBucket;// List objectsList<ListBucket.Contents> objects = info.contentsList;// nextMarker for the next pagenextMarker = info.nextMarker;// List objectsforeach (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();// Local file directoryList<Task> taskList = new List<Task>();for (int i = 0; i < downloadList.Count; i++){// Traverse the task list and write downloaded contents to the filename_i file.COSXMLDownloadTask downloadTask = new COSXMLDownloadTask(bucket, downloadList[i],localDir, "filename_" + i.ToString());// Download asynchronously and add to the task list.Task task = transferManager.DownloadAsync(downloadTask);taskList.Add(task);}Console.WriteLine("download tasks submitted, total " + taskList.Count + " tasks added");//Wait for all tasks in TaskList to finish.foreach(Task task in taskList){task.Wait();Console.WriteLine("download completed");}}static void Main(string[] args){TransferDownloadObjectModel m = new TransferDownloadObjectModel();/// Download objects from the folder in batches.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") // Set the default region. For abbreviations of COS regions, visit https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1..Build();string secretId = "SECRET_ID"; // SecretId of the TencentCloud API. For more information about how to obtain the API key, see https://console.tencentcloud.com/cam/capi.string secretKey = "SECRET_KEY"; // SecretKey of the TencentCloud API. For more information about how to obtain the API key, see https://console.tencentcloud.com/cam/capi.long durationSecond = 600; // Validity period of the request signature in secondsQCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,secretKey, durationSecond);this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);}/// Download an objectpublic void GetObject(){//.cssg-snippet-body-start:[get-object]try{// Bucket name in the format of `BucketName-APPID`. You can get APPID by referring to https://console.tencentcloud.com/developer.string bucket = "examplebucket-1250000000";string key = "exampleobject"; // Object keystring localDir = System.IO.Path.GetTempPath();// Local file directorystring localFileName = "my-local-temp-file"; // Filename of the local fileGetObjectRequest request = new GetObjectRequest(bucket, key, localDir, localFileName);// Set the progress callbackrequest.SetCosProgressCallback(delegate (long completed, long total){Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));});// Execute the requestGetObjectResult result = cosXml.GetObject(request);// Request succeededConsole.WriteLine(result.GetResultInfo());}catch (COSXML.CosException.CosClientException clientEx){// Request failedConsole.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){// Request failedConsole.WriteLine("CosServerException: " + serverEx.GetInfo());}}static void Main(string[] args){GetObjectModel m = new GetObjectModel();/// Download an objectm.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") // Set the default region. For abbreviations of COS regions, visit https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1..Build();string secretId = "SECRET_ID"; // SecretId of the TencentCloud API. For more information about how to obtain the API key, see https://console.tencentcloud.com/cam/capi.string secretKey = "SECRET_KEY"; // SecretKey of the TencentCloud API. For more information about how to obtain the API key, see https://console.tencentcloud.com/cam/capi.long durationSecond = 600; // Validity period of the request signature in secondsQCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,secretKey, durationSecond);this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);}/// Download the returned bytes datapublic void downloadToMem() {try{// Bucket name in the format of `BucketName-APPID`. You can get APPID by referring to https://console.tencentcloud.com/developer.string bucket = "examplebucket-1250000000";string key = "exampleobject"; // Object keyGetObjectBytesRequest request = new GetObjectBytesRequest(bucket, key);// Set the progress callbackrequest.SetCosProgressCallback(delegate (long completed, long total){Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));});// Execute the requestGetObjectBytesResult result = cosXml.GetObject(request);// Get content for the byte arraybyte[] content = result.content;// Request succeededConsole.WriteLine(result.GetResultInfo());}catch (COSXML.CosException.CosClientException clientEx){// Request failedConsole.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){// Request failedConsole.WriteLine("CosServerException: " + serverEx.GetInfo());}}// .cssg-methods-pragmastatic void Main(string[] args){GetObjectModel m = new GetObjectModel();/// Download the object to memorym.downloadToMem();// .cssg-methods-pragma}}}
Was this page helpful?