API | 操作名 | 操作描述 |
删除单个对象 | 在存储桶中删除指定对象 | |
删除多个对象 | 在存储桶中批量删除对象 |
String bucket = "examplebucket-1250000000"; //存储桶名称,格式:BucketName-APPIDString cosPath = "exampleobject"; //对象在存储桶中的位置标识符,即对象键DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(bucket,cosPath);cosXmlService.deleteObjectAsync(deleteObjectRequest,new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {DeleteObjectResult deleteObjectResult = (DeleteObjectResult) result;}// 如果您使用 kotlin 语言来调用,请注意回调方法中的异常是可空的,否则不会回调 onFail 方法,即:// clientException 的类型为 CosXmlClientException?,serviceException 的类型为 CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
// 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";List<String> objectList = new ArrayList<String>();objectList.add("exampleobject1"); //对象在存储桶中的位置标识符,即对象键objectList.add("exampleobject2"); //对象在存储桶中的位置标识符,即对象键DeleteMultiObjectRequest deleteMultiObjectRequest =new DeleteMultiObjectRequest(bucket, objectList);// Quiet 模式只返回报错的 Object 信息。否则返回每个 Object 的删除结果。deleteMultiObjectRequest.setQuiet(true);cosXmlService.deleteMultiObjectAsync(deleteMultiObjectRequest,new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {DeleteMultiObjectResult deleteMultiObjectResult =(DeleteMultiObjectResult) result;}// 如果您使用 kotlin 语言来调用,请注意回调方法中的异常是可空的,否则不会回调 onFail 方法,即:// clientException 的类型为 CosXmlClientException?,serviceException 的类型为 CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
/
分隔对象名,形成类似文件系统的路径,从而模拟出来的。所以删除文件夹的操作,在 COS 上相当于删除一批有着同样前缀的对象。例如:文件夹 prefix/
,代表的是以 prefix/
为前缀的所有对象,所以删除 prefix/
意味着删除以 prefix/
为前缀的所有对象。// 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";String prefix = "folder1/"; //指定前缀GetBucketRequest getBucketRequest = new GetBucketRequest(bucket);getBucketRequest.setPrefix(prefix);// prefix表示要删除的文件夹getBucketRequest.setPrefix(prefix);// 设置最大遍历出多少个对象, 一次listobject最大支持1000getBucketRequest.setMaxKeys(1000);GetBucketResult getBucketResult = null;do {try {getBucketResult = cosXmlService.getBucket(getBucketRequest);List<ListBucket.Contents> contents = getBucketResult.listBucket.contentsList;DeleteMultiObjectRequest deleteMultiObjectRequest = new DeleteMultiObjectRequest(bucket);for (ListBucket.Contents content : contents) {deleteMultiObjectRequest.setObjectList(content.key);}cosXmlService.deleteMultiObject(deleteMultiObjectRequest);getBucketRequest.setMarker(getBucketResult.listBucket.nextMarker);} catch (CosXmlClientException e) {e.printStackTrace();return;} catch (CosXmlServiceException e) {e.printStackTrace();return;}} while (getBucketResult.listBucket.isTruncated);
本页内容是否解决了您的问题?