tencent cloud

Cloud Object Storage

最新情報とお知らせ
製品アップデート情報
製品のお知らせ
製品概要
製品概要
機能概要
応用シナリオ
製品の優位性
基本概念
リージョンとアクセスドメイン名
仕様と制限
製品の課金
課金概要
課金方式
課金項目
無料利用枠
記帳例
請求書の確認とダウンロード
お支払い遅れについて
よくある質問
クイックスタート
コンソールクイックスタート
COSBrowserクイックスタート
ユーザーガイド
リクエストの作成
バケット
オブジェクト
データ管理
バッチ処理
グローバルアクセラレーション
監視とアラーム
運用管理センター
データ処理
インテリジェントツールボックス使用ガイド
データワークフロー
アプリ統合
ツールガイド
ツール概要
環境のインストールと設定
COSBrowserツール
COSCLIツール
COSCMDツール
COS Migrationツール
FTP Serverツール
Hadoopツール
COSDistCpツール
HDFS TO COSツール
オンラインツール (Onrain Tsūru)
セルフ診断ツール
実践チュートリアル
概要
アクセス制御と権限管理
パフォーマンスの最適化
AWS S3 SDKを使用したCOSアクセス
データディザスタリカバリバックアップ
ドメイン名管理の実践
画像処理の実践
COSオーディオビデオプレーヤーの実践
データセキュリティ
データ検証
COSコスト最適化ソリューション
サードパーティアプリケーションでのCOSの使用
移行ガイド
サードパーティクラウドストレージのデータをCOSへ移行
データレークストレージ
クラウドネイティブデータレイク
メタデータアクセラレーション
データアクセラレーター GooseFS
データ処理
データ処理概要
画像処理
メディア処理
コンテンツ審査
ファイル処理
ドキュメントプレビュー
トラブルシューティング
RequestId取得の操作ガイド
パブリックネットワーク経由でのCOSへのファイルアップロード速度の遅さ
COSへのアクセス時に403エラーコードが返される
リソースアクセス異常
POST Objectの一般的な異常
セキュリティとコンプライアンス
データ災害復帰
データセキュリティ
クラウドアクセスマネジメント
よくある質問
よくあるご質問
一般的な問題
従量課金に関するご質問
ドメインコンプライアンスに関するご質問
バケット設定に関する質問
ドメイン名とCDNに関するご質問
ファイル操作に関するご質問
権限管理に関するご質問
データ処理に関するご質問
データセキュリティに関するご質問
署名付きURLに関するご質問
SDKクラスに関するご質問
ツール類に関するご質問
APIクラスに関するご質問
Agreements
Service Level Agreement
プライバシーポリシー
データ処理とセキュリティ契約
連絡先
用語集
ドキュメントCloud Object Storage

Getting Started

フォーカスモード
フォントサイズ
最終更新日: 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.

ヘルプとサポート

この記事はお役に立ちましたか?

フィードバック