tencent cloud

Cloud Object Storage

Release Notes and Announcements
Release Notes
Announcements
Product Introduction
Overview
Features
Use Cases
Strengths
Concepts
Regions and Access Endpoints
Specifications and Limits
Service Regions and Service Providers
Billing
Billing Overview
Billing Method
Billable Items
Free Tier
Billing Examples
Viewing and Downloading Bill
Payment Overdue
FAQs
Getting Started
Console
Getting Started with COSBrowser
User Guide
Creating Request
Bucket
Object
Data Management
Batch Operation
Global Acceleration
Monitoring and Alarms
Operations Center
Data Processing
Content Moderation
Smart Toolbox
Data Processing Workflow
Application Integration
User Tools
Tool Overview
Installation and Configuration of Environment
COSBrowser
COSCLI (Beta)
COSCMD
COS Migration
FTP Server
Hadoop
COSDistCp
HDFS TO COS
GooseFS-Lite
Online Tools
Diagnostic Tool
Use Cases
Overview
Access Control and Permission Management
Performance Optimization
Accessing COS with AWS S3 SDK
Data Disaster Recovery and Backup
Domain Name Management Practice
Image Processing
Audio/Video Practices
Workflow
Direct Data Upload
Content Moderation
Data Security
Data Verification
Big Data Practice
COS Cost Optimization Solutions
Using COS in the Third-party Applications
Migration Guide
Migrating Local Data to COS
Migrating Data from Third-Party Cloud Storage Service to COS
Migrating Data from URL to COS
Migrating Data Within COS
Migrating Data Between HDFS and COS
Data Lake Storage
Cloud Native Datalake Storage
Metadata Accelerator
GooseFS
Data Processing
Data Processing Overview
Image Processing
Media Processing
Content Moderation
File Processing Service
File Preview
Troubleshooting
Obtaining RequestId
Slow Upload over Public Network
403 Error for COS Access
Resource Access Error
POST Object Common Exceptions
API Documentation
Introduction
Common Request Headers
Common Response Headers
Error Codes
Request Signature
Action List
Service APIs
Bucket APIs
Object APIs
Batch Operation APIs
Data Processing APIs
Job and Workflow
Content Moderation APIs
Cloud Antivirus API
SDK Documentation
SDK Overview
Preparations
Android SDK
C SDK
C++ SDK
.NET(C#) SDK
Flutter SDK
Go SDK
iOS SDK
Java SDK
JavaScript SDK
Node.js SDK
PHP SDK
Python SDK
React Native SDK
Mini Program SDK
Error Codes
Harmony SDK
Endpoint SDK Quality Optimization
Security and Compliance
Data Disaster Recovery
Data Security
Cloud Access Management
FAQs
Popular Questions
General
Billing
Domain Name Compliance Issues
Bucket Configuration
Domain Names and CDN
Object Operations
Logging and Monitoring
Permission Management
Data Processing
Data Security
Pre-signed URL Issues
SDKs
Tools
APIs
Agreements
Service Level Agreement
Privacy Policy
Data Processing And Security Agreement
Contact Us
Glossary

Getting Started

PDF
Mode fokus
Ukuran font
Terakhir diperbarui: 2026-01-27 17:53:47

Relevant Resources

For SDK source code downloads, see: XML .NET SDK.
Quick download link for SDK: XML .NET SDK.
For all sample code in the SDK documentation, see SDK Code Samples.
For the SDK change log, see ChangeLog.
For SDK FAQs, see: .NET (C#) SDK FAQ.
Note:
If you encounter errors such as functions or methods not found while using the SDK, please first upgrade the SDK to the latest version and try again.

Environment Configuration and Preparation

The .NET SDK is developed based on .NET Standard 2.0.
Windows: Install .NET Core 2.0 or later, or .NET Framework 4.5 or later.
Linux/Mac: Install .NET Core 2.0 or later.
You need to obtain a Tencent Cloud API key, which is a prerequisite for your use of the various features of the COS SDK.
Note:
For the meanings and acquisition methods of names, such as SecretId, SecretKey, and Bucket mentioned in this document, see COS Terminology Information.

Installing the SDK

We provide the NuGet integration method. You can add it in your project's csproj file:
<PackageReference Include="Tencent.QCloud.Cos.Sdk" Version="5.4.*" />
If you are using the .NET CLI, install it with the following command:
dotnet add package Tencent.QCloud.Cos.Sdk
If you are developing for target framework .NET Framework version 4.0 and below, download Releases and use the COSXML-Compatible.dll file.
In a Visual Studio project, add the .NET (C#) SDK by selecting Project > Add Reference > Browse > COSXML-Compatible.dll.
Note:
The compatibility package does not include support for advanced features such as upload and download. For details, see Backward Compatibility Guide.

Initialize COS Service

The following section describes how to use the COS .NET SDK to perform basic operations, such as initializing a client, creating a bucket, listing buckets, uploading an object, listing objects, downloading an object, and deleting an object.
Commonly used namespaces in the SDK include:
using COSXML;
using COSXML.Auth;
using COSXML.Model.Object;
using COSXML.Model.Bucket;
using COSXML.CosException;
Before any request related to the COS service is performed, you need to instantiate three objects: 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.

Initialization

Note:
The temporary key used in the initialization example below can be generated and used as described in the Temporary Key Generation and Usage Guidelines.
// 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.

Set API Access Key

SDK provides 3 types: continuously updated temporary keys, unchanged temporary keys, permanent keys.
Note:
It is recommended that users use temporary keys to call the SDK, to further enhance the security of SDK usage through temporary authorization. When applying for temporary keys, please follow the principle of least privilege to prevent exposure of resources beyond the target bucket or object.
If you must use permanent keys, it is recommended to follow the principle of least privilege to restrict the scope of permissions for permanent keys.
Continuously Updated Temporary Keys (Recommended)
Unchanged Temporary Key (Not Recommended)
Permanent Key (Not Recommended)
Since temporary keys expire after a certain validity period, the following method ensures that new temporary keys can be obtained automatically after expiration.
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 Cloud
string tmpSecretId = "SECRET_ID"; // "temporary key SecretId", for the temporary key generation and usage guide, see https://www.tencentcloud.com/document/product/436/14048
string tmpSecretKey = "SECRET_KEY"; // "temporary key SecretKey", for the temporary key generation and usage guide, see https://www.tencentcloud.com/document/product/436/14048
string tmpToken = "COS_TOKEN"; // "temporary key token", for the temporary key generation and usage guide, see https://www.tencentcloud.com/document/product/436/14048
long tmpStartTime = 1546860702;//valid start time of the temporary key, accurate to the second
long tmpExpiredTime = 1546862502;//valid expiration time of the temporary key, accurate to the second
// Call the interface to update the key
SetQCloudCredential(tmpSecretId, tmpSecretKey,
String.Format("{0};{1}", tmpStartTime, tmpExpiredTime), tmpToken);
}
}

QCloudCredentialProvider cosCredentialProvider = new CustomQCloudCredentialProvider();
Note:
Since temporary keys expire after a certain validity period, this method will result in request failure after expiration and is not recommended.
string tmpSecretId = "SECRET_ID"; // "temporary key SecretId", for the temporary key generation and usage guide, see https://www.tencentcloud.com/document/product/436/14048
string tmpSecretKey = "SECRET_KEY"; // "temporary key SecretKey", for the temporary key generation and usage guide, see https://www.tencentcloud.com/document/product/436/14048
string tmpToken = "COS_TOKEN"; // "temporary key token", for the temporary key generation and usage guide, see https://www.tencentcloud.com/document/product/436/14048
long tmpExpireTime = 1546862502;//valid expiration time of the temporary key, accurate to the second
QCloudCredentialProvider 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=1
string 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=1
long durationSecond = 600; //validity duration per request signature, in seconds
QCloudCredentialProvider cosCredentialProvider = new DefaultQCloudCredentialProvider(
secretId, secretKey, durationSecond);

Initialize CosXmlServer

Use 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);

