tencent cloud

Cloud Object Storage

Check Bucket Existence

PDF
Modo Foco
Tamanho da Fonte
Última atualização: 2026-03-05 16:46:21

Introduction

This document provides sample code for quickly checking whether a bucket exists.
sample code actually calls the COS API HeadBucket, which is a simplified version of this API.
In addition to checking whether a bucket exists, HeadBucket can also determine whether access to the bucket is permitted, with the following cases:
The bucket exists and has read access, and the HTTP status code 200 is returned.
No read access to the bucket, returns HTTP status code 403.
The bucket does not exist, returns HTTP status code 404.

Must-Knows

Before performing bucket-related operations, you need to have the relevant permissions:
If you want to search for a bucket, when the authorization policy is set up, action needs to be set to cos:HeadBucket. For more authorization, see business APIs that support CAM.

Related Examples

Function Name
Description
Example code
Check Bucket Existence
Check Bucket Existence

Preliminary Preparation

Using Permanent Key to Create CosAPI

Before calling the COS API, you must first create an instance of CosAPI for 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 Case: Check if the Bucket Exists

Check Bucket existence via the convenience API provided by the SDK.

Method Prototype

bool CosAPI::IsBucketExist(const std::string& bucket_name)

Request Example

void IsBucketExist(qcloud_cos::CosAPI& cos) {
std::cout << "===================IsBucketExist====================="
<< std::endl;
std::cout << (cos.IsBucketExist("abcdefg") ? "true" : "false") << std::endl;
std::cout << (cos.IsBucketExist(bucket_name) ? "true" : "false") << std::endl;
std::cout
<< "===================================================================="
<< std::endl;
}

Parameter Description

Parameter Name
Description
Type
bucket_name
Bucket Name
string

Returned Result Description

Returns a bool value: true indicates existence, false indicates non-existence.

Ajuda e Suporte

Esta página foi útil?

comentários