简介
本文档提供关于对象的删除操作相关的 API 概览以及 SDK 示例代码。
注意事项
若您使用删除单个对象接口,需要具有目标对象的删除权限:在您进行 授权策略 时,action 需要设置为
cos:DeleteObject
,更多授权请参见 支持CAM的业务接口。若您在匿名访问场景下使用删除多个对象接口,需要具有所有目标对象的删除权限和批删权限:在您进行 授权策略 时,action 需要设置为
cos:DeleteObject
和cos:DeleteMultipleObjects
,更多授权请参见 支持CAM的业务接口。若您在非匿名访问场景下使用删除多个对象接口,需要具有所有目标对象的删除权限:在您进行 授权策略 时,action 需要设置为
cos:DeleteObject
,更多授权请参见 支持CAM的业务接口。相关示例
功能名称 | 描述 | 示例代码 |
删除单个对象 | 在存储桶中删除指定对象 | |
删除多个对象 | 在存储桶中批量删除对象 |
删除对象
功能说明
删除指定的对象(DELETE Object)。
使用案例:删除单个对象
QCloudDeleteObjectRequest* deleteObjectRequest = [QCloudDeleteObjectRequest new];// 存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucketdeleteObjectRequest.bucket = @"examplebucket-1250000000";// 对象键,是对象在 COS 上的完整路径,如果带目录的话,格式为 "video/xxx/movie.mp4"deleteObjectRequest.object = @"exampleobject";[deleteObjectRequest setFinishBlock:^(id outputObject, NSError *error) {// outputObject 包含所有的响应 http 头部NSDictionary* info = (NSDictionary *) outputObject;}];[[QCloudCOSXMLService defaultCOSXML] DeleteObject:deleteObjectRequest];
let deleteObject = QCloudDeleteObjectRequest.init();// 存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucketdeleteObject.bucket = "examplebucket-1250000000";// 对象键,是对象在 COS 上的完整路径,如果带目录的话,格式为 "video/xxx/movie.mp4"deleteObject.object = "exampleobject";deleteObject.finishBlock = {(result,error)inif let result = result {// result 包含响应的 header 信息} else {print(error!);}}QCloudCOSXMLService.defaultCOSXML().deleteObject(deleteObject);
使用案例:删除目录
QCloudGetBucketRequest* request = [QCloudGetBucketRequest new];// 存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucketrequest.bucket = @"examplebucket-1250000000";// 单次返回的最大条目数量,默认1000request.maxKeys = 100;//要删除的目录名称:带'/'request.prefix = @"prefix";[request setFinishBlock:^(QCloudListBucketResult * result, NSError* error) {if(!error){NSMutableArray *deleteInfosArr = [NSMutableArray array];for (QCloudBucketContents *content in result.contents) {QCloudDeleteMultipleObjectRequest *delteRequest = [QCloudDeleteMultipleObjectRequest new];delteRequest.bucket = request.bucket;QCloudDeleteObjectInfo *object = [QCloudDeleteObjectInfo new];object.key = content.key;[deleteInfosArr addObject:object];}QCloudDeleteInfo *deleteInfos = [QCloudDeleteInfo new];deleteInfos.objects = [deleteInfosArr copy];QCloudDeleteMultipleObjectRequest *delteRequest = [QCloudDeleteMultipleObjectRequest new];delteRequest.bucket = @"examplebucket-1250000000";delteRequest.deleteObjects = deleteInfos;[delteRequest setFinishBlock:^(QCloudDeleteResult *outputObject, NSError *error) {NSLog(@"outputObject = %@",outputObject);}];[[QCloudCOSXMLService defaultCOSXML] DeleteMultipleObject:delteRequest];}}];[[QCloudCOSXMLService defaultCOSXML] GetBucket:request];
let getBucketReq = QCloudGetBucketRequest.init();// 存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucketgetBucketReq.bucket = "examplebucket-1250000000";// 单次返回的最大条目数量,默认1000getBucketReq.maxKeys = 100;//要删除的目录名称:带'/'getBucketReq.prefix = "dir/";getBucketReq.setFinish { (result, error) inif let result = result {let contents = result.contents;let infos = NSMutableArray.init();for content in contents {let info = QCloudDeleteObjectInfo.init();info.key = content.key;infos.add(info);}let mutipleDel = QCloudDeleteMultipleObjectRequest.init();// 要删除的文件集合let deleteInfos = QCloudDeleteInfo.init();// 存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucketmutipleDel.bucket = "examplebucket-1250000000";deleteInfos.objects = infos as! [QCloudDeleteObjectInfo];// 布尔值,这个值决定了是否启动 Quiet 模式:// true:启动 Quiet 模式// false:启动 Verbose 模式// 默认值为 FalsedeleteInfos.quiet = false;// 封装了需要批量删除的多个对象的信息mutipleDel.deleteObjects = deleteInfos;mutipleDel.setFinish { (result, error) inif let result = result {let deleted = result.deletedObjectslet failed = result.deletedFailedObjects} else {print(error!);}}QCloudCOSXMLService.defaultCOSXML().deleteMultipleObject(mutipleDel);} else {print(error!);}}QCloudCOSXMLService.defaultCOSXML().getBucket(getBucketReq);
删除多个对象
功能说明
批量删除多个对象(DELETE Multiple Objects)。
使用案例:批量删除对象
QCloudDeleteMultipleObjectRequest* delteRequest = [QCloudDeleteMultipleObjectRequest new];delteRequest.bucket = @"examplebucket-1250000000";// 要删除的单个文件QCloudDeleteObjectInfo* deletedObject0 = [QCloudDeleteObjectInfo new];// 对象键,是对象在 COS 上的完整路径,如果带目录的话,格式为 "video/xxx/movie.mp4"deletedObject0.key = @"exampleobject";// 要删除的文件集合QCloudDeleteInfo* deleteInfo = [QCloudDeleteInfo new];// 布尔值,这个值决定了是否启动 Quiet 模式:// true:启动 Quiet 模式// false:启动 Verbose 模式// 默认值为 FalsedeleteInfo.quiet = NO;// 存放需要删除对象信息的数组deleteInfo.objects = @[deletedObject0];// 封装了需要批量删除的多个对象的信息delteRequest.deleteObjects = deleteInfo;[delteRequest setFinishBlock:^(QCloudDeleteResult* outputObject,NSError *error) {// 可以从 outputObject 中获取 response 中 etag 或者自定义头部等信息}];[[QCloudCOSXMLService defaultCOSXML] DeleteMultipleObject:delteRequest];
let mutipleDel = QCloudDeleteMultipleObjectRequest.init();// 存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucketmutipleDel.bucket = "examplebucket-1250000000";// 要删除的单个文件let info1 = QCloudDeleteObjectInfo.init();// 对象键,是对象在 COS 上的完整路径,如果带目录的话,格式为 "video/xxx/movie.mp4"info1.key = "exampleobject";let info2 = QCloudDeleteObjectInfo.init();// 要删除的文件集合let deleteInfos = QCloudDeleteInfo.init();// 存放需要删除对象信息的数组deleteInfos.objects = [info1,info2];// 布尔值,这个值决定了是否启动 Quiet 模式:// true:启动 Quiet 模式// false:启动 Verbose 模式// 默认值为 FalsedeleteInfos.quiet = false;// 封装了需要批量删除的多个对象的信息mutipleDel.deleteObjects = deleteInfos;mutipleDel.setFinish { (result, error) inif let result = result {let deleted = result.deletedObjectslet failed = result.deletedFailedObjects} else {print(error!);}}QCloudCOSXMLService.defaultCOSXML().deleteMultipleObject(mutipleDel);
使用案例:删除带指定前缀的对象
QCloudGetBucketRequest* request = [QCloudGetBucketRequest new];// 存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucketrequest.bucket = @"examplebucket-1250000000";// 单次返回的最大条目数量,默认1000request.maxKeys = 100;//如果要删除指定前缀的文件:prefix为文件名前缀request.prefix = @"prefix";[request setFinishBlock:^(QCloudListBucketResult * result, NSError* error) {if(!error){NSMutableArray *deleteInfosArr = [NSMutableArray array];for (QCloudBucketContents *content in result.contents) {QCloudDeleteMultipleObjectRequest *delteRequest = [QCloudDeleteMultipleObjectRequest new];delteRequest.bucket = request.bucket;QCloudDeleteObjectInfo *object = [QCloudDeleteObjectInfo new];object.key = content.key;[deleteInfosArr addObject:object];}QCloudDeleteInfo *deleteInfos = [QCloudDeleteInfo new];deleteInfos.objects = [deleteInfosArr copy];QCloudDeleteMultipleObjectRequest *delteRequest = [QCloudDeleteMultipleObjectRequest new];delteRequest.bucket = @"examplebucket-1250000000";delteRequest.deleteObjects = deleteInfos;[delteRequest setFinishBlock:^(QCloudDeleteResult *outputObject, NSError *error) {NSLog(@"outputObject = %@",outputObject);}];[[QCloudCOSXMLService defaultCOSXML] DeleteMultipleObject:delteRequest];}}];[[QCloudCOSXMLService defaultCOSXML] GetBucket:request];
let getBucketReq = QCloudGetBucketRequest.init();// 存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucketgetBucketReq.bucket = "examplebucket-1250000000";// 单次返回的最大条目数量,默认1000getBucketReq.maxKeys = 100;//如果要删除指定前缀的文件:prefix为文件名前缀getBucketReq.prefix = "dir/";getBucketReq.setFinish { (result, error) inif let result = result {let contents = result.contents;let infos = NSMutableArray.init();for content in contents {let info = QCloudDeleteObjectInfo.init();info.key = content.key;infos.add(info);}let mutipleDel = QCloudDeleteMultipleObjectRequest.init();// 要删除的文件集合let deleteInfos = QCloudDeleteInfo.init();// 存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucketmutipleDel.bucket = "examplebucket-1250000000";deleteInfos.objects = infos as! [QCloudDeleteObjectInfo];// 布尔值,这个值决定了是否启动 Quiet 模式:// true:启动 Quiet 模式// false:启动 Verbose 模式// 默认值为 FalsedeleteInfos.quiet = false;// 封装了需要批量删除的多个对象的信息mutipleDel.deleteObjects = deleteInfos;mutipleDel.setFinish { (result, error) inif let result = result {let deleted = result.deletedObjectslet failed = result.deletedFailedObjects} else {print(error!);}}QCloudCOSXMLService.defaultCOSXML().deleteMultipleObject(mutipleDel);} else {print(error!);}}QCloudCOSXMLService.defaultCOSXML().getBucket(getBucketReq);
SDK API 参考
API 操作
关于删除单个对象的 API 接口说明,请参见 DELETE Object 文档。
关于删除多个对象的 API 接口说明,请参见 DELETE Multiple Objects 文档。