Access COS Service

Create a bucket
Querying the Bucket List
PUT Object
Querying the Object List
Downloading an Object
Deleting Objects
For the complete example of creating a bucket, please go to GitHub.
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/capi
string secretKey = Environment.GetEnvironmentVariable("SECRET_KEY"); // TencentCloud API key SecretKey. To obtain the API key, refer to https://console.tencentcloud.com/cam/capi
long durationSecond = 600; //validity duration per request signature, in seconds
QCloudCredentialProvider 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/developer
string 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 request
PutBucketResult result = cosXml.PutBucket(request);
//Request succeeded
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 CreateBucketModelMain()
{
CreateBucketModel m = new CreateBucketModel();
m.PutBucket();
}
}
}
For the complete example of querying the bucket list, please go to GitHub to view.
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/capi
string secretKey = Environment.GetEnvironmentVariable("SECRET_KEY"); // TencentCloud API key SecretKey. To obtain the API key, refer to https://console.tencentcloud.com/cam/capi
long durationSecond = 600; //validity duration per request signature, in seconds
QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond);
this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);
}
ListBucketModel()
{
InitCosXml();
}
public void GetService()
{
try
{
GetServiceRequest request = new GetServiceRequest();
//Execute the request
GetServiceResult result = cosXml.GetService(request);
// Get all buckets
List<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();
}
}
}
For the complete example of uploading objects, please go to GitHub to view.
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/developer
private 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/capi
string secretKey = Environment.GetEnvironmentVariable("SECRET_KEY"); // TencentCloud API key SecretKey. To obtain the API key, refer to https://console.tencentcloud.com/cam/capi
long durationSecond = 600; //validity duration per request signature, in seconds
QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond);
this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);
}
UploadObject()
{
// Custom parameters of the demo
InitParams();
// Initialize the COS service
InitCosXml();
}

