API | 操作名 | 操作描述 |
简单上传对象 | 上传一个对象至存储桶 | |
表单上传对象 | 使用表单请求上传对象 | |
追加上传对象 | 使用分块追加的方式上传对象 |
API | 操作名 | 操作描述 |
查询分块上传 | 查询正在进行中的分块上传信息 | |
初始化分块上传 | 初始化分块上传操作 | |
上传分块 | 分块上传对象 | |
查询已上传块 | 查询特定分块上传操作中的已上传的块 | |
完成分块上传 | 完成整个文件的分块上传 | |
终止分块上传 | 终止一个分块上传操作并删除已上传的块 |
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") // 设置默认的地域, COS 地域的简称请参照 https://www.tencentcloud.com/document/product/436/6224.Build();string secretId = "SECRET_ID"; // 云 API 密钥 SecretId, 获取 API 密钥请参照 https://console.tencentcloud.com/cam/capistring secretKey = "SECRET_KEY"; // 云 API 密钥 SecretKey, 获取 API 密钥请参照 https://console.tencentcloud.com/cam/capilong durationSecond = 600; //每次请求签名有效时长,单位为秒QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,secretKey, durationSecond);this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);}/// 高级接口上传文件public async Task TransferUploadFile(){// 初始化 TransferConfigTransferConfig transferConfig = new TransferConfig();// 手动设置开始分块上传的大小阈值为10MB,默认值为5MBtransferConfig.DivisionForUpload = 10 * 1024 * 1024;// 手动设置分块上传中每个分块的大小为2MB,默认值为1MBtransferConfig.SliceSizeForUpload = 2 * 1024 * 1024;// 初始化 TransferManagerTransferManager transferManager = new TransferManager(cosXml, transferConfig);// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.tencentcloud.com/developerString 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 = 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();/// 高级接口上传对象m.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") // 设置默认的地域, COS 地域的简称请参照 https://www.tencentcloud.com/document/product/436/6224.Build();string secretId = "SECRET_ID"; // 云 API 密钥 SecretId, 获取 API 密钥请参照 https://console.tencentcloud.com/cam/capistring secretKey = "SECRET_KEY"; // 云 API 密钥 SecretKey, 获取 API 密钥请参照 https://console.tencentcloud.com/cam/capilong durationSecond = 600; //每次请求签名有效时长,单位为秒QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,secretKey, durationSecond);this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);}/// 上传二进制数据public void TransferUploadBytes(){try{// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";string cosPath = "exampleObject"; // 对象键byte[] data = new byte[1024]; // 二进制数据PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, cosPath, data);// 发起上传PutObjectResult result = cosXml.PutObject(putObjectRequest);Console.WriteLine(result.GetResultInfo());}catch (COSXML.CosException.CosClientException clientEx){//请求失败Console.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){//请求失败Console.WriteLine("CosServerException: " + serverEx.GetInfo());}}static void Main(string[] args){TransferUploadObjectModel m = new TransferUploadObjectModel();/// 高级接口上传二进制数据m.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") // 设置默认的地域, COS 地域的简称请参照 https://www.tencentcloud.com/document/product/436/6224.Build();string secretId = "SECRET_ID"; // 云 API 密钥 SecretId, 获取 API 密钥请参照 https://console.tencentcloud.com/cam/capistring secretKey = "SECRET_KEY"; // 云 API 密钥 SecretKey, 获取 API 密钥请参照 https://console.tencentcloud.com/cam/capilong durationSecond = 600; //每次请求签名有效时长,单位为秒QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,secretKey, durationSecond);this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);}/// 文件流上传, 从 5.4.24 版本开始支持public void PutObjectStream(){try{// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";string key = "exampleobject"; //对象键string srcPath = @"temp-source-file";//本地文件绝对路径// 打开只读的文件流对象FileStream fileStream = new FileStream(srcPath, FileMode.Open, FileAccess.Read);// 组装上传请求,其中 offset sendLength 为可选参数long offset = 0L;long sendLength = fileStream.Length;PutObjectRequest request = new PutObjectRequest(bucket, key, fileStream, offset, sendLength);//设置进度回调request.SetCosProgressCallback(delegate (long completed, long total){Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));});//执行请求PutObjectResult result = cosXml.PutObject(request);//关闭文件流fileStream.Close();//对象的 eTagstring eTag = result.eTag;//对象的 crc64ecma 校验值string crc64ecma = result.crc64ecma;//打印请求结果Console.WriteLine(result.GetResultInfo());}catch (COSXML.CosException.CosClientException clientEx){//请求失败Console.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){//请求失败Console.WriteLine("CosServerException: " + serverEx.GetInfo());}}static void Main(string[] args){PutObjectModel m = new PutObjectModel();/// 从文件流上传对象m.PutObjectStream();}}}
uploadTask.Pause();
uploadTask.Resume();
uploadTask.Cancel();
TransferConfig transferConfig = new TransferConfig();// 初始化 TransferManagerTransferManager transferManager = new TransferManager(cosXml, transferConfig);// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";for (int i = 0; i < 5; i++) {// 上传对象string cosPath = "exampleobject" + i; //对象在存储桶中的位置标识符,即称对象键string srcPath = @"temp-source-file";//本地文件绝对路径COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, cosPath);uploadTask.SetSrcPath(srcPath);await transferManager.UploadAsync(uploadTask);}
try{// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";string cosPath = "dir/"; // 对象键PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, cosPath, new byte[0]);cosXml.PutObject(putObjectRequest);}catch (COSXML.CosException.CosClientException clientEx){//请求失败Console.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){//请求失败Console.WriteLine("CosServerException: " + serverEx.GetInfo());}
/
结尾,否则会被识别为文件夹。try{// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";string key = "exampleobject"; //对象键string srcPath = @"temp-source-file";//本地文件绝对路径PutObjectRequest request = new PutObjectRequest(bucket, key, srcPath);//设置进度回调request.SetCosProgressCallback(delegate (long completed, long total){Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));});//执行请求PutObjectResult result = cosXml.PutObject(request);//对象的 eTagstring eTag = result.eTag;}catch (COSXML.CosException.CosClientException clientEx){//请求失败Console.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){//请求失败Console.WriteLine("CosServerException: " + serverEx.GetInfo());}
try{// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";string key = "exampleobject"; //对象键string srcPath = @"temp-source-file";//本地文件绝对路径PostObjectRequest request = new PostObjectRequest(bucket, key, srcPath);//设置进度回调request.SetCosProgressCallback(delegate (long completed, long total){Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));});//执行请求PostObjectResult result = cosXml.PostObject(request);//请求成功Console.WriteLine(result.GetResultInfo());}catch (COSXML.CosException.CosClientException clientEx){//请求失败Console.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){//请求失败Console.WriteLine("CosServerException: " + serverEx.GetInfo());}
try{// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";string key = "exampleobject"; //对象键string srcPath = @"temp-source-file";//本地文件绝对路径//首次append上传,追加位置传0,创建一个appendable对象long next_append_position = 0;AppendObjectRequest request = 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));});AppendObjectResult result = cosXml.AppendObject(request);//获取下次追加位置next_append_position = result.nextAppendPosition;Console.WriteLine(result.GetResultInfo());//执行追加,传入上次获取的对象末尾request = 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){//请求失败Console.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){//请求失败Console.WriteLine("CosServerException: " + serverEx.GetInfo());}
try{// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";ListMultiUploadsRequest request = new ListMultiUploadsRequest(bucket);//执行请求ListMultiUploadsResult result = cosXml.ListMultiUploads(request);//请求成功Console.WriteLine(result.GetResultInfo());}catch (COSXML.CosException.CosClientException clientEx){//请求失败Console.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){//请求失败Console.WriteLine("CosServerException: " + serverEx.GetInfo());}
try{// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";string key = "exampleobject"; //对象键InitMultipartUploadRequest request = new InitMultipartUploadRequest(bucket, key);//执行请求InitMultipartUploadResult result = cosXml.InitMultipartUpload(request);//请求成功this.uploadId = result.initMultipartUpload.uploadId; //用于后续分块上传的 uploadIdConsole.WriteLine(result.GetResultInfo());}catch (COSXML.CosException.CosClientException clientEx){//请求失败Console.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){//请求失败Console.WriteLine("CosServerException: " + serverEx.GetInfo());}
try{// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";string key = "exampleobject"; //对象键string uploadId = "exampleUploadId"; //初始化分块上传返回的uploadIdint partNumber = 1; //分块编号,必须从1开始递增string srcPath = @"temp-source-file";//本地文件绝对路径UploadPartRequest request = new UploadPartRequest(bucket, key, partNumber,uploadId, srcPath, 0, -1);//设置进度回调request.SetCosProgressCallback(delegate (long completed, long total){Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));});//执行请求UploadPartResult result = cosXml.UploadPart(request);//请求成功//获取返回分块的eTag,用于后续CompleteMultiUploadsthis.eTag = result.eTag;Console.WriteLine(result.GetResultInfo());}catch (COSXML.CosException.CosClientException clientEx){//请求失败Console.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){//请求失败Console.WriteLine("CosServerException: " + serverEx.GetInfo());}
try{// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";string key = "exampleobject"; //对象键string uploadId = "exampleUploadId"; //初始化分块上传返回的uploadIdListPartsRequest request = new ListPartsRequest(bucket, key, uploadId);//执行请求ListPartsResult result = cosXml.ListParts(request);//请求成功//列举已上传的分块List<COSXML.Model.Tag.ListParts.Part> alreadyUploadParts = result.listParts.parts;Console.WriteLine(result.GetResultInfo());}catch (COSXML.CosException.CosClientException clientEx){//请求失败Console.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){//请求失败Console.WriteLine("CosServerException: " + serverEx.GetInfo());}
try{// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";string key = "exampleobject"; //对象键string uploadId = "exampleUploadId"; //初始化分块上传返回的uploadIdCompleteMultipartUploadRequest request = new CompleteMultipartUploadRequest(bucket,key, uploadId);//设置已上传的parts,必须有序,按照partNumber递增request.SetPartNumberAndETag(1, this.eTag);//执行请求CompleteMultipartUploadResult result = cosXml.CompleteMultiUpload(request);//请求成功Console.WriteLine(result.GetResultInfo());}catch (COSXML.CosException.CosClientException clientEx){//请求失败Console.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){//请求失败Console.WriteLine("CosServerException: " + serverEx.GetInfo());}
try{// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";string key = "exampleobject"; //对象键string uploadId = "exampleUploadId"; //初始化分块上传返回的uploadIdAbortMultipartUploadRequest request = new AbortMultipartUploadRequest(bucket, key, uploadId);//执行请求AbortMultipartUploadResult result = cosXml.AbortMultiUpload(request);//请求成功Console.WriteLine(result.GetResultInfo());}catch (COSXML.CosException.CosClientException clientEx){//请求失败Console.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){//请求失败Console.WriteLine("CosServerException: " + serverEx.GetInfo());}
本页内容是否解决了您的问题?