API | Operation | Description |
Uploading an object | Uploads an object to a bucket. | |
Uploading an object using an HTML form | Uploads an object using an HTML form. | |
Appending parts | Uploads an object by appending the object by parts. |
API | Operation | Description |
Querying multipart uploads | Queries in-progress multipart uploads. | |
Initializing a multipart upload operation | Initializes a multipart upload operation. | |
Uploading parts | Uploads an object in multiple parts. | |
Querying uploaded parts | Queries the uploaded parts of a multipart upload. | |
Completing a multipart upload | Completes the multipart upload of a file. | |
Aborting a multipart upload | Aborts a multipart upload and deletes the uploaded parts. |
using COSXML.Model.Object;using COSXML.Auth;using COSXML.Transfer;using System;using COSXML;using System.Threading.Tasks;namespace COSSnippet{public class TransferUploadObjectModel {private CosXml cosXml;TransferUploadObjectModel() {CosXmlConfig config = new CosXmlConfig.Builder().SetRegion("COS_REGION") // Set the default region. For abbreviations of COS regions, visit https://www.tencentcloud.com/document/product/436/6224..Build();string secretId = "SECRET_ID"; // SecretId of the TencentCloud API. For more information about how to obtain the API key, see https://console.tencentcloud.com/cam/capi.string secretKey = "SECRET_KEY"; // SecretKey of the TencentCloud API. For more information about how to obtain the API key, see https://console.tencentcloud.com/cam/capi.long durationSecond = 600; // Validity period of the request signature in secondsQCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,secretKey, durationSecond);this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);}/// Upload a file via advanced APIpublic async Task TransferUploadFile(){// Initialize TransferConfigTransferConfig transferConfig = new TransferConfig();// Set the object size threshold for multipart upload to 10 MB. Default value: 5 MBtransferConfig.DivisionForUpload = 10 * 1024 * 1024;// Set the size of each object part for multipart upload to 2 MB. Default value: 1 MBtransferConfig.SliceSizeForUpload = 2 * 1024 * 1024;// Initialize TransferManagerTransferManager transferManager = new TransferManager(cosXml, transferConfig);// Bucket name in the format of `BucketName-APPID`. You can get APPID by referring to https://console.tencentcloud.com/developer.String bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; // 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);}}static void Main(string[] args){TransferUploadObjectModel m = new TransferUploadObjectModel();/// Upload an object via advanced APIm.TransferUploadFile().Wait();// .cssg-methods-pragma}}}
using COSXML.Model.Object;using COSXML.Auth;using COSXML.Transfer;using System;using COSXML;namespace COSSnippet{public class TransferUploadObjectModel {private CosXml cosXml;TransferUploadObjectModel() {CosXmlConfig config = new CosXmlConfig.Builder().SetRegion("COS_REGION") // Set the default region. For abbreviations of COS regions, visit https://www.tencentcloud.com/document/product/436/6224..Build();string secretId = "SECRET_ID"; // SecretId of the TencentCloud API. For more information about how to obtain the API key, see https://console.tencentcloud.com/cam/capi.string secretKey = "SECRET_KEY"; // SecretKey of the TencentCloud API. For more information about how to obtain the API key, see https://console.tencentcloud.com/cam/capi.long durationSecond = 600; // Validity period of the request signature in secondsQCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,secretKey, durationSecond);this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);}/// Upload binary datapublic void TransferUploadBytes(){try{// Bucket name in the format of `BucketName-APPID`. You can get APPID by referring to https://console.tencentcloud.com/developer.string bucket = "examplebucket-1250000000";string cosPath = "exampleObject"; // Object keybyte[] data = new byte[1024]; // Binary dataPutObjectRequest putObjectRequest = new PutObjectRequest(bucket, cosPath, data);// Initiate an upload.PutObjectResult result = cosXml.PutObject(putObjectRequest);Console.WriteLine(result.GetResultInfo());}catch (COSXML.CosException.CosClientException clientEx){// Request failedConsole.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){// Request failedConsole.WriteLine("CosServerException: " + serverEx.GetInfo());}}static void Main(string[] args){TransferUploadObjectModel m = new TransferUploadObjectModel();/// Upload binary data via an advanced APIm.TransferUploadBytes();}}}
using COSXML.Model.Object;using COSXML.Utils;using COSXML.Auth;using System;using COSXML;using System.IO;namespace COSSnippet{public class PutObjectModel {private CosXml cosXml;PutObjectModel() {CosXmlConfig config = new CosXmlConfig.Builder().SetRegion("COS_REGION") // Set the default region. For abbreviations of COS regions, visit https://www.tencentcloud.com/document/product/436/6224..Build();string secretId = "SECRET_ID"; // SecretId of the TencentCloud API. For more information about how to obtain the API key, see https://console.tencentcloud.com/cam/capi.string secretKey = "SECRET_KEY"; // SecretKey of the TencentCloud API. For more information about how to obtain the API key, see https://console.tencentcloud.com/cam/capi.long durationSecond = 600; // Validity period of the request signature in secondsQCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,secretKey, durationSecond);this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);}/// Uploading with a file stream, supported from v5.4.24public void PutObjectStream(){try{// Bucket name in the format of `BucketName-APPID`. You can get APPID by referring to https://console.tencentcloud.com/developer.string bucket = "examplebucket-1250000000";string key = "exampleobject"; // Object keystring srcPath = @"temp-source-file";// Absolute path of the local file// Open the read-only file stream objectFileStream fileStream = new FileStream(srcPath, FileMode.Open, FileAccess.Read);// Assemble the upload request, where offset sendLength is optionallong offset = 0L;long sendLength = fileStream.Length;PutObjectRequest request = new PutObjectRequest(bucket, key, fileStream, offset, sendLength);// Set the progress callbackrequest.SetCosProgressCallback(delegate (long completed, long total){Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));});// Execute the requestPutObjectResult result = cosXml.PutObject(request);// Close the file streamfileStream.Close();// Object ETagstring eTag = result.eTag;// crc64ecma value of the objectstring crc64ecma = result.crc64ecma;// Print the request result.Console.WriteLine(result.GetResultInfo());}catch (COSXML.CosException.CosClientException clientEx){// Request failedConsole.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){// Request failedConsole.WriteLine("CosServerException: " + serverEx.GetInfo());}}static void Main(string[] args){PutObjectModel m = new PutObjectModel();/// Upload the object with a file streamm.PutObjectStream();}}}
uploadTask.Pause();
uploadTask.Resume();
uploadTask.Cancel();
TransferConfig transferConfig = new TransferConfig();// Initialize TransferManagerTransferManager transferManager = new TransferManager(cosXml, transferConfig);// Bucket name in the format of `BucketName-APPID`. You can get APPID by referring to https://console.tencentcloud.com/developer.string bucket = "examplebucket-1250000000";for (int i = 0; i < 5; i++) {// Upload an objectstring cosPath = "exampleobject" + i; // Location identifier of an object in the bucket, i.e. the object keystring srcPath = @"temp-source-file";// Absolute path of the local fileCOSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, cosPath);uploadTask.SetSrcPath(srcPath);await transferManager.UploadAsync(uploadTask);}
try{// Bucket name in the format of `BucketName-APPID`. You can get APPID by referring to https://console.tencentcloud.com/developer.string bucket = "examplebucket-1250000000";string cosPath = "dir/"; // Object keyPutObjectRequest putObjectRequest = new PutObjectRequest(bucket, cosPath, new byte[0]);cosXml.PutObject(putObjectRequest);}catch (COSXML.CosException.CosClientException clientEx){// Request failedConsole.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){// Request failedConsole.WriteLine("CosServerException: " + serverEx.GetInfo());}
/
; otherwise, it will be identified as a folder.APPID
) can have up to 1,000 bucket ACLs and an unlimited number of object ACLs. Do not configure ACLs for an object during upload if you don’t need to control access to it. The object will inherit the permissions of its bucket by default.try{// Bucket name in the format of `BucketName-APPID`. You can get APPID by referring to https://console.tencentcloud.com/developer.string bucket = "examplebucket-1250000000";string key = "exampleobject"; // Object keystring srcPath = @"temp-source-file";// Absolute path of the local filePutObjectRequest request = new PutObjectRequest(bucket, key, srcPath);// Set the progress callbackrequest.SetCosProgressCallback(delegate (long completed, long total){Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));});// Execute the requestPutObjectResult result = cosXml.PutObject(request);// Object ETagstring eTag = result.eTag;}catch (COSXML.CosException.CosClientException clientEx){// Request failedConsole.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){// Request failedConsole.WriteLine("CosServerException: " + serverEx.GetInfo());}
try{// Bucket name in the format of `BucketName-APPID`. You can get APPID by referring to https://console.tencentcloud.com/developer.string bucket = "examplebucket-1250000000";string key = "exampleobject"; // Object keystring srcPath = @"temp-source-file";// Absolute path of the local filePostObjectRequest request = new PostObjectRequest(bucket, key, srcPath);// Set the progress callbackrequest.SetCosProgressCallback(delegate (long completed, long total){Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));});// Execute the requestPostObjectResult result = cosXml.PostObject(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{// Bucket name in the format of `BucketName-APPID`. You can get APPID by referring to https://console.tencentcloud.com/developer.string bucket = "examplebucket-1250000000";string key = "exampleobject"; // Object keystring srcPath = @"temp-source-file";// Absolute path of the local file// Append the first part. 0 is passed in for the appending position, and an appendable object is createdlong next_append_position = 0;AppendObjectRequest request = new AppendObjectRequest(bucket, key, srcPath, next_append_position);// Set the progress callbackrequest.SetCosProgressCallback(delegate (long completed, long total){Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));});AppendObjectResult result = cosXml.AppendObject(request);// Get the next appending positionnext_append_position = result.nextAppendPosition;Console.WriteLine(result.GetResultInfo());// Execute appending and pass in the object end obtained last timerequest = new AppendObjectRequest(bucket, key, srcPath, next_append_position);request.SetCosProgressCallback(delegate (long completed, long total){Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));});result = cosXml.AppendObject(request);Console.WriteLine(result.GetResultInfo());}catch (COSXML.CosException.CosClientException clientEx){// Request failedConsole.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){// Request failedConsole.WriteLine("CosServerException: " + serverEx.GetInfo());}
Initiate Multipart Upload
and get the UploadId
.UploadId
to upload the parts with Upload Part - Copy
.Complete Multipart Upload
.UploadId
of the multipart upload, you can query the multipart upload job with List Multipart Uploads
to get the UploadId
of the corresponding file.UploadId
to list the uploaded parts with List Parts
.UploadId
to upload the remaining parts with Upload Part
.Complete Multipart Upload
.UploadId
of the multipart upload, you can query the multipart upload job with List Multipart Uploads
to get the UploadId
of the corresponding file.Abort Multipart Upload
.List Multipart Uploads
) is used to query in-progress multipart uploads in a specified bucket.try{// Bucket name in the format of `BucketName-APPID`. You can get APPID by referring to https://console.tencentcloud.com/developer.string bucket = "examplebucket-1250000000";ListMultiUploadsRequest request = new ListMultiUploadsRequest(bucket);// Execute the requestListMultiUploadsResult result = cosXml.ListMultiUploads(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());}
uploadId
.try{// Bucket name in the format of `BucketName-APPID`. You can get APPID by referring to https://console.tencentcloud.com/developer.string bucket = "examplebucket-1250000000";string key = "exampleobject"; // Object keyInitMultipartUploadRequest request = new InitMultipartUploadRequest(bucket, key);// Execute the requestInitMultipartUploadResult result = cosXml.InitMultipartUpload(request);// Request succeededthis.uploadId = result.initMultipartUpload.uploadId; // The uploadId to use for subsequent multipart uploadsConsole.WriteLine(result.GetResultInfo());}catch (COSXML.CosException.CosClientException clientEx){// Request failedConsole.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){// Request failedConsole.WriteLine("CosServerException: " + serverEx.GetInfo());}
Upload Part
) is used to upload an object in parts.try{// Bucket name in the format of `BucketName-APPID`. You can get APPID by referring to https://console.tencentcloud.com/developer.string bucket = "examplebucket-1250000000";string key = "exampleobject"; // Object keystring uploadId = "exampleUploadId"; // uploadId returned when the multipart upload is initializedint partNumber = 1; // Part number, increases incrementally starting from 1string srcPath = @"temp-source-file";// Absolute path of the local fileUploadPartRequest request = new UploadPartRequest(bucket, key, partNumber,uploadId, srcPath, 0, -1);// Set the progress callbackrequest.SetCosProgressCallback(delegate (long completed, long total){Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));});// Execute the requestUploadPartResult result = cosXml.UploadPart(request);// Request succeeded// Get the ETag of the returned part for subsequent CompleteMultiUploads.this.eTag = result.eTag;Console.WriteLine(result.GetResultInfo());}catch (COSXML.CosException.CosClientException clientEx){// Request failedConsole.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){// Request failedConsole.WriteLine("CosServerException: " + serverEx.GetInfo());}
List Parts
) is used to query the uploaded parts of a multipart upload.try{// Bucket name in the format of `BucketName-APPID`. You can get APPID by referring to https://console.tencentcloud.com/developer.string bucket = "examplebucket-1250000000";string key = "exampleobject"; // Object keystring uploadId = "exampleUploadId"; // uploadId returned when the multipart upload is initializedListPartsRequest request = new ListPartsRequest(bucket, key, uploadId);// Execute the requestListPartsResult result = cosXml.ListParts(request);// Request succeeded// List the parts that have been uploadedList<COSXML.Model.Tag.ListParts.Part> alreadyUploadParts = result.listParts.parts;Console.WriteLine(result.GetResultInfo());}catch (COSXML.CosException.CosClientException clientEx){// Request failedConsole.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){// Request failedConsole.WriteLine("CosServerException: " + serverEx.GetInfo());}
Complete Multipart Upload
) is used to complete the multipart upload of a file.try{// Bucket name in the format of `BucketName-APPID`. You can get APPID by referring to https://console.tencentcloud.com/developer.string bucket = "examplebucket-1250000000";string key = "exampleobject"; // Object keystring uploadId = "exampleUploadId"; // uploadId returned when the multipart upload is initializedCompleteMultipartUploadRequest request = new CompleteMultipartUploadRequest(bucket,key, uploadId);// Concatenate uploaded parts in ascending order by partNumber.request.SetPartNumberAndETag(1, this.eTag);// Execute the requestCompleteMultipartUploadResult result = cosXml.CompleteMultiUpload(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());}
Abort Multipart Upload
) is used to abort a multipart upload and delete the uploaded parts.try{// Bucket name in the format of `BucketName-APPID`. You can get APPID by referring to https://console.tencentcloud.com/developer.string bucket = "examplebucket-1250000000";string key = "exampleobject"; // Object keystring uploadId = "exampleUploadId"; // uploadId returned when the multipart upload is initializedAbortMultipartUploadRequest request = new AbortMultipartUploadRequest(bucket, key, uploadId);// Execute the requestAbortMultipartUploadResult result = cosXml.AbortMultiUpload(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());}
Was this page helpful?