API | 操作名 | 操作描述 |
设置对象复制(修改对象属性) | 复制文件到目标路径 | |
删除单个对象 | 在存储桶中删除指定对象 |
API | 操作名 | 操作描述 |
查询分块 | 查询正在进行中的分块信息 | |
初始化分块 | 初始化分块操作 | |
复制分块 | 将其他对象复制为一个分块 | |
查询已上传/复制块 | 查询特定分块操作中的已复制/上传的块 | |
完成分块上传/复制 | 完成整个文件的分块上传/复制 | |
终止分块上传/复制 | 终止一个分块操作并删除已上传/复制的块 |
using COSXML.Model.Tag;using COSXML.Auth;using COSXML.Transfer;using System;using COSXML;using System.Threading.Tasks;namespace COSSnippet{public class TransferCopyObjectModel {private CosXml cosXml;TransferCopyObjectModel() {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 TransferCopyObject(){TransferConfig transferConfig = new TransferConfig();//手动设置分块复制阈值,小于阈值的对象使用简单复制,大于阈值的对象使用分块复制,不设定则默认为5MBtransferConfig.DdivisionForCopy = 5242880;//手动设置高级接口的自动分块大小,不设定则默认为2MBtransferConfig.SliceSizeForCopy = 2097152;// 初始化 TransferManagerTransferManager transferManager = new TransferManager(cosXml, transferConfig);string sourceAppid = "1250000000"; //账号 appidstring sourceBucket = "sourcebucket-1250000000"; //"源对象所在的存储桶string sourceRegion = "COS_REGION"; //源对象的存储桶所在的地域string sourceKey = "sourceObject"; //源对象键//构造源对象属性CopySourceStruct copySource = new CopySourceStruct(sourceAppid, sourceBucket,sourceRegion, sourceKey);string bucket = "examplebucket-1250000000"; //目标存储桶,格式:BucketName-APPIDstring key = "exampleobject"; //目标对象的对象键COSXMLCopyTask copytask = new COSXMLCopyTask(bucket, key, copySource);try {COSXML.Transfer.COSXMLCopyTask.CopyTaskResult result = awaittransferManager.CopyAsync(copytask);Console.WriteLine(result.GetResultInfo());string eTag = result.eTag;}catch (COSXML.CosException.CosClientException clientEx){//请求失败Console.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){//请求失败Console.WriteLine("CosServerException: " + serverEx.GetInfo());}}static void Main(string[] args){TransferCopyObjectModel m = new TransferCopyObjectModel();/// 高级接口拷贝对象m.TransferCopyObject().Wait();}}}
try{string sourceAppid = "1250000000"; //账号 appidstring sourceBucket = "sourcebucket-1250000000"; //"源对象所在的存储桶string sourceRegion = "COS_REGION"; //源对象的存储桶所在的地域string sourceKey = "sourceObject"; //源对象键//构造源对象属性CopySourceStruct copySource = new CopySourceStruct(sourceAppid, sourceBucket,sourceRegion, sourceKey);// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";string key = "exampleobject"; //对象键CopyObjectRequest request = new CopyObjectRequest(bucket, key);//设置拷贝源request.SetCopySource(copySource);//设置是否拷贝还是更新,此处是拷贝request.SetCopyMetaDataDirective(COSXML.Common.CosMetaDataDirective.Copy);//执行请求CopyObjectResult result = cosXml.CopyObject(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 appId = "1250000000"; //账号 appidstring region = "COS_REGION"; //源对象的存储桶所在的地域//构造对象属性CopySourceStruct copySource = new CopySourceStruct(appId, bucket,region, key);CopyObjectRequest request = new CopyObjectRequest(bucket, key);//设置拷贝源request.SetCopySource(copySource);//设置是否拷贝还是更新,此处是拷贝request.SetCopyMetaDataDirective(COSXML.Common.CosMetaDataDirective.Replaced);// 修改为归档存储request.SetCosStorageClass("ARCHIVE");//执行请求CopyObjectResult result = cosXml.CopyObject(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{string sourceAppid = "1250000000"; //账号 appidstring sourceBucket = "sourcebucket-1250000000"; //"源对象所在的存储桶string sourceRegion = "COS_REGION"; //源对象的存储桶所在的地域string sourceKey = "sourceObject"; //源对象键//构造源对象属性CopySourceStruct copySource = new CopySourceStruct(sourceAppid, sourceBucket,sourceRegion, sourceKey);// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";string key = "exampleobject"; //对象键CopyObjectRequest request = new CopyObjectRequest(bucket, key);//设置拷贝源request.SetCopySource(copySource);//设置是否拷贝还是更新,此处是拷贝request.SetCopyMetaDataDirective(COSXML.Common.CosMetaDataDirective.Replaced);// 替换元数据request.SetRequestHeader("Content-Disposition", "attachment; filename=example.jpg");//执行请求CopyObjectResult result = cosXml.CopyObject(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 appId = "1250000000"; //账号 appidstring region = "COS_REGION"; //源对象的存储桶所在的地域//构造对象属性CopySourceStruct copySource = new CopySourceStruct(appId, bucket,region, key);CopyObjectRequest request = new CopyObjectRequest(bucket, key);//设置拷贝源request.SetCopySource(copySource);//设置是否拷贝还是更新,此处是拷贝request.SetCopyMetaDataDirective(COSXML.Common.CosMetaDataDirective.Replaced);// 替换元数据request.SetRequestHeader("Content-Disposition", "attachment; filename=example.jpg");request.SetRequestHeader("Content-Type", "image/png");//执行请求CopyObjectResult result = cosXml.CopyObject(request);//请求成功Console.WriteLine(result.GetResultInfo());}catch (COSXML.CosException.CosClientException clientEx){//请求失败Console.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){//请求失败Console.WriteLine("CosServerException: " + serverEx.GetInfo());}
string sourceAppid = "1250000000"; // 账号 appid// 源对象所在存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.tencentcloud.com/developerstring sourceBucket = "sourcebucket-1250000000";string sourceRegion = "COS_REGION"; //源对象的存储桶所在的地域string sourceKey = "sourceObject"; //源对象键//构造源对象属性CopySourceStruct copySource = new CopySourceStruct(sourceAppid, sourceBucket,sourceRegion, sourceKey);// 目标存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";string key = "exampleobject"; //目标对象的对象键COSXMLCopyTask copyTask = new COSXMLCopyTask(bucket, key, copySource);try {// 拷贝对象COSXML.Transfer.COSXMLCopyTask.CopyTaskResult result = awaittransferManager.CopyAsync(copyTask);Console.WriteLine(result.GetResultInfo());// 删除对象DeleteObjectRequest request = new DeleteObjectRequest(sourceBucket, sourceKey);DeleteObjectResult deleteResult = cosXml.DeleteObject(request);// 打印结果Console.WriteLine(deleteResult.GetResultInfo());} catch (Exception e) {Console.WriteLine("CosException: " + e);}
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{string sourceAppid = "1250000000"; //账号 appidstring sourceBucket = "sourcebucket-1250000000"; //"源对象所在的存储桶string sourceRegion = "COS_REGION"; //源对象的存储桶所在的地域string sourceKey = "sourceObject"; //源对象键//构造源对象属性COSXML.Model.Tag.CopySourceStruct copySource = new CopySourceStruct(sourceAppid,sourceBucket, sourceRegion, sourceKey);// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";string key = "exampleobject"; //对象键string uploadId = this.uploadId; //初始化分块上传/复制返回的uploadIdint partNumber = 1; //分块编号,必须从1开始递增UploadPartCopyRequest request = new UploadPartCopyRequest(bucket, key,partNumber, uploadId);//设置拷贝源request.SetCopySource(copySource);//设置复制分块(指定块的范围,如 0 ~ 1M)request.SetCopyRange(0, 1024 * 1024);//执行请求UploadPartCopyResult result = cosXml.PartCopy(request);//请求成功//获取返回分块的eTag,用于后续CompleteMultiUploadsthis.eTag = result.copyPart.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());}
本页内容是否解决了您的问题?