public void PutObject()
{
try {
// Bucket name. The format must be bucketname-APPID, where for obtaining the APPID, refer to https://console.tencentcloud.com/developer
string bucket = "examplebucket-1250000000";
string key = "exampleobject"; // Object key
string srcPath = @"temp-source-file"; // Absolute path of the local file

PutObjectRequest 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 result
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 UploadObjectMain()
{
UploadObject domo = new UploadObject();
// Upload a file.
domo.PutObject();
}
}
}
For the complete example of querying object lists, please go to GitHub to view.
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/capi
string secretKey = Environment.GetEnvironmentVariable("SECRET_KEY"); // TencentCloud API key SecretKey. To obtain the API key, refer to https://console.tencentcloud.com/cam/capi
long durationSecond = 600; //validity duration per request signature, in seconds
QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond);
this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);
}

ListObjectModel()
{
InitCosXml();
InitParams();
}

// Obtain the first page data of the object versions list
public void ListObjectsVersioning()
{
try
{
// Bucket name. The format must be bucketname-APPID, where APPID can be obtained at https://console.tencentcloud.com/developer
string bucket = "examplebucket-version-1250000000";
ListBucketVersionsRequest request = new ListBucketVersionsRequest(bucket);
//Execute the request
ListBucketVersionsResult result = cosXml.ListBucketVersions(request);
//information about the bucket
ListBucketVersions info = result.listBucketVersions;
//Request result status
Console.WriteLine(result.GetResultInfo());
List<ListBucketVersions.Version> objects = info.objectVersionList;
List<ListBucketVersions.CommonPrefixes> prefixes = info.commonPrefixesList;
//returned information
Console.WriteLine(info);
if (info.isTruncated)
{
// Data is truncated; record the data index
this.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 list
public void ListObjectsVersioningNextPage()
{
try
{
// Bucket name. The format must be bucketname-APPID, where APPID can be obtained at https://console.tencentcloud.com/developer
string bucket = "examplebucket-version-1250000000";
ListBucketVersionsRequest request = new ListBucketVersionsRequest(bucket);
// The ending index of the previous page's data
request.SetKeyMarker(this.keyMarker);
request.SetVersionIdMarker(this.versionIdMarker);
//Execute the request
ListBucketVersionsResult result = cosXml.ListBucketVersions(request);
//Request result status
Console.WriteLine(result.GetResultInfo());
ListBucketVersions info = result.listBucketVersions;
Console.WriteLine(info.GetInfo());
if (info.isTruncated)
{
// Data is truncated; record the data index
this.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 list
public void GetBucketFirstPage()
{
try
{
// Bucket name. The format must be bucketname-APPID, where APPID can be obtained at https://console.tencentcloud.com/developer
string bucket = "examplebucket-1250000000";
GetBucketRequest request = new GetBucketRequest(bucket);
//Execute the request
GetBucketResult result = cosXml.GetBucket(request);
//information about the bucket
COSXML.Model.Tag.ListBucket info = result.listBucket;
if (info.isTruncated)
{
// Data is truncated; record the data index
this.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 list
public void GetBucketNextPage()
{
try
{
// Bucket name. The format must be bucketname-APPID, where APPID can be obtained at https://console.tencentcloud.com/developer
string bucket = "examplebucket-1250000000";
GetBucketRequest request = new GetBucketRequest(bucket);
// Index of the last data pull
request.SetMarker(this.nextMarker);
//Execute the request
GetBucketResult result = cosXml.GetBucket(request);
//information about the bucket
COSXML.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 subdirectories
public void GetBucketWithDelimiter()
{
try
{
// Bucket name. The format must be bucketname-APPID, where APPID can be obtained at https://console.tencentcloud.com/developer
string bucket = "examplebucket-1250000000";
GetBucketRequest request = new GetBucketRequest(bucket);
//Obtain objects under a/ and subdirectories
request.SetPrefix("dir/");
request.SetDelimiter("/");
//Execute the request
GetBucketResult result = cosXml.GetBucket(request);
//information about the bucket
COSXML.Model.Tag.ListBucket info = result.listBucket;
// Object list
List<COSXML.Model.Tag.ListBucket.Contents> objects = info.contentsList;
// Subdirectory list
List<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();
}

}
}
For the complete example of downloading objects, please go to GitHub to view.
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/capi
string secretKey = Environment.GetEnvironmentVariable("SECRET_KEY"); // TencentCloud API key SecretKey. To obtain the API key, refer to https://console.tencentcloud.com/cam/capi
long durationSecond = 600; //validity duration per request signature, in seconds
QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond);
this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);
}
DownloadObject()
{
// Custom parameters of the demo
InitParams();
// Initialize the COS service
InitCosXml();
}

public void GetObject()
{
try
{
// Bucket name. The format must be bucketname-APPID, where APPID can be obtained at https://console.tencentcloud.com/developer
string bucket = "examplebucket-1250000000";
string key = "exampleobject"; // object key
string localDir = Path.GetTempPath();//local folder
string localFileName = "my-local-temp-file"; //Specify the locally saved file name
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));
});
//Execute the request
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());
}
}
public static void DownloadObjectMain()
{
DownloadObject demo = new DownloadObject();
demo.GetObject();
}
}
}
Note:
Once an object is deleted, its corresponding data will no longer be accessible.
For the complete sample on deleting objects, please go to GitHub to view.
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/capi
string secretKey = Environment.GetEnvironmentVariable("SECRET_KEY"); // TencentCloud API key SecretKey. To obtain the API key, refer to https://console.tencentcloud.com/cam/capi
long durationSecond = 600; //validity duration per request signature, in seconds
QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond);
this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);
}

DeleteObjectModel()
{
InitCosXml();
InitParams();
}

// Delete the object
public void DeleteObject()
{
try
{
// Bucket name. The format must be bucketname-APPID, where APPID can be obtained at https://console.tencentcloud.com/developer
string bucket = "examplebucket-1250000000";
string key = "exampleobject"; // object key
DeleteObjectRequest request = new DeleteObjectRequest(bucket, key);
//Execute the request
DeleteObjectResult result = cosXml.DeleteObject(request);
//Request succeeded
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 DeleteObjectModelMain()
{
DeleteObjectModel m = new DeleteObjectModel();
// Delete the object
m.DeleteObject();
}
}
}

FAQs

You may encounter some common issues during usage. For solutions, refer to .NET SDK FAQ.

Bantuan dan Dukungan

Apakah halaman ini membantu?

masukan