tencent cloud

Cloud Object Storage

Verify Object Existence

PDF
Focus Mode
Font Size
Last updated: 2026-03-05 16:59:11

Introduction

This article introduces the sample code and description for quickly checking whether an object exists in a bucket using the COS C++ SDK. The sample code actually calls the HEAD Object COS API, which is a simplified version of this interface.

Must-Knows

If you want to determine whether an object exists, you need to have read permission for the target object: when you configure the authorization policy, the action needs to be set to cos:HeadObject. For more authorization, see business APIs supporting CAM.

Related Examples

Function Name
Description
Example code
Verify Object Existence
Provides the feature to determine whether an object exists.

Preliminary Preparation

Create CosAPI

Before calling the COS API, 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

Check Whether the Object Exists

By calling the query object metadata API, you can check if an object exists.

Method Prototype

bool CosAPI::IsObjectExist(const std::string& bucket_name, const std::string& object_name)

Request Example

void IsObjectExistDemo(qcloud_cos::CosAPI& cos) {
bool is_exist = cos.IsObjectExist(bucket_name, "test.txt");

std::cout << "=====================IsObjectExist=======================" << std::endl;
std::cout << (is_exist ? "true" : "false") << std::endl;
std::cout << "=========================================================" << std::endl;
}

Parameter Description

Parameter Name
Description
Type
bucket_name
The naming format of a Bucket is BucketName-APPID. For details, see Naming Conventions
string
object_name
An object key is a unique identifier for an object in a 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

Return Result Description

Success: Returns a boolean value, where true indicates existence and false indicates non-existence.

API Operations

For the API description related to determining whether an object exists, see the HEAD Object document.


Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback