Resources
Download the XML .NET SDK source code here. Download XML .NET SDK here. Find the complete code here. Note:
If you encounter errors such as non-existent functions or methods when using the SDK, you can update the SDK to the latest version and try again.
Step 1. Integrate the SDK
Environmental dependencies
The .NET SDK is developed based on .NET Standard 2.0.
Windows: Install .NET Core 2.0 or above or .NET Framework 2.0 or above.
For Linux/Mac users: Install .NET Core 2.0 or above.
Installing the SDK
You can install the SDK using NuGet by adding the following to the csproj
file in your project:
<PackageReference Include="Tencent.QCloud.Cos.Sdk" Version="5.4.*" />
If .NET CLI is used instead, run the following command:
dotnet add package Tencent.QCloud.Cos.Sdk
If you develop for the target framework .Net Framework 4.0 or below, please download Releases and use COSXML-Compatible.dll
. In your Visual Studio project, click Project > Add Reference > Browse > COSXML-Compatible.dll to add the .NET(C#) SDK.
Note:
The compatible package does not support features such as advanced upload and download. For details, see Backward Compatibility. Step 2. Initialize COS Services
The section below describes how to perform basic COS operations with the .NET SDK, such as initializing a client, creating a bucket, querying a bucket list, uploading an object, querying an object list, downloading an object, and deleting an object.
Note:
For the definition of terms such as SecretId, SecretKey, and Bucket, see COS Glossary. Namespaces commonly used in this SDK include:
using COSXML;
using COSXML.Auth;
using COSXML.Model.Object;
using COSXML.Model.Bucket;
using COSXML.CosException;
Before making any COS requests, always instantiate the following 3 objects: CosXmlConfig
, QCloudCredentialProvider
, and CosXmlServer
.
CosXmlConfig
provides an API to configure SDK.
QCloudCredentialProvider
provides an API to set the key information.
CosXmlServer
provides APIs to perform operations on COS API services.
1. Initialize a COS service
string appid = "1250000000";
string region = "COS_REGION";
CosXmlConfig config = new CosXmlConfig.Builder()
.IsHttps(true)
.SetRegion(region)
.SetDebugLog(true)
.Build();
2. Provide access credentials
The SDK supports three types of access credentials: updated temporary keys, unchanging temporary keys, and permanent keys.
Type 1: updated temporary key
Since temporary keys are short term, you can obtain new ones using the following method:
public class CustomQCloudCredentialProvider : DefaultSessionQCloudCredentialProvider
{
public CustomQCloudCredentialProvider(): base(null, null, 0L, null) {
;
}
public override void Refresh()
{
string tmpSecretId = "SECRET_ID";
string tmpSecretKey = "SECRET_KEY";
string tmpToken = "COS_TOKEN";
long tmpStartTime = 1546860702;
long tmpExpireTime = 1546862502;
SetQCloudCredential(tmpSecretId, tmpSecretKey,
String.Format("{0};{1}", tmpStartTime, tmpExpiredTime), tmpToken);
}
}
QCloudCredentialProvider cosCredentialProvider = new CustomQCloudCredentialProvider();
Type 2: unchanging temporary key (not recommended)
Caution:
This method is not recommended as a request may fail if the temporary key has expired at the time of request.
string tmpSecretId = "SECRET_ID";
string tmpSecretKey = "SECRET_KEY";
string tmpToken = "COS_TOKEN";
long tmpExpireTime = 1546862502;
QCloudCredentialProvider cosCredentialProvider = new DefaultSessionQCloudCredentialProvider(
tmpSecretId, tmpSecretKey, tmpExpireTime, tmpToken);
Type 3: permanent key (not recommended)
string secretId = Environment.GetEnvironmentVariable("SECRET_ID");
string secretKey = Environment.GetEnvironmentVariable("SECRET_KEY");
long durationSecond = 600;
QCloudCredentialProvider cosCredentialProvider = new DefaultQCloudCredentialProvider(
secretId, secretKey, durationSecond);
3. Initialize CosXmlServer
Use CosXmlConfig
and QCloudCredentialProvider
to initialize the CosXmlServer
service class. We recommend you use the service class as a singleton in your project.
CosXml cosXml = new CosXmlServer(config, cosCredentialProvider);
Step 3. Access COS
Creating a bucket
try
{
String bucket = "examplebucket-1250000000";
PutBucketRequest request = new PutBucketRequest(bucket);
PutBucketResult result = cosXml.PutBucket(request);
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
Querying the bucket list
try
{
GetServiceRequest request = new GetServiceRequest();
GetServiceResult result = cosXml.GetService(request);
List<ListAllMyBuckets.Bucket> allBuckets = result.listAllMyBuckets.buckets;
}
catch (COSXML.CosException.CosClientException clientEx)
{
Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
Uploading an object
TransferConfig transferConfig = new TransferConfig();
TransferManager transferManager = new TransferManager(cosXml, transferConfig);
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject";
String srcPath = @"temp-source-file";
COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, cosPath);
uploadTask.SetSrcPath(srcPath);
uploadTask.progressCallback = delegate (long completed, long total)
{
Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
};
try {
COSXML.Transfer.COSXMLUploadTask.UploadTaskResult result = await
transferManager.UploadAsync(uploadTask);
Console.WriteLine(result.GetResultInfo());
string eTag = result.eTag;
} catch (Exception e){
Console.WriteLine("CosException: " + e);
}
Querying objects
try
{
String bucket = "examplebucket-1250000000";
GetBucketRequest request = new GetBucketRequest(bucket);
GetBucketResult result = cosXml.GetBucket(request);
ListBucket info = result.listBucket;
if (info.isTruncated) {
this.nextMarker = info.nextMarker;
}
}
catch (COSXML.CosException.CosClientException clientEx)
{
Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
Downloading an object
TransferConfig transferConfig = new TransferConfig();
TransferManager transferManager = new TransferManager(cosXml, transferConfig);
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject";
string localDir = System.IO.Path.GetTempPath();
string localFileName = "my-local-temp-file";
COSXMLDownloadTask downloadTask = new COSXMLDownloadTask(bucket, cosPath,
localDir, localFileName);
downloadTask.progressCallback = delegate (long completed, long total)
{
Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
};
try {
COSXML.Transfer.COSXMLDownloadTask.DownloadTaskResult result = await
transferManager.DownloadAsync(downloadTask);
Console.WriteLine(result.GetResultInfo());
string eTag = result.eTag;
} catch (Exception e){
Console.WriteLine("CosException: " + e);
}
Deleting an object
try
{
string bucket = "examplebucket-1250000000";
string key = "exampleobject";
DeleteObjectRequest request = new DeleteObjectRequest(bucket, key);
DeleteObjectResult result = cosXml.DeleteObject(request);
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
Apakah halaman ini membantu?