API | 操作名 | 操作描述 |
删除单个对象 | 在存储桶中删除指定对象 | |
删除多个对象 | 在存储桶中批量删除对象 |
try{// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";string key = "exampleobject"; //对象键DeleteObjectRequest request = new DeleteObjectRequest(bucket, key);//执行请求DeleteObjectResult result = cosXml.DeleteObject(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";DeleteMultiObjectRequest request = new DeleteMultiObjectRequest(bucket);//设置返回结果形式request.SetDeleteQuiet(false);//对象keystring key = "exampleobject"; //对象键List<string> objects = new List<string>();objects.Add(key);request.SetObjectKeys(objects);//执行请求DeleteMultiObjectResult result = cosXml.DeleteMultiObjects(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 nextMarker = null;// 循环请求直到没有下一页数据do{// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";string prefix = "folder1/"; //指定前缀GetBucketRequest listRequest = new GetBucketRequest(bucket);//获取 folder1/ 下的所有对象以及子目录listRequest.SetPrefix(prefix);listRequest.SetMarker(nextMarker);//执行列出对象请求GetBucketResult listResult = cosXml.GetBucket(listRequest);ListBucket info = listResult.listBucket;// 对象列表List<ListBucket.Contents> objects = info.contentsList;// 下一页的下标nextMarker = info.nextMarker;DeleteMultiObjectRequest deleteRequest = new DeleteMultiObjectRequest(bucket);//设置返回结果形式deleteRequest.SetDeleteQuiet(false);//对象列表List<string> deleteObjects = new List<string>();foreach (var content in objects){deleteObjects.Add(content.key);}deleteRequest.SetObjectKeys(deleteObjects);//执行批量删除请求DeleteMultiObjectResult deleteResult = cosXml.DeleteMultiObjects(deleteRequest);//打印请求结果Console.WriteLine(deleteResult.GetResultInfo());} while (nextMarker != null);}catch (COSXML.CosException.CosClientException clientEx){//请求失败Console.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){//请求失败Console.WriteLine("CosServerException: " + serverEx.GetInfo());}
本页内容是否解决了您的问题?