tencent cloud

Cloud Object Storage

Deleting Objects

Baixar
Modo Foco
Tamanho da Fonte
Última atualização: 2026-03-05 16:58:10

Introduction

This article introduces the sample code and description that demonstrate the feature of deleting an object using the COS C++ SDK.

Must-Knows

If you use the delete single object API, you need to have the delete permission for the target object: when you perform authorization policy, action needs to be set to cos:DeleteObject. For more authorization, see business interfaces supporting CAM.
If you use the delete multiple objects API in an anonymous access scenario, you need to have the delete permission and batch delete permission for all target objects: when you perform authorization policy, action needs to be set to cos:DeleteObject and cos:DeleteMultipleObjects. For more authorization, see business interfaces supporting CAM.
If you use the delete multiple objects API in a non-anonymous access scenario, you need to have the delete permission for all target objects: when you perform authorization policy, action needs to be set to cos:DeleteObject. For more authorization, see business interfaces supporting CAM.

Related Examples

Function Name
Description
Example code
Delete an object.
Provides the feature of deleting a single object and deleting multiple objects.

Preliminary Preparation

Create CosAPI

Before the COS API is called, you must first create an instance of CosAPI to make subsequent call requests.
qcloud_cos::CosAPI InitCosAPI() {
uint64_t appid = 12500000000;
std::string region = "ap-guangzhou";// Region of the bucket, see https://www.tencentcloud.com/document/product/436/62?from_cn_redirect=1
std::string secret_id = "************************************"; // User's SecretId. It is recommended to use sub-account keys, with authorization following the least privilege principle to mitigate usage risks. For information on how to obtain sub-account keys, see https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1
std::string secret_key = "************************************"; // User's SecretKey. It is recommended to use sub-account keys, with authorization following the least privilege principle to mitigate usage risks. For information on how to obtain sub-account keys, see https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1
qcloud_cos::CosConfig config(appid, secret_id, secret_key, region);
qcloud_cos::CosAPI cos_tmp(config);
return cos_tmp;
}

Create CosAPI Using a Temporary Key

To access COS with a temporary key, you need to create a CosAPI instance using the temporary key.
qcloud_cos::CosAPI InitCosAPI() {
// You need to have obtained the temporary key results: tmp_secret_id, tmp_secret_key,
// For generating temporary keys, see https://www.tencentcloud.com/document/product/436/14048?from_cn_redirect=1#cos-sts-sdk
uint64_t appid = 12500000000;
std::string region = "ap-guangzhou";
std::string tmp_secret_id = "************************************";
std::string tmp_secret_key = "************************************";
std::string tmp_token = "token";
qcloud_cos::CosConfig config(appid, tmp_secret_id, tmp_secret_key, region);
config.SetTmpToken(tmp_token);
qcloud_cos::CosAPI cos_tmp(config);
return cos_tmp;
}

Use Cases

Deleting an Object

Note:
This Demo demonstrates how to use the COS C++ SDK to delete a single object.

Method Prototype

CosResult CosAPI::DeleteObject(const DeleteObjectReq& req, DeleteObjectResp* resp)

Request Example

void DeleteObjectDemo(qcloud_cos::CosAPI& cos) {
std::string object_name = "test.txt";
qcloud_cos::DeleteObjectReq req(bucket_name, object_name);
// req.SetXCosVersionId("xxxxx"); // You can specify the version to delete
qcloud_cos::DeleteObjectResp resp;
qcloud_cos::CosResult result = cos.DeleteObject(req, &resp);

std::cout << "===================DeleteObjectResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "============================================================" << std::endl;
}

Parameter Description

Parameter Name
Description
Type
req
Delete file request
DeleteObjectReq
resp
Delete file response
DeleteObjectResp
DeleteObjectReq Member or Function Description:
Member or Function
Description
Parameter Type
bucket_name
Bucket name, which can be set via the constructor or set method.
The naming format for buckets is BucketName-APPID. For details, see Naming Conventions
string
object_name
Object key (Key), which can be set via the constructor or set method.
is the unique identifier of the object in the bucket. For example, in the object access domain name examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/picture.jpg, the object key is doc/picture.jpg. For details, see Object Key
string
SetXCosVersionId
Set the version number of the object to be deleted. This is an optional setting and is only available when bucket versioning is enabled.
string
DeleteObjectResp Member Function Description:
Member functions
Description
Return Type
GetXCosRequestId
Obtain the request ID
string

Returning Description

CosResult main member functions are as follows:
Member functions
Description
Return Type
IsSucc
Indicates whether the operation is successful; returns true for success, false for failure.
bool
GetHttpStatus
Obtain the http status code.
int
GetErrorCode
The error code can be obtained when the request fails.
string
GetErrorMsg
Obtain the error message when the request fails.
string
GetXCosRequestId
Obtain the request ID.
string
Usage examples for CosResult are as follows. Users may choose to utilize them based on their needs:
void PrintResult(const qcloud_cos::CosResult& result, const qcloud_cos::BaseResp& resp) {
if (result.IsSucc()) {
std::cout << "Request Succ." << std::endl;
std::cout << resp.DebugString() << std::endl;
} else {
std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
std::cout << "HttpStatus=" << result.GetHttpStatus() << std::endl;
std::cout << "ErrorCode=" << result.GetErrorCode() << std::endl;
std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
std::cout << "ResourceAddr=" << result.GetResourceAddr() << std::endl;
std::cout << "XCosRequestId=" << result.GetXCosRequestId() << std::endl;
std::cout << "XCosTraceId=" << result.GetXCosTraceId() << std::endl;
}
}

