cos:DeleteObject. For more information on authorization, see CAM-supported Business APIs.cos:DeleteObject and cos:DeleteMultipleObjects. For more information on authorization, see CAM-supported Business APIs.cos:DeleteObject. For more information on authorization, see CAM-supported Business APIs.Feature Name | Description | Example code |
Delete an object. | Provides the feature to delete a single object and delete multiple objects. |
String bucket = "examplebucket-1250000000"; // Bucket name. Format: BucketName-APPIDString cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e., the object key.DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(bucket,cosPath);cosXmlService.deleteObjectAsync(deleteObjectRequest,new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {DeleteObjectResult deleteObjectResult = (DeleteObjectResult) result;}// If you call the API using kotlin, note that the exception in the callback method must be nullable. Otherwise, the onFail method will not be called, as follows:// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
// A bucket name consists of bucketname-appid. The appid must be included. You can view bucket names in the COS console. 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 key.objectList.add("exampleobject2"); // The location identifier of the object in the bucket, i.e., the object key.DeleteMultiObjectRequest deleteMultiObjectRequest =new DeleteMultiObjectRequest(bucket, objectList);// In Quiet mode, only information about objects that encountered errors is returned. Otherwise, the deletion result for each Object is returned.deleteMultiObjectRequest.setQuiet(true);cosXmlService.deleteMultiObjectAsync(deleteMultiObjectRequest,new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {DeleteMultiObjectResult deleteMultiObjectResult =(DeleteMultiObjectResult) result;}// If you call the API using kotlin, note that the exception in the callback method must be nullable. Otherwise, the onFail method will not be called, as follows:// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
/ to separate object names, forming a file system-like path. Therefore, deleting a folder in COS is equivalent to deleting a batch of objects that share the same prefix. For example: the folder prefix/ represents all objects prefixed with prefix/. Thus, deleting prefix/ means deleting all objects prefixed with prefix/.// A bucket name consists of bucketname-appid. The appid must be included. You can view bucket names in the COS console. https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";String prefix = "folder1/"; // Specify the prefix.GetBucketRequest getBucketRequest = new GetBucketRequest(bucket);// prefix indicates the folder to be deleted.getBucketRequest.setPrefix(prefix);// Set the maximum number of objects to be traversed, and the maximum number of objects supported by a listobject is 1,000.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);
Esta página foi útil?
Você também pode entrar em contato com a Equipe de vendas ou Enviar um tíquete em caso de ajuda.
comentários