NuGet integration method. You can add it in your project's csproj file:<PackageReference Include="Tencent.QCloud.Cos.Sdk" Version="5.4.*" />
dotnet add package Tencent.QCloud.Cos.Sdk
using COSXML;using COSXML.Auth;using COSXML.Model.Object;using COSXML.Model.Bucket;using COSXML.CosException;
CosXmlConfig, QCloudCredentialProvider, and CosXmlServer. Where:CosXmlConfig provides configuration interfaces for the SDK.QCloudCredentialProvider provides interfaces for setting key information.CosXmlServer provides various COS API service interfaces.// Initialize CosXmlConfig.string region = "COS_REGION"; // Set a default bucket region.CosXmlConfig config = new CosXmlConfig.Builder().IsHttps(true) // Set the default HTTPS request..SetRegion(region) // Set a default bucket region..SetDebugLog(true) // Display log..Build(); // Create CosXmlConfig object.
public class CustomQCloudCredentialProvider : DefaultSessionQCloudCredentialProvider{// It is assumed that starting without keys, or an initial temporary key can also be used for initialization.public CustomQCloudCredentialProvider(): base(null, null, 0L, null) {;}public override void Refresh(){//... First request temporary keys through Tencent Cloudstring tmpSecretId = "SECRET_ID"; // "temporary key SecretId", for the temporary key generation and usage guide, see https://www.tencentcloud.com/document/product/436/14048string tmpSecretKey = "SECRET_KEY"; // "temporary key SecretKey", for the temporary key generation and usage guide, see https://www.tencentcloud.com/document/product/436/14048string tmpToken = "COS_TOKEN"; // "temporary key token", for the temporary key generation and usage guide, see https://www.tencentcloud.com/document/product/436/14048long tmpStartTime = 1546860702;//valid start time of the temporary key, accurate to the secondlong tmpExpiredTime = 1546862502;//valid expiration time of the temporary key, accurate to the second// Call the interface to update the keySetQCloudCredential(tmpSecretId, tmpSecretKey,String.Format("{0};{1}", tmpStartTime, tmpExpiredTime), tmpToken);}}QCloudCredentialProvider cosCredentialProvider = new CustomQCloudCredentialProvider();
string tmpSecretId = "SECRET_ID"; // "temporary key SecretId", for the temporary key generation and usage guide, see https://www.tencentcloud.com/document/product/436/14048string tmpSecretKey = "SECRET_KEY"; // "temporary key SecretKey", for the temporary key generation and usage guide, see https://www.tencentcloud.com/document/product/436/14048string tmpToken = "COS_TOKEN"; // "temporary key token", for the temporary key generation and usage guide, see https://www.tencentcloud.com/document/product/436/14048long tmpExpireTime = 1546862502;//valid expiration time of the temporary key, accurate to the secondQCloudCredentialProvider cosCredentialProvider = new DefaultSessionQCloudCredentialProvider(tmpSecretId, tmpSecretKey, tmpExpireTime, tmpToken);
string secretId = Environment.GetEnvironmentVariable("SECRET_ID"); // User's SecretId. It is recommended to use a sub-account key, following the principle of least privilege authorization to reduce usage risks. For obtaining sub-account keys, see https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1string secretKey = Environment.GetEnvironmentVariable("SECRET_KEY"); // User's SecretKey. It is recommended to use a sub-account key, following the principle of least privilege authorization to reduce usage risks. For obtaining sub-account keys, see https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1long durationSecond = 600; //validity duration per request signature, in secondsQCloudCredentialProvider cosCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond);
CosXmlConfig and QCloudCredentialProvider to initialize the CosXmlServer service class. It is recommended to use the service class as a singleton in the program.CosXml cosXml = new CosXmlServer(config, cosCredentialProvider);
using COSXML;using COSXML.Auth;using COSXML.Model.Bucket;namespace COSXMLDemo{public class CreateBucketModel{public CosXml cosXml;// Initialize the COS service instance.private void InitCosXml(){string region = Environment.GetEnvironmentVariable("COS_REGION");CosXmlConfig config = new CosXmlConfig.Builder().SetRegion(region) // Set the default region. For the abbreviation of COS regions, refer to https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.Build();string secretId = Environment.GetEnvironmentVariable("SECRET_ID"); // TencentCloud API key SecretId. To obtain the API key, refer to https://console.tencentcloud.com/cam/capistring secretKey = Environment.GetEnvironmentVariable("SECRET_KEY"); // TencentCloud API key SecretKey. To obtain the API key, refer to https://console.tencentcloud.com/cam/capilong durationSecond = 600; //validity duration per request signature, in secondsQCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond);this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);}CreateBucketModel(){InitCosXml();}public void PutBucket(){try{// Bucket name. The format must be BucketName-APPID. For APPID, refer to https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";PutBucketRequest request = new PutBucketRequest(bucket);request.SetCosACL("private"); //Define the acl attribute of the Object. Valid values: private, public-read-write, public-read; Default value: private//Execute the requestPutBucketResult result = cosXml.PutBucket(request);//Request succeededConsole.WriteLine(result.GetResultInfo());}catch (COSXML.CosException.CosClientException clientEx){Console.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){Console.WriteLine("CosServerException: " + serverEx.GetInfo());}}public static void CreateBucketModelMain(){CreateBucketModel m = new CreateBucketModel();m.PutBucket();}}}
using COSXML;using COSXML.Auth;using COSXML.Model.Service;using COSXML.Model.Tag;namespace COSXMLDemo{public class ListBucketModel{public CosXml cosXml;// Initialize the COS service instance.private void InitCosXml(){string region = Environment.GetEnvironmentVariable("COS_REGION");CosXmlConfig config = new CosXmlConfig.Builder().SetRegion(region) // Set the default region. For the abbreviation of COS regions, refer to https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.Build();string secretId = Environment.GetEnvironmentVariable("SECRET_ID"); // TencentCloud API key SecretId. To obtain the API key, refer to https://console.tencentcloud.com/cam/capistring secretKey = Environment.GetEnvironmentVariable("SECRET_KEY"); // TencentCloud API key SecretKey. To obtain the API key, refer to https://console.tencentcloud.com/cam/capilong durationSecond = 600; //validity duration per request signature, in secondsQCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond);this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);}ListBucketModel(){InitCosXml();}public void GetService(){try{GetServiceRequest request = new GetServiceRequest();//Execute the requestGetServiceResult result = cosXml.GetService(request);// Get all bucketsList<ListAllMyBuckets.Bucket> allBuckets = result.listAllMyBuckets.buckets;foreach (var bucket in allBuckets){Console.WriteLine(bucket.name);}}catch (COSXML.CosException.CosClientException clientEx){Console.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){Console.WriteLine("CosServerException: " + serverEx.GetInfo());}}public static void ListBucketModelMain(){ListBucketModel m = new ListBucketModel();m.GetService();}}}
using System.Runtime.InteropServices;using System.Text;using COSXML.Auth;using COSXML.Transfer;using COSXML;using COSXML.Model.Bucket;using COSXML.Model.Object;namespace COSXMLDemo{public class UploadObject {private CosXml cosXml;// Bucket name. The format must be bucketname-APPID, where for obtaining the APPID, refer to https://console.tencentcloud.com/developerprivate string bucket;public void InitParams(){bucket = Environment.GetEnvironmentVariable("BUCKET");}// Initialize the COS service instance.private void InitCosXml(){// Set variables from Environment.GetEnvironmentVariable. Users can also directly assign values to variables, for example: string region = "ap-guagnzhou";string region = Environment.GetEnvironmentVariable("COS_REGION");CosXmlConfig config = new CosXmlConfig.Builder().SetRegion(region) // Set the default region. For COS region abbreviations, refer to https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.Build();string secretId = Environment.GetEnvironmentVariable("SECRET_ID"); // TencentCloud API key SecretId. To obtain the API key, refer to https://console.tencentcloud.com/cam/capistring secretKey = Environment.GetEnvironmentVariable("SECRET_KEY"); // TencentCloud API key SecretKey. To obtain the API key, refer to https://console.tencentcloud.com/cam/capilong durationSecond = 600; //validity duration per request signature, in secondsQCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond);this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);}UploadObject(){// Custom parameters of the demoInitParams();// Initialize the COS serviceInitCosXml();}public void PutObject(){try {// Bucket name. The format must be bucketname-APPID, where for obtaining the APPID, refer to https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";string key = "exampleobject"; // Object keystring srcPath = @"temp-source-file"; // Absolute path of the local filePutObjectRequest request = new PutObjectRequest(bucket, key, srcPath);// Playback progress callback.request.SetCosProgressCallback(delegate (long completed, long total) {Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));});PutObjectResult result = cosXml.PutObject(request);// Print the returned resultConsole.WriteLine(result.GetResultInfo());} catch (COSXML.CosException.CosClientException clientEx) {Console.WriteLine("CosClientException: " + clientEx);} catch (COSXML.CosException.CosServerException serverEx) {Console.WriteLine("CosServerException: " + serverEx.GetInfo());}}public static void UploadObjectMain(){UploadObject domo = new UploadObject();// Upload a file.domo.PutObject();}}}
using COSXML;using COSXML.Auth;using COSXML.Model.Bucket;using COSXML.Model.Tag;namespace COSXMLDemo{public class ListObjectModel{public CosXml cosXml;// Initialize the COS service instance.public string bucket;public void InitParams(){bucket = Environment.GetEnvironmentVariable("BUCKET");}// Initialize the COS service instance.private void InitCosXml(){string region = Environment.GetEnvironmentVariable("COS_REGION");CosXmlConfig config = new CosXmlConfig.Builder().SetRegion(region) // Set the default region. For the abbreviation of COS regions, refer to https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.Build();string secretId = Environment.GetEnvironmentVariable("SECRET_ID"); // TencentCloud API key SecretId. To obtain the API key, refer to https://console.tencentcloud.com/cam/capistring secretKey = Environment.GetEnvironmentVariable("SECRET_KEY"); // TencentCloud API key SecretKey. To obtain the API key, refer to https://console.tencentcloud.com/cam/capilong durationSecond = 600; //validity duration per request signature, in secondsQCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond);this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);}ListObjectModel(){InitCosXml();InitParams();}// Obtain the first page data of the object versions listpublic void ListObjectsVersioning(){try{// Bucket name. The format must be bucketname-APPID, where APPID can be obtained at https://console.tencentcloud.com/developerstring bucket = "examplebucket-version-1250000000";ListBucketVersionsRequest request = new ListBucketVersionsRequest(bucket);//Execute the requestListBucketVersionsResult result = cosXml.ListBucketVersions(request);//information about the bucketListBucketVersions info = result.listBucketVersions;//Request result statusConsole.WriteLine(result.GetResultInfo());List<ListBucketVersions.Version> objects = info.objectVersionList;List<ListBucketVersions.CommonPrefixes> prefixes = info.commonPrefixesList;//returned informationConsole.WriteLine(info);if (info.isTruncated){// Data is truncated; record the data indexthis.keyMarker = info.nextKeyMarker;this.versionIdMarker = info.nextVersionIdMarker;}}catch (COSXML.CosException.CosClientException clientEx){Console.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){Console.WriteLine("CosServerException: " + serverEx.GetInfo());}}private string keyMarker = "";private string versionIdMarker = "";// Obtain the next page data of the object versions listpublic void ListObjectsVersioningNextPage(){try{// Bucket name. The format must be bucketname-APPID, where APPID can be obtained at https://console.tencentcloud.com/developerstring bucket = "examplebucket-version-1250000000";ListBucketVersionsRequest request = new ListBucketVersionsRequest(bucket);// The ending index of the previous page's datarequest.SetKeyMarker(this.keyMarker);request.SetVersionIdMarker(this.versionIdMarker);//Execute the requestListBucketVersionsResult result = cosXml.ListBucketVersions(request);//Request result statusConsole.WriteLine(result.GetResultInfo());ListBucketVersions info = result.listBucketVersions;Console.WriteLine(info.GetInfo());if (info.isTruncated){// Data is truncated; record the data indexthis.keyMarker = info.nextKeyMarker;this.versionIdMarker = info.nextVersionIdMarker;}}catch (COSXML.CosException.CosClientException clientEx){Console.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){Console.WriteLine("CosServerException: " + serverEx.GetInfo());}}public string nextMarker;//Obtaining the first page of the object listpublic void GetBucketFirstPage(){try{// Bucket name. The format must be bucketname-APPID, where APPID can be obtained at https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";GetBucketRequest request = new GetBucketRequest(bucket);//Execute the requestGetBucketResult result = cosXml.GetBucket(request);//information about the bucketCOSXML.Model.Tag.ListBucket info = result.listBucket;if (info.isTruncated){// Data is truncated; record the data indexthis.nextMarker = info.nextMarker;}Console.WriteLine(result.GetResultInfo());}catch (COSXML.CosException.CosClientException clientEx){Console.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){Console.WriteLine("CosServerException: " + serverEx.GetInfo());}}//Obtaining the second page of the object listpublic void GetBucketNextPage(){try{// Bucket name. The format must be bucketname-APPID, where APPID can be obtained at https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";GetBucketRequest request = new GetBucketRequest(bucket);// Index of the last data pullrequest.SetMarker(this.nextMarker);//Execute the requestGetBucketResult result = cosXml.GetBucket(request);//information about the bucketCOSXML.Model.Tag.ListBucket info = result.listBucket;Console.WriteLine(result.GetResultInfo());}catch (COSXML.CosException.CosClientException clientEx){Console.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){Console.WriteLine("CosServerException: " + serverEx.GetInfo());}}//Obtaining the object list and subdirectoriespublic void GetBucketWithDelimiter(){try{// Bucket name. The format must be bucketname-APPID, where APPID can be obtained at https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";GetBucketRequest request = new GetBucketRequest(bucket);//Obtain objects under a/ and subdirectoriesrequest.SetPrefix("dir/");request.SetDelimiter("/");//Execute the requestGetBucketResult result = cosXml.GetBucket(request);//information about the bucketCOSXML.Model.Tag.ListBucket info = result.listBucket;// Object listList<COSXML.Model.Tag.ListBucket.Contents> objects = info.contentsList;// Subdirectory listList<COSXML.Model.Tag.ListBucket.CommonPrefixes> subDirs = info.commonPrefixesList;Console.WriteLine(result.GetResultInfo());}catch (COSXML.CosException.CosClientException clientEx){Console.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){Console.WriteLine("CosServerException: " + serverEx.GetInfo());}}public static void ListObjectModelMain(){ListObjectModel demo = new ListObjectModel();//Obtaining list objects// demo.GetBucketFirstPage();// demo.GetBucketNextPage();// demo.GetBucketWithDelimiter();demo.ListObjectsVersioning();demo.ListObjectsVersioningNextPage();}}}
using COSXML;using COSXML.Auth;using COSXML.Model.Bucket;using COSXML.Model.Object;using COSXML.Model.Tag;using COSXML.Transfer;namespace COSXMLDemo{public class DownloadObject{public CosXml cosXml;// Initialize the COS service instance.public string bucket;public void InitParams(){bucket = Environment.GetEnvironmentVariable("BUCKET");}// Initialize the COS service instance.private void InitCosXml(){string region = Environment.GetEnvironmentVariable("COS_REGION");CosXmlConfig config = new CosXmlConfig.Builder().SetRegion(region) // Set the default region. For the abbreviation of COS regions, refer to https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.Build();string secretId = Environment.GetEnvironmentVariable("SECRET_ID"); // TencentCloud API key SecretId. To obtain the API key, refer to https://console.tencentcloud.com/cam/capistring secretKey = Environment.GetEnvironmentVariable("SECRET_KEY"); // TencentCloud API key SecretKey. To obtain the API key, refer to https://console.tencentcloud.com/cam/capilong durationSecond = 600; //validity duration per request signature, in secondsQCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond);this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);}DownloadObject(){// Custom parameters of the demoInitParams();// Initialize the COS serviceInitCosXml();}public void GetObject(){try{// Bucket name. The format must be bucketname-APPID, where APPID can be obtained at https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";string key = "exampleobject"; // object keystring localDir = Path.GetTempPath();//local folderstring localFileName = "my-local-temp-file"; //Specify the locally saved file nameGetObjectRequest request = new GetObjectRequest(bucket, key, localDir, localFileName);request.SetCosProgressCallback(delegate (long completed, long total){Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));});//Execute the requestGetObjectResult 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());}}public static void DownloadObjectMain(){DownloadObject demo = new DownloadObject();demo.GetObject();}}}
using COSXML.Model.Object;using COSXML.Model.Tag;using COSXML.Model.Bucket;using COSXML.Auth;using COSXML;namespace COSXMLDemo{public class DeleteObjectModel{public CosXml cosXml;// Initialize the COS service instance.public string bucket;public void InitParams(){bucket = Environment.GetEnvironmentVariable("BUCKET");}// Initialize the COS service instance.private void InitCosXml(){string region = Environment.GetEnvironmentVariable("COS_REGION");CosXmlConfig config = new CosXmlConfig.Builder().SetRegion(region) // Set the default region. For the abbreviation of COS regions, refer to https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.Build();string secretId = Environment.GetEnvironmentVariable("SECRET_ID"); // TencentCloud API key SecretId. To obtain the API key, refer to https://console.tencentcloud.com/cam/capistring secretKey = Environment.GetEnvironmentVariable("SECRET_KEY"); // TencentCloud API key SecretKey. To obtain the API key, refer to https://console.tencentcloud.com/cam/capilong durationSecond = 600; //validity duration per request signature, in secondsQCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond);this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);}DeleteObjectModel(){InitCosXml();InitParams();}// Delete the objectpublic void DeleteObject(){try{// Bucket name. The format must be bucketname-APPID, where APPID can be obtained at https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";string key = "exampleobject"; // object keyDeleteObjectRequest request = new DeleteObjectRequest(bucket, key);//Execute the requestDeleteObjectResult result = cosXml.DeleteObject(request);//Request succeededConsole.WriteLine(result.GetResultInfo());}catch (COSXML.CosException.CosClientException clientEx){Console.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){Console.WriteLine("CosServerException: " + serverEx.GetInfo());}}public static void DeleteObjectModelMain(){DeleteObjectModel m = new DeleteObjectModel();// Delete the objectm.DeleteObject();}}}
Feedback