tencent cloud

List Objects
Last updated:2026-03-05 16:57:24
List Objects
Last updated: 2026-03-05 16:57:24

Introduction

This article introduces the sample code and description for implementing the listing objects feature via the COS C++ SDK.

Must-Knows

If you want to use the listing objects feature, you need to have read permission for the target bucket. When configuring the authorization policy, set action to cos:GetBucket. For more authorization, see CAM-supported business APIs.

Related Examples

Function Name
Description
Example code
List Objects
Provides the feature to query partial or all objects in a bucket.

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

List Objects in the Bucket

Note:
This Demo demonstrates how to use the COS C++ SDK to list objects in a bucket.
Request parameters such as the prefix for listing objects can be specified. See how to set it in the request body below.

Method Prototype

CosResult CosAPI::GetBucket(const GetBucketReq& req, GetBucketResp* resp)

Request Example

void GetBucketDemo(qcloud_cos::CosAPI& cos) {
qcloud_cos::GetBucketReq req(bucket_name);
// Set the listed object names to be prefixed with 'prefix'
req.SetPrefix("test");
// Set the maximum number of objects to list. A single listobject operation supports up to 1000
req.SetMaxKeys(10);
qcloud_cos::GetBucketResp resp;
qcloud_cos::CosResult result = cos.GetBucket(req, &resp);
std::cout << "===================GetBucketResponse=====================" << std::endl;
if (result.IsSucc()) {
// object contents represent the list of objects in this listing
std::vector<Content> contents = resp.GetContents();
for (const Content& content : contents) {
// key of the object
std::string key = content.m_key;
// etag of the object
std::string etag = content.m_etag;
// Length of the object
std::string file_size = content.m_size;
// Storage type of the object
std::string storage_classes = content.m_storage_class;
std::cout << "key:" << key << "\\netag:" << etag << "\\nfile_size:" << file_size << "\\nstorage_classes:" << storage_classes << std::endl;
std::cout << "==================================" << std::endl;
}

if (resp.IsTruncated()) {
// Indicates that the listing is incomplete and has been truncated
// Next starting position
std::string next_marker = resp.GetNextMarker();
std::cout << "next_marker:" << next_marker << std::endl;
}
} else {
std::cout << "GetBucket Fail, ErrorMsg: " << result.GetErrorMsg() << std::endl;
}
std::cout << "=========================================================" << std::endl;
}

Parameter Description

Parameter Name
Description
Type
req
List Objects Request
GetBucketReq
resp
List Objects Response
GetBucketResp
GetBucketReq 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
SetPrefix
Limits the returned result objects to those with the specified prefix. If not called, no restriction is applied, meaning all members under the Bucket.
string
SetDelimiter
Delimiter, limits the returned path to those that start with prefix and end with the first occurrence of delimiter
string
SetMarker
Mark the starting point of the list. It can be set to empty for the first time. Subsequent requests need to be set to the nextMarker in the previous listObjects return value.
string
SetMaxKeys
Maximum number of members to return (must not exceed 1000). Default value: 1000.
uint64_t
GetBucketResp Member Function Description:
Member functions
Description
Return Type
GetContents
Obtain the object list for this listing. For specific usage, refer to the request example above.
vector<Content>
IsTruncated
Whether the response is truncated; returns true if it is truncated, otherwise false.
bool
GetNextMarker
This node is returned only when the response is truncated (IsTruncated is true). Its value is the last object key in the current response entries. When you need to request subsequent entries, pass the value of this node as the marker parameter in the next request.
string
GetCommonPrefixes
The identical part from the prefix or from the beginning (if prefix is not specified) to the first delimiter is defined as a Common Prefix. This field is returned only when the delimiter parameter is specified in the request.
vector<std::string>
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 description of listing objects, see the List Objects document.
Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback