API | Operation | Description |
Deleting an object | Deletes an object from a bucket. | |
Deleting multiple objects | Deletes multiple objects from a bucket. |
DELETE Object
) is used to delete a specified object.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 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());}
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";DeleteMultiObjectRequest request = new DeleteMultiObjectRequest(bucket);// Set the return result formatrequest.SetDeleteQuiet(false);// Object keystring key = "exampleobject"; // Object keyList<string> objects = new List<string>();objects.Add(key);request.SetObjectKeys(objects);// Execute the requestDeleteMultiObjectResult result = cosXml.DeleteMultiObjects(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 nextMarker = null;// Request until there is no next page of datado{// 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 prefix = "folder1/"; // Specify a prefix.GetBucketRequest listRequest = new GetBucketRequest(bucket);// Obtain all objects and subdirectories in folder1/.listRequest.SetPrefix(prefix);listRequest.SetMarker(nextMarker);// Execute the list object request.GetBucketResult listResult = cosXml.GetBucket(listRequest);ListBucket info = listResult.listBucket;// List objectsList<ListBucket.Contents> objects = info.contentsList;// nextMarker for the next pagenextMarker = info.nextMarker;DeleteMultiObjectRequest deleteRequest = new DeleteMultiObjectRequest(bucket);// Set the return result formatdeleteRequest.SetDeleteQuiet(false);// List objectsList<string> deleteObjects = new List<string>();foreach (var content in objects){deleteObjects.Add(content.key);}deleteRequest.SetObjectKeys(deleteObjects);// Execute the batch delete request.DeleteMultiObjectResult deleteResult = cosXml.DeleteMultiObjects(deleteRequest);// Print the request result.Console.WriteLine(deleteResult.GetResultInfo());} while (nextMarker != null);}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?