Batch Delete Objects

Note:
This Demo demonstrates how to use the COS C++ SDK to delete objects in batches.
Whether the method is successful is determined by checking whether resp.GetErrorMsgs().empty().

Method Prototype

CosResult CosAPI::DeleteObjects(const DeleteObjectsReq& req, DeleteObjectsResp* resp)

Request Example

void DeleteObjectsDemo(qcloud_cos::CosAPI& cos) {
std::vector<ObjectVersionPair> to_be_deleted;
{
ObjectVersionPair pair;
std::string object_name = "test_dir/audio.mp3";
std::string version_id = ""; // If needed, you can specify the version to delete; if it is empty, no version is specified
to_be_deleted.push_back(ObjectVersionPair(object_name, version_id));
}
{
std::string object_name = "test_dir/video.mp4";
std::string version_id = ""; // If needed, you can specify the version to delete; if it is empty, no version is specified
to_be_deleted.push_back(ObjectVersionPair(object_name, version_id));
}

qcloud_cos::DeleteObjectsReq req(bucket_name, to_be_deleted);
// req.SetQuiet(); // When enabled in Quiet mode, no information about successfully deleted objects is returned; defaults to Verbose mode
qcloud_cos::DeleteObjectsResp resp;
qcloud_cos::CosResult result = cos.DeleteObjects(req, &resp);

std::cout << "===================DeleteObjectsResponse=====================" << std::endl;
std::vector<DeletedInfo> deleted_infos = resp.GetDeletedInfos(); // Entries of successfully deleted individual objects; this element is returned only in Verbose mode
std::vector<ErrorInfo> error_infos = resp.GetErrorMsgs(); // Entries for objects with failed deletions
if (!error_infos.empty()) {
std::cout << "==================Failed part message==================" << std::endl;
for (ErrorInfo& error_info : error_infos) {
std::cout << "key: " << error_info.m_key << "\\ncode: " << error_info.m_code << "\\nmessage: " << error_info.m_message << std::endl;
std::cout << "====================================" << std::endl;
}
} else {
std::cout << "DeleteObjects All Succ." << std::endl;
}
std::cout << "=============================================================" << std::endl;
}

Parameter Description

Parameter Name
Description
Type
req
Batch delete request
DeleteObjectsReq
resp
Batch delete response
DeleteObjectsResp
DeleteObjectsReq Member or Function Description:
Member or Function
Description
Parameter Type
bucket_name
Bucket name, which can be set via the constructor or set method.
The naming format for buckets is BucketName-APPID. For details, see Naming Conventions
string
m_objvers
List of objects to be deleted. For specific usage, refer to the example. To delete a specific version of an object, set the version_id in ObjectVersionPair to non-empty.
vector<ObjectVersionPair>
SetQuiet
Specifies that the deletion result is returned in quiet mode, meaning only error messages for failed deletions are returned. If not enabled, all information for both successful and failed deletions is returned.
None
DeleteObjectsResp Member Function Description:
Member functions
Description
Return Type
GetDeletedInfos
Get the entries of successfully deleted individual objects; this element is returned only in Verbose mode (that is, when SetQuiet is not enabled).
vector<DeletedInfo>
GetErrorMsgs
Obtain the entries of objects that failed deletion. For specific usage, refer to the example.
vector<ErrorInfo>
GetXCosRequestId
Obtain the request ID
string

Returning Description

CosResult main member functions are as follows:
Member functions
Description
Return Type
IsSucc
Indicates whether the operation is successful; returns true for success, false for failure.
bool
GetHttpStatus
Obtain the http status code.
int
GetErrorCode
The error code can be obtained when the request fails.
string
GetErrorMsg
Obtain the error message when the request fails.
string
GetXCosRequestId
Obtain the request ID.
string
Usage examples for CosResult are as follows. Users may choose to utilize them based on their needs:
void PrintResult(const qcloud_cos::CosResult& result, const qcloud_cos::BaseResp& resp) {
if (result.IsSucc()) {
std::cout << "Request Succ." << std::endl;
std::cout << resp.DebugString() << std::endl;
} else {
std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
std::cout << "HttpStatus=" << result.GetHttpStatus() << std::endl;
std::cout << "ErrorCode=" << result.GetErrorCode() << std::endl;
std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
std::cout << "ResourceAddr=" << result.GetResourceAddr() << std::endl;
std::cout << "XCosRequestId=" << result.GetXCosRequestId() << std::endl;
std::cout << "XCosTraceId=" << result.GetXCosTraceId() << std::endl;
}
}

API Operations

For the API documentation on deleting a single object, see the DELETE Object document.
For the API documentation on deleting multiple objects, see the DELETE Multiple Objects document.

Ajuda e Suporte

Esta página foi útil?

comentários