删除对象

最近更新时间:2024-08-13 18:02:01

我的收藏

简介

本文档提供关于对象的删除操作相关的 API 概览以及 SDK 示例代码。

注意事项

若您使用删除单个对象接口,需要具有目标对象的删除权限:在您进行 授权策略 时,action 需要设置为cos:DeleteObject,更多授权请参见 支持CAM的业务接口
若您在匿名访问场景下使用删除多个对象接口,需要具有所有目标对象的删除权限和批删权限:在您进行 授权策略 时,action 需要设置为cos:DeleteObjectcos:DeleteMultipleObjects,更多授权请参见 支持CAM的业务接口
若您在非匿名访问场景下使用删除多个对象接口,需要具有所有目标对象的删除权限:在您进行 授权策略 时,action 需要设置为cos:DeleteObject,更多授权请参见 支持CAM的业务接口

相关示例

功能名称
描述
示例代码
删除单个对象
在存储桶中删除指定对象
删除多个对象
在存储桶中批量删除对象

删除对象

功能说明

删除指定的对象(DELETE Object)。

使用案例:删除单个对象

Objective-C
Swift
QCloudDeleteObjectRequest* deleteObjectRequest = [QCloudDeleteObjectRequest new];

// 存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket
deleteObjectRequest.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/bucket
deleteObject.bucket = "examplebucket-1250000000";


// 对象键,是对象在 COS 上的完整路径,如果带目录的话,格式为 "video/xxx/movie.mp4"
deleteObject.object = "exampleobject";

deleteObject.finishBlock = {(result,error)in
if let result = result {
// result 包含响应的 header 信息
} else {
print(error!);
}
}
QCloudCOSXMLService.defaultCOSXML().deleteObject(deleteObject);

使用案例:删除目录

Objective-C
Swift
QCloudGetBucketRequest* request = [QCloudGetBucketRequest new];
// 存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket
request.bucket = @"examplebucket-1250000000";
// 单次返回的最大条目数量,默认1000
request.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/bucket
getBucketReq.bucket = "examplebucket-1250000000";

// 单次返回的最大条目数量,默认1000
getBucketReq.maxKeys = 100;

//要删除的目录名称:带'/'
getBucketReq.prefix = "dir/";

getBucketReq.setFinish { (result, error) in
if 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/bucket
mutipleDel.bucket = "examplebucket-1250000000";

deleteInfos.objects = infos as! [QCloudDeleteObjectInfo];

// 布尔值,这个值决定了是否启动 Quiet 模式:
// true:启动 Quiet 模式
// false:启动 Verbose 模式
// 默认值为 False
deleteInfos.quiet = false;

// 封装了需要批量删除的多个对象的信息
mutipleDel.deleteObjects = deleteInfos;

mutipleDel.setFinish { (result, error) in
if let result = result {
let deleted = result.deletedObjects
let failed = result.deletedFailedObjects
} else {
print(error!);
}
}
QCloudCOSXMLService.defaultCOSXML().deleteMultipleObject(mutipleDel);
} else {
print(error!);
}
}
QCloudCOSXMLService.defaultCOSXML().getBucket(getBucketReq);

删除多个对象

功能说明

批量删除多个对象(DELETE Multiple Objects)。

使用案例:批量删除对象

Objective-C
Swift
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 模式
// 默认值为 False
deleteInfo.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/bucket
mutipleDel.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 模式
// 默认值为 False
deleteInfos.quiet = false;

// 封装了需要批量删除的多个对象的信息
mutipleDel.deleteObjects = deleteInfos;

mutipleDel.setFinish { (result, error) in
if let result = result {
let deleted = result.deletedObjects
let failed = result.deletedFailedObjects
} else {
print(error!);
}
}
QCloudCOSXMLService.defaultCOSXML().deleteMultipleObject(mutipleDel);

使用案例:删除带指定前缀的对象

Objective-C
Swift
QCloudGetBucketRequest* request = [QCloudGetBucketRequest new];
// 存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket
request.bucket = @"examplebucket-1250000000";
// 单次返回的最大条目数量,默认1000
request.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/bucket
getBucketReq.bucket = "examplebucket-1250000000";

// 单次返回的最大条目数量,默认1000
getBucketReq.maxKeys = 100;

//如果要删除指定前缀的文件:prefix为文件名前缀
getBucketReq.prefix = "dir/";


getBucketReq.setFinish { (result, error) in
if 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/bucket
mutipleDel.bucket = "examplebucket-1250000000";

deleteInfos.objects = infos as! [QCloudDeleteObjectInfo];

// 布尔值,这个值决定了是否启动 Quiet 模式:
// true:启动 Quiet 模式
// false:启动 Verbose 模式
// 默认值为 False
deleteInfos.quiet = false;

// 封装了需要批量删除的多个对象的信息
mutipleDel.deleteObjects = deleteInfos;

mutipleDel.setFinish { (result, error) in
if let result = result {
let deleted = result.deletedObjects
let failed = result.deletedFailedObjects
} else {
print(error!);
}
}
QCloudCOSXMLService.defaultCOSXML().deleteMultipleObject(mutipleDel);
} else {
print(error!);
}
}
QCloudCOSXMLService.defaultCOSXML().getBucket(getBucketReq);

SDK API 参考

SDK 所有接口的具体参数与方法说明,请参考 SDK API

API 操作

关于删除单个对象的 API 接口说明,请参见 DELETE Object 文档。
关于删除多个对象的 API 接口说明,请参见 DELETE Multiple Objects 文档。