API | Operation | Description |
Copying an object (modifying object attributes) | Copies a file to a destination path. | |
Deleting an object | Deletes a specified object from a bucket. |
API | Operation | Description |
Querying parts | Queries in-progress parts. | |
Initializing a part | Initializes a part. | |
Copying a part | Copies an object as a part. | |
Querying copied/uploaded parts | Queries the copied/uploaded parts of a multipart operation. | |
Completing a multipart upload/copy | Completes the multipart upload/copy of a file. | |
Aborting a multipart upload/copy | Aborts a multipart operation and deletes the uploaded/copied parts. |
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") // 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);}/// Copy an object via advanced APIpublic async Task TransferCopyObject(){TransferConfig transferConfig = new TransferConfig();// Manually set the multipart copy threshold. If the object size is less than the threshold, simple copy is used. Otherwise, multipart copy is used. If no value is specified, the default value 5 MB is used.transferConfig.DdivisionForCopy = 5242880;// Manually set the automatic part size for the advanced API. If no value is specified, the default value 2 MB is used.transferConfig.SliceSizeForCopy = 2097152;// Initialize TransferManagerTransferManager transferManager = new TransferManager(cosXml, transferConfig);string sourceAppid = "1250000000"; // Account appidstring sourceBucket = "sourcebucket-1250000000"; //" Source object bucketstring sourceRegion = "COS_REGION"; // Source object bucket regionstring sourceKey = "sourceObject"; // Source object key// Construct source object attributesCopySourceStruct copySource = new CopySourceStruct(sourceAppid, sourceBucket,sourceRegion, sourceKey);string bucket = "examplebucket-1250000000"; // Destination bucket in the format of BucketName-APPIDstring key = "exampleobject"; // Object key of the destination bucketCOSXMLCopyTask 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){// Request failedConsole.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){// Request failedConsole.WriteLine("CosServerException: " + serverEx.GetInfo());}}static void Main(string[] args){TransferCopyObjectModel m = new TransferCopyObjectModel();/// Copy an object via advanced APIm.TransferCopyObject().Wait();}}}
PUT Object - Copy
) is used to copy a file to a destination path. You can set and modify object attributes and metadata in the copy request. The API also supports in-situ copy, which allows you to modify object metadata where it is.try{string sourceAppid = "1250000000"; // Account appidstring sourceBucket = "sourcebucket-1250000000"; //" Source object bucketstring sourceRegion = "COS_REGION"; // Source object bucket regionstring sourceKey = "sourceObject"; // Source object key// Construct source object attributesCopySourceStruct copySource = new CopySourceStruct(sourceAppid, sourceBucket,sourceRegion, sourceKey);// 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 keyCopyObjectRequest request = new CopyObjectRequest(bucket, key);// Set the copy sourcerequest.SetCopySource(copySource);// Set whether to copy or update. Copy is used hererequest.SetCopyMetaDataDirective(COSXML.Common.CosMetaDataDirective.Copy);// Execute the requestCopyObjectResult result = cosXml.CopyObject(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 appId = "1250000000"; // Account APPIDstring region = "COS_REGION"; // Region where the bucket of the source object resides// Construct object attributesCopySourceStruct copySource = new CopySourceStruct(appId, bucket,region, key);CopyObjectRequest request = new CopyObjectRequest(bucket, key);// Set the copy sourcerequest.SetCopySource(copySource);// Set whether to copy or update. Copy is used hererequest.SetCopyMetaDataDirective(COSXML.Common.CosMetaDataDirective.Replaced);// Change the storage class to ARCHIVErequest.SetCosStorageClass("ARCHIVE");// Execute the requestCopyObjectResult result = cosXml.CopyObject(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{string sourceAppid = "1250000000"; // Account appidstring sourceBucket = "sourcebucket-1250000000"; //" Source object bucketstring sourceRegion = "COS_REGION"; // Source object bucket regionstring sourceKey = "sourceObject"; // Source object key// Construct source object attributesCopySourceStruct copySource = new CopySourceStruct(sourceAppid, sourceBucket,sourceRegion, sourceKey);// 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 keyCopyObjectRequest request = new CopyObjectRequest(bucket, key);// Set the copy sourcerequest.SetCopySource(copySource);// Set whether to copy or update. Copy is used hererequest.SetCopyMetaDataDirective(COSXML.Common.CosMetaDataDirective.Replaced);// Replace metadatarequest.SetRequestHeader("Content-Disposition", "attachment; filename=example.jpg");// Execute the requestCopyObjectResult result = cosXml.CopyObject(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 appId = "1250000000"; // Account APPIDstring region = "COS_REGION"; // Region where the bucket of the source object resides// Construct object attributesCopySourceStruct copySource = new CopySourceStruct(appId, bucket,region, key);CopyObjectRequest request = new CopyObjectRequest(bucket, key);// Set the copy sourcerequest.SetCopySource(copySource);// Set whether to copy or update. Copy is used hererequest.SetCopyMetaDataDirective(COSXML.Common.CosMetaDataDirective.Replaced);// Replace metadatarequest.SetRequestHeader("Content-Disposition", "attachment; filename=example.jpg");request.SetRequestHeader("Content-Type", "image/png");// Execute the requestCopyObjectResult result = cosXml.CopyObject(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());}
Bucket
) and object key (ObjectKey
) to identify objects, moving an object will change the object identifier. Currently, COS .NET SDK does not provide a standalone API to change object identifiers. However, you can still move the object with a combination of basic operations (object copy and object delete).picture.jpg
object to the “doc” directory that is in the same bucket (mybucket-1250000000
), you can copy the picture.jpg
to the “doc” directory (making the object key doc/picture.jpg
) and then delete the source object.picture.jpg
in the mybucket-1250000000
bucket to another bucket myanothorbucket-1250000000
, you can copy the object to the myanothorbucket-1250000000
bucket first and then delete the source object.string sourceAppid = "1250000000"; // Account APPID// Name of the bucket where the source object resides, in the format of `BucketName-APPID`. You can get APPID by referring to https://console.tencentcloud.com/developer.string sourceBucket = "sourcebucket-1250000000";string sourceRegion = "COS_REGION"; // Source object bucket regionstring sourceKey = "sourceObject"; // Source object key// Construct source object attributesCopySourceStruct copySource = new CopySourceStruct(sourceAppid, sourceBucket,sourceRegion, sourceKey);// Name of destination bucket, 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 key of the destination bucketCOSXMLCopyTask copyTask = new COSXMLCopyTask(bucket, key, copySource);try {// Copy the object.COSXML.Transfer.COSXMLCopyTask.CopyTaskResult result = awaittransferManager.CopyAsync(copyTask);Console.WriteLine(result.GetResultInfo());// Delete the object.DeleteObjectRequest request = new DeleteObjectRequest(sourceBucket, sourceKey);DeleteObjectResult deleteResult = cosXml.DeleteObject(request);// Print results.Console.WriteLine(deleteResult.GetResultInfo());} catch (Exception e) {Console.WriteLine("CosException: " + e);}
InitiateMultipartUpload
, UploadPart/UploadPartCopy
, and `CompleteMultipartUpload APIs can be used for both multipart upload and multipart copy. Therefore, there is a certain degree of overlap between the APIs and usages between multipart copy and multipart upload.Initiate Multipart Upload
and get the UploadId
.UploadId
to copy the parts with Upload Part - Copy
.Complete Multipart Upload
.UploadId
of the multipart copy, you can query the multipart copy job with List Multipart Uploads
to get the UploadId
of the corresponding file.UploadId
to list the copied parts with List Parts
.UploadId
to copy the remaining parts with Upload Part - Copy
.Complete Multipart Upload
.UploadId
of the multipart upload, you can query the multipart copy 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 copies 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());}
Initiate Multipart Upload
) is used to initialize a multipart copy operation and get its 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 copiesConsole.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 - Copy
) is used to copy an object as a part.try{string sourceAppid = "1250000000"; // Account appidstring sourceBucket = "sourcebucket-1250000000"; //" Source object bucketstring sourceRegion = "COS_REGION"; // Source object bucket regionstring sourceKey = "sourceObject"; // Source object key// Construct source object attributesCOSXML.Model.Tag.CopySourceStruct copySource = new CopySourceStruct(sourceAppid,sourceBucket, sourceRegion, sourceKey);// 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 = this.uploadId; // `uploadId` returned when the multipart upload/copy is initializedint partNumber = 1; // Part number, increases incrementally starting from 1UploadPartCopyRequest request = new UploadPartCopyRequest(bucket, key,partNumber, uploadId);// Set the copy sourcerequest.SetCopySource(copySource);// Set the range of parts to be copied, e.g., 0 to 1Mrequest.SetCopyRange(0, 1024 * 1024);// Execute the requestUploadPartCopyResult result = cosXml.PartCopy(request);// Request succeeded// Get the ETag of the returned part for subsequent CompleteMultiUploads.this.eTag = result.copyPart.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 copied parts of a multipart copy.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/copy is initializedListPartsRequest request = new ListPartsRequest(bucket, key, uploadId);// Execute the requestListPartsResult result = cosXml.ListParts(request);// Request succeeded// List uploaded/copied partsList<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 copy 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/copy is initializedCompleteMultipartUploadRequest request = new CompleteMultipartUploadRequest(bucket,key, uploadId);// Concatenate copied 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 copy and delete the copied 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/copy 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());}
Apakah halaman ini membantu?