API | Operation | Description |
Deleting an object | Deletes an object from a bucket. | |
Deleting multiple objects | Deletes multiple objects from a bucket in a single request |
DELETE Object
) is used to delete a specified object.String bucket = "examplebucket-1250000000"; // Bucket, formatted as BucketName-APPIDString cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e., the object keyDeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(bucket,cosPath);cosXmlService.deleteObjectAsync(deleteObjectRequest,new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {DeleteObjectResult deleteObjectResult = (DeleteObjectResult) result;}// If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:// clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
// Bucket name in the format of BucketName-APPID (APPID is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";List<String> objectList = new ArrayList<String>();objectList.add("exampleobject1"); // The location identifier of the object in the bucket, i.e., the object keyobjectList.add("exampleobject2"); // The location identifier of the object in the bucket, i.e., the object keyDeleteMultiObjectRequest deleteMultiObjectRequest =new DeleteMultiObjectRequest(bucket, objectList);// In quiet mode, only information on objects that failed to be deleted will be returned; otherwise, the deletion result of each object will be returned.deleteMultiObjectRequest.setQuiet(true);cosXmlService.deleteMultiObjectAsync(deleteMultiObjectRequest,new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {DeleteMultiObjectResult deleteMultiObjectResult =(DeleteMultiObjectResult) result;}// If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:// clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
prefix/
is actually all objects prefixed with prefix/
. Therefore, you can delete all objects prefixed with prefix/
to delete the prefix/
directory.// Bucket name in the format of BucketName-APPID (APPID is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";String prefix = "folder1/"; // Specify a prefix.GetBucketRequest getBucketRequest = new GetBucketRequest(bucket);getBucketRequest.setPrefix(prefix);// "prefix" indicates the directory to delete.getBucketRequest.setPrefix(prefix);// Set the maximum number of traversed objects (up to 1,000 per listobject request)getBucketRequest.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);
Apakah halaman ini membantu?