csproj file in your project:<PackageReference Include="Tencent.QCloud.Cos.Sdk" Version="5.4.*" />
dotnet add package Tencent.QCloud.Cos.Sdk
COSXML-Compatible.dll.using COSXML;using COSXML.Auth;using COSXML.Model.Object;using COSXML.Model.Bucket;using COSXML.CosException;
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.// Initialize CosXmlConfig.string appid = "1250000000";// Set the APPID of your Tencent Cloud account.string region = "COS_REGION"; // Set the default bucket region.CosXmlConfig config = new CosXmlConfig.Builder().IsHttps(true) // Set HTTPS as default request method..SetRegion(region) // Set the default bucket region..SetDebugLog(true) // Display logs..Build(); // Create a CosXmlConfig object.
public class CustomQCloudCredentialProvider : DefaultSessionQCloudCredentialProvider{// Even if you do not already have a current temporary key, you can still use this method.public CustomQCloudCredentialProvider(): base(null, null, 0L, null) {;}public override void Refresh(){//... First, request a temporary key from Tencent Cloud.string tmpSecretId = "SECRET_ID"; // SecretId of a temporary key. For more information about how to generate and use a temporary key, visit https://www.tencentcloud.com/document/product/436/14048.string tmpSecretKey = "SECRET_KEY"; // SecretKey of a temporary key. For more information about how to generate and use a temporary key, visit https://www.tencentcloud.com/document/product/436/14048.string tmpToken = "COS_TOKEN"; // Token of a temporary key. For more information about how to generate and use a temporary key, visit https://www.tencentcloud.com/document/product/436/14048.long tmpStartTime = 1546860702;// Start time in seconds of the temporary key’s validity periodlong tmpExpireTime = 1546862502;// End time in seconds of the temporary key’s validity period// Call the API to update the keySetQCloudCredential(tmpSecretId, tmpSecretKey,String.Format("{0};{1}", tmpStartTime, tmpExpiredTime), tmpToken);}}QCloudCredentialProvider cosCredentialProvider = new CustomQCloudCredentialProvider();
string tmpSecretId = "SECRET_ID"; // “SecretId of the temporary key”;string tmpSecretKey = "SECRET_KEY"; // “SecretKey of the temporary key”;string tmpToken = "COS_TOKEN"; // “Token of the temporary key”;long tmpExpireTime = 1546862502;// End time in seconds of the temporary key’s validity periodQCloudCredentialProvider cosCredentialProvider = new DefaultSessionQCloudCredentialProvider(tmpSecretId, tmpSecretKey, tmpExpireTime, tmpToken);
string secretId = Environment.GetEnvironmentVariable("SECRET_ID"); // User `SecretId`. We recommend you use a sub-account key and follow the principle of least privilege to reduce risks. For information about how to obtain a sub-account key, visit https://www.tencentcloud.com/document/product/598/32675.string secretKey = Environment.GetEnvironmentVariable("SECRET_KEY"); // User `SecretKey`. We recommend you use a sub-account key and follow the principle of least privilege to reduce risks. For information about how to obtain a sub-account key, visit https://www.tencentcloud.com/document/product/598/32675.long durationSecond = 600; // Validity period of each request signature in secondsQCloudCredentialProvider cosCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond);
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);
try{String bucket = "examplebucket-1250000000"; // Format: BucketName-APPIDPutBucketRequest request = new PutBucketRequest(bucket);// Execute the requestPutBucketResult result = cosXml.PutBucket(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());}
try{GetServiceRequest request = new GetServiceRequest();// Execute the requestGetServiceResult result = cosXml.GetService(request);// Get the list of all buckets.List<ListAllMyBuckets.Bucket> allBuckets = result.listAllMyBuckets.buckets;}catch (COSXML.CosException.CosClientException clientEx){// Request failedConsole.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){// Request failedConsole.WriteLine("CosServerException: " + serverEx.GetInfo());}
// Initialize TransferConfigTransferConfig transferConfig = new TransferConfig();// Initialize TransferManagerTransferManager transferManager = new TransferManager(cosXml, transferConfig);String bucket = "examplebucket-1250000000"; // Bucket name in the format of BucketName-APPIDString cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e., the object keyString srcPath = @"temp-source-file";// Absolute path to the local file// Upload an objectCOSXMLUploadTask 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 = awaittransferManager.UploadAsync(uploadTask);Console.WriteLine(result.GetResultInfo());string eTag = result.eTag;} catch (Exception e){Console.WriteLine("CosException: " + e);}
try{String bucket = "examplebucket-1250000000"; // Format: BucketName-APPIDGetBucketRequest request = new GetBucketRequest(bucket);// Execute the requestGetBucketResult result = cosXml.GetBucket(request);// Bucket informationListBucket info = result.listBucket;if (info.isTruncated) {// The data is truncated, and the next marker of the data is recorded.this.nextMarker = info.nextMarker;}}catch (COSXML.CosException.CosClientException clientEx){// Request failedConsole.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){// Request failedConsole.WriteLine("CosServerException: " + serverEx.GetInfo());}
// Initialize TransferConfigTransferConfig transferConfig = new TransferConfig();// Initialize TransferManagerTransferManager transferManager = new TransferManager(cosXml, transferConfig);String bucket = "examplebucket-1250000000"; // Bucket name in the format of BucketName-APPIDString cosPath = "exampleobject"; // The 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);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 (Exception e){Console.WriteLine("CosException: " + e);}
try{string bucket = "examplebucket-1250000000"; // Bucket name in the format of BucketName-APPIDstring 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){// Request failedConsole.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){// Request failedConsole.WriteLine("CosServerException: " + serverEx.GetInfo());}
피드백