Release Notes
Announcements
cos:GetObject, for more authorization, see CAM-supported business APIs. Function Name | Description | Example code |
high-level APIs | High-level APIs use multithreading to concurrently download multiple chunks based on range, and only support downloading to files. There are two types: one is the multithreaded version, and the other is the multithreaded resumable download version. | |
simple APIs | The GET Object API can implement streaming downloads and download objects to local files, but does not support the resumable download feature. | |
Asynchronous high-level APIs | Asynchronous high-level APIs encapsulate the high-level APIs, using multithreading to concurrently download multiple chunks based on range, and only support downloading to files. There are two types: one is the multithreaded version, and the other is the multithreaded resumable download version. They support progress callback and download status callback features. | |
Asynchronous simple APIs | Asynchronous high-level APIs encapsulate simple APIs, using multithreading to concurrently download multiple chunks based on range, and only support downloading to files. They support progress callback and download status callback features, but do not support multithreading and resumable download features. |
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=1std::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=1std::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=1qcloud_cos::CosConfig config(appid, secret_id, secret_key, region);qcloud_cos::CosAPI cos_tmp(config);return cos_tmp;}
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-sdkuint64_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;}
CosResult CosAPI::MultiGetObject(const MultiGetObjectReq& req, MultiGetObjectResp* resp)
void MultiGetObjectDemo(qcloud_cos::CosAPI& cos) {std::string file_path = "test_file/big_file.txt";std::string object_name = "big_file.txt";// This configuration is global. Once explicitly set, all subsequent APIs involving multi-threaded downloads will use this configuration.CosSysConfig::SetDownThreadPoolSize(10); // Download thread pool size, defaulting to 10CosSysConfig::SetDownSliceSize(4 * 1024 * 1024); // Download range size, default 4Mqcloud_cos::MultiGetObjectReq req(bucket_name, object_name, file_path);qcloud_cos::MultiGetObjectResp resp;qcloud_cos::CosResult result = cos.MultiGetObject(req, &resp);std::cout << "===================GetObjectResponse=====================" << std::endl;PrintResult(result, resp);std::cout << "=========================================================" << std::endl;}
Parameter Name | Description | Type |
req | Download file request | MultiGetObjectReq |
resp | File download response | MultiGetObjectResp |
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 |
local_file_path | Local file path, which can be set via the constructor or set method. | string |
SetTrafficLimit | Used for traffic control of download objects, unit: bit/s. By default, no traffic control is performed. | string |
Member functions | Description | Return Type |
GetXCosMetas | Obtain the custom metadata map. | map<std::string, std::string> |
GetXCosMeta | Obtain the specified custom metadata. | string |
GetXCosServerSideEncryption | Obtain the server-side encryption algorithm used | string |
GetEtag | Obtain the Etag of the stored download object. | string |
GetXCosRequestId | Obtain the request ID. | string |
Member functions | Description | Parameter Type |
SetDownSliceSize | Set the part size for part download, unit: byte (Byte), default: 4MB. The setting method is CosSysConfig::SetDownSliceSize(4 * 1024 * 1024). | uint64_t |
SetDownThreadPoolSize | Set the download thread pool size, defaulting to 10. The setting method is CosSysConfig::SetDownThreadPoolSize(10). | unsigned |
Member functions | Description | Return Type |
IsSucc | Indicates whether the operation is successful; returns true for success, false for failure. | bool |
GetHttpStatus | Obtains the HTTP status code. | int |
GetErrorCode | Obtain the error code when the request fails. | string |
GetErrorMsg | Obtain the error message when the request fails. | string |
GetXCosRequestId | Obtain the request ID. | string |
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;}}
CosResult CosAPI::ResumableGetObject(const GetObjectByFileReq& req, GetObjectByFileResp* resp)
void ResumableGetObjectDemo(qcloud_cos::CosAPI& cos) {std::string file_path = "test_file/big_file.txt";std::string object_name = "big_file.txt";qcloud_cos::GetObjectByFileReq req(bucket_name, object_name, file_path);qcloud_cos::GetObjectByFileResp resp;CosResult result = cos.ResumableGetObject(req, &resp);std::cout << "===================ResumableGetObject====================" << std::endl;PrintResult(result, resp);std::cout << "=========================================================" << std::endl;}
Parameter Name | Description | Type |
req | Download file request | GetObjectByFileReq |
resp | File download response | GetObjectByFileResp |
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 |
local_file_path | Local file path, which can be set via the constructor or set method. | string |
SetTrafficLimit | Used for traffic control of download objects, unit: bit/s. By default, no traffic control is performed. | uint64_t |
Member functions | Description | Return Type |
GetXCosMetas | Obtain the custom metadata map. | map<std::string, std::string> |
GetXCosMeta | Obtain the specified custom metadata. | string |
GetXCosServerSideEncryption | Obtain the server-side encryption algorithm used | string |
GetEtag | Obtain the Etag of the stored download object. | string |
GetXCosRequestId | Obtain the request ID. | string |
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 |
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;}}
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=1std::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=1std::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=1qcloud_cos::CosConfig config(appid, secret_id, secret_key, region);qcloud_cos::CosAPI cos_tmp(config);return cos_tmp;}
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-sdkuint64_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;}
CosResult CosAPI::GetObject(const GetObjectByFileReq& req, GetObjectByFileResp* resp)
void GetObjectByFileDemo(qcloud_cos::CosAPI& cos) {std::string object_name = "test_src.txt";std::string file_path = "./test_file/text2.txt";qcloud_cos::GetObjectByFileReq req(bucket_name, object_name, file_path);// Download objects with rate limiting. The default unit is bit/s, and the rate limit value can be set in the range of 819200 - 838860800// uint64_t traffic_limit = 0;// req.SetTrafficLimit(traffic_limit);qcloud_cos::GetObjectByFileResp resp;qcloud_cos::CosResult result = cos.GetObject(req, &resp);std::cout << "===================GetObjectResponse=====================" << std::endl;PrintResult(result, resp);std::cout << "=========================================================" << std::endl;}
Parameter Name | Description | Type |
req | Download file request | GetObjectByFileReq |
resp | File download response | GetObjectByFileResp |
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 |
local_file_path | Local file path, which can be set via the constructor or set method. | string |
SetTrafficLimit | Set single-connection speed limit. The setting method is req.SetTrafficLimit(819200). The default unit is bit/s, and the speed limit value can be set in the range of 819200 - 838860800. | uint64_t |
Member functions | Description | Return Type |
GetXCosMetas | Obtain the custom metadata map | map<std::string, std::string> |
GetXCosMeta | Obtain the specified custom metadata. | string |
GetXCosServerSideEncryption | Obtain the server-side encryption algorithm used | string |
GetEtag | Obtain the Etag of the stored download object. | string |
GetXCosRequestId | Obtain the request ID. | string |
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 |
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;}}
CosResult CosAPI::GetObject(const GetObjectByStreamReq& req, GetObjectByStreamResp* resp)
void GetObjectByStreamDemo(qcloud_cos::CosAPI& cos) {std::string object_name = "test.txt";std::ostringstream os;qcloud_cos::GetObjectByStreamReq req(bucket_name, object_name, os);// Download objects with rate limiting. The default unit is bit/s, and the rate limit value can be set in the range of 819200 - 838860800// uint64_t traffic_limit = 0;// req.SetTrafficLimit(traffic_limit);qcloud_cos::GetObjectByStreamResp resp;qcloud_cos::CosResult result = cos.GetObject(req, &resp);std::cout << "===================GetObjectResponse=====================" << std::endl;PrintResult(result, resp);std::cout << "=========================================================" << std::endl;std::cout << os.str() << std::endl;}
Parameter Name | Description | Type |
req | Download file request | GetObjectByStreamReq |
resp | File download response | GetObjectByStreamResp |
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 |
os | Output stream, which can be set via the constructor. | ostream |
SetTrafficLimit | Set single-connection speed limit. The setting method is req.SetTrafficLimit(819200). The default unit is bit/s, and the speed limit value can be set in the range of 819200 - 838860800. | uint64_t |
Member functions | Description | Return Type |
GetXCosMetas | Obtain the custom metadata map. | map<std::string, std::string> |
GetXCosMeta | Obtain the specified custom metadata. | string |
GetXCosServerSideEncryption | Obtain the server-side encryption algorithm used | string |
GetEtag | Obtain the Etag of the stored download object. | string |
GetXCosRequestId | Obtain the request ID. | string |
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 |
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;}}
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=1std::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=1std::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=1qcloud_cos::CosConfig config(appid, secret_id, secret_key, region);qcloud_cos::CosAPI cos_tmp(config);return cos_tmp;}
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-sdkuint64_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;}
SharedAsyncContext CosAPI::AsyncMultiGetObject(const AsyncMultiGetObjectReq& req)
/** This method is a sample progress callback for asynchronous object download*/void ProgressCallback(uint64_t transferred_size, uint64_t total_size, void* user_data) {qcloud_cos::ObjectReq* req = static_cast<qcloud_cos::ObjectReq*>(user_data);if (0 == transferred_size % 1048576) {std::cout << "ObjectName:" << req->GetObjectName() << ", TranferedSize:" << transferred_size << ",TotalSize:" << total_size << std::endl;}}/** This method is a sample of a completion callback for asynchronous object download*/void GetObjectAsyncDoneCallback(const SharedAsyncContext& context, void* user_data) {UNUSED_PARAM(user_data)std::cout << "AsyncMultiGetObjectDemoCallback, BucketName:"<< context->GetBucketName()<< ", ObjectName:" << context->GetObjectName()<< ", LocalFile:" << context->GetLocalFilePath() << std::endl;// The response corresponding to qcloud_cos::MultiGetObjectReq is qcloud_cos::GetObjectByFileRespif (context->GetResult().IsSucc()) {// Obtain the responsestd::cout << "GetObject succeed" << std::endl;std::cout << "Result:" << context->GetResult().DebugString() << std::endl;AsyncResp resp = context->GetAsyncResp();std::cout << "ETag:" << resp.GetEtag() << std::endl;std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;} else {std::cout << "GetObject failed" << std::endl;std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;}}void AsyncMultiGetObjectDemo(qcloud_cos::CosAPI& cos) {CosSysConfig::SetAsynThreadPoolSize(2); // Set asynchronous thread pool size, defaulting to 2std::string local_file = "test_file/big_file.txt";std::string object_name = "big_file.txt";qcloud_cos::AsyncMultiGetObjectReq req(bucket_name, object_name, local_file);// Set the download progress callbackreq.SetTransferProgressCallback(&ProgressCallback);// Set the download status callbackreq.SetDoneCallback(&GetObjectAsyncDoneCallback);// Set private data, which corresponds to user_data in the callbackreq.SetUserData(&req);// Begin downloadSharedAsyncContext context = cos.AsyncMultiGetObject(req);std::cout << "===================AsyncMultiGetObject======================" << std::endl;// Wait for the download to completestd::cout << "wait finish..." << std::endl;context->WaitUntilFinish();// Check the resultif (context->GetResult().IsSucc()) {// Obtain the responsestd::cout << "AsyncMultiGetObject succeed" << std::endl;std::cout << "Result:" << context->GetResult().DebugString() << std::endl;AsyncResp resp = context->GetAsyncResp();std::cout << "ETag:" << resp.GetEtag() << std::endl;std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;} else {std::cout << "AsyncMultiGetObject failed" << std::endl;std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;}std::cout << "============================================================" << std::endl;}
Parameter Name | Description | Type |
req | Download file request | AsyncMultiGetObjectReq |
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 |
local_file_path | Local file path, which can be set via the constructor or set method. | string |
SetTransferProgressCallback | Set the download progress callback method | TransferProgressCallback |
SetDoneCallback | Set the download progress status method | DoneCallback |
Member functions | Description | Parameter Type |
SetAsynThreadPoolSize | Set the size of the asynchronous thread pool, defaulting to 2. The setting method is CosSysConfig::SetAsyncThreadPoolSize(2). | unsigned |
Member functions | Description | Return Type |
WaitUntilFinish | Wait for the download to complete. | None |
GetResult | Obtain the Result. | CosResult |
GetAsyncResp | Obtain the Resp response. | AsyncResp |
Member functions | Description | Return Type |
GetXCosHashCrc64Ecma | Obtain the CRC64 of the stored download object. | string |
GetEtag | Obtain the Etag of the stored download object. | string |
GetXCosRequestId | Obtain the request ID. | string |
Member functions | Description | Return Type |
IsSucc | Indicates whether the operation is successful; returns true for success, false for failure. | bool |
GetHttpStatus | Obtains the HTTP status code. | int |
GetErrorCode | Obtain the error code when the request fails. | string |
GetErrorMsg | Obtain the error message when the request fails. | string |
GetXCosRequestId | Obtain the request ID. | string |
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;}}
SharedAsyncContext CosAPI::AsyncResumableGetObject(const AsyncGetObjectReq& req)
/** This method is a sample progress callback for asynchronous object download*/void ProgressCallback(uint64_t transferred_size, uint64_t total_size, void* user_data) {qcloud_cos::ObjectReq* req = static_cast<qcloud_cos::ObjectReq*>(user_data);if (0 == transferred_size % 1048576) {std::cout << "ObjectName:" << req->GetObjectName() << ", TranferedSize:" << transferred_size << ",TotalSize:" << total_size << std::endl;}}/** This method is a sample of a completion callback for asynchronous object download*/void GetObjectAsyncDoneCallback(const SharedAsyncContext& context, void* user_data) {UNUSED_PARAM(user_data)std::cout << "AsyncResumableGetObjectDemoCallback, BucketName:"<< context->GetBucketName()<< ", ObjectName:" << context->GetObjectName()<< ", LocalFile:" << context->GetLocalFilePath() << std::endl;// The response corresponding to qcloud_cos::MultiGetObjectReq is qcloud_cos::GetObjectByFileRespif (context->GetResult().IsSucc()) {// Obtain the responsestd::cout << "GetObject succeed" << std::endl;std::cout << "Result:" << context->GetResult().DebugString() << std::endl;AsyncResp resp = context->GetAsyncResp();std::cout << "ETag:" << resp.GetEtag() << std::endl;std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;} else {std::cout << "GetObject failed" << std::endl;std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;}}void AsyncResumableGetObjectDemo(qcloud_cos::CosAPI& cos) {std::string local_file = "test_file/big_file.txt";std::string object_name = "big_file.txt";qcloud_cos::AsyncGetObjectReq req(bucket_name, object_name, local_file);// Set the download progress callbackreq.SetTransferProgressCallback(&ProgressCallback);// Set the download status callbackreq.SetDoneCallback(&GetObjectAsyncDoneCallback);// Set private data, which corresponds to user_data in the callbackreq.SetUserData(&req);// Begin downloadSharedAsyncContext context = cos.AsyncResumableGetObject(req);std::cout << "===================AsyncResumableGetObject======================" << std::endl;// Wait for the download to completestd::cout << "wait finish..." << std::endl;context->WaitUntilFinish();// Check the resultif (context->GetResult().IsSucc()) {// Obtain the responsestd::cout << "AsyncResumableGetObject succeed" << std::endl;std::cout << "Result:" << context->GetResult().DebugString() << std::endl;AsyncResp resp = context->GetAsyncResp();std::cout << "ETag:" << resp.GetEtag() << std::endl;std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;} else {std::cout << "AsyncResumableGetObject failed" << std::endl;std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;}std::cout << "================================================================" << std::endl;}
Parameter Name | Description | Type |
req | Download file request | AsyncGetObjectReq |
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 |
local_file_path | Local file path, which can be set via the constructor or set method. | string |
SetTransferProgressCallback | Set the download progress callback method. | TransferProgressCallback |
SetDoneCallback | Set the download progress status method. | DoneCallback |
Member functions | Description | Parameter Type |
SetAsynThreadPoolSize | Set the size of the asynchronous thread pool, defaulting to 2. The setting method is CosSysConfig::SetAsyncThreadPoolSize(2). | unsigned |
Member functions | Description | Return Type |
WaitUntilFinish | Wait for the download to complete. | None |
GetResult | Obtain the Result. | CosResult |
GetAsyncResp | Obtain the Resp response. | AsyncResp |
Member functions | Description | Return Type |
GetXCosHashCrc64Ecma | Obtain the CRC64 of the stored download object. | string |
GetEtag | Obtain the Etag of the stored download object. | string |
GetXCosRequestId | Obtain the request ID. | string |
Member functions | Description | Return Type |
IsSucc | Indicates whether the operation is successful; returns true for success, false for failure. | bool |
GetHttpStatus | Obtains the HTTP status code. | int |
GetErrorCode | Obtain the error code when the request fails. | string |
GetErrorMsg | Obtain the error message when the request fails. | string |
GetXCosRequestId | Obtain the request ID. | string |
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;}}
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=1std::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=1std::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=1qcloud_cos::CosConfig config(appid, secret_id, secret_key, region);qcloud_cos::CosAPI cos_tmp(config);return cos_tmp;}
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-sdkuint64_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;}
SharedAsyncContext CosAPI::AsyncGetObject(const AsyncGetObjectReq& req)
/** This method is a sample progress callback for asynchronous object download*/void ProgressCallback(uint64_t transferred_size, uint64_t total_size, void* user_data) {qcloud_cos::ObjectReq* req = static_cast<qcloud_cos::ObjectReq*>(user_data);if (0 == transferred_size % 1048576) {std::cout << "ObjectName:" << req->GetObjectName() << ", TranferedSize:" << transferred_size << ",TotalSize:" << total_size << std::endl;}}/** This method is a sample of a completion callback for asynchronous object download*/void GetObjectAsyncDoneCallback(const SharedAsyncContext& context, void* user_data) {UNUSED_PARAM(user_data)std::cout << "AsyncGetObjectDemoCallback, BucketName:"<< context->GetBucketName()<< ", ObjectName:" << context->GetObjectName()<< ", LocalFile:" << context->GetLocalFilePath() << std::endl;// The response corresponding to qcloud_cos::MultiGetObjectReq is qcloud_cos::GetObjectByFileRespif (context->GetResult().IsSucc()) {// Obtain the responsestd::cout << "GetObject succeed" << std::endl;std::cout << "Result:" << context->GetResult().DebugString() << std::endl;AsyncResp resp = context->GetAsyncResp();std::cout << "ETag:" << resp.GetEtag() << std::endl;std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;} else {std::cout << "GetObject failed" << std::endl;std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;}}void AsyncGetObjectDemo(qcloud_cos::CosAPI& cos) {std::string local_file = "test_file/text.txt";std::string object_name = "text.txt";qcloud_cos::AsyncGetObjectReq req(bucket_name, object_name, local_file);req.SetRecvTimeoutInms(1000 * 60);// Set the download progress callbackreq.SetTransferProgressCallback(&ProgressCallback);// Set the download status callbackreq.SetDoneCallback(&GetObjectAsyncDoneCallback);// Set private data, which corresponds to user_data in the callbackreq.SetUserData(&req);// Begin downloadSharedAsyncContext context = cos.AsyncGetObject(req);std::cout << "===================AsyncGetObject======================" << std::endl;// Wait for the download to completestd::cout << "wait finish..." << std::endl;context->WaitUntilFinish();// Check the resultif (context->GetResult().IsSucc()) {// Obtain the responsestd::cout << "AsyncGetObject succeed" << std::endl;std::cout << "Result:" << context->GetResult().DebugString() << std::endl;AsyncResp resp = context->GetAsyncResp();std::cout << "ETag:" << resp.GetEtag() << std::endl;std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;} else {std::cout << "AsyncGetObject failed" << std::endl;std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;}std::cout << "=======================================================" << std::endl;}
Parameter Name | Description | Type |
req | Download file request | AsyncGetObjectReq |
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 |
local_file_path | Local file path, which can be set via the constructor or set method. | string |
SetTransferProgressCallback | Set the download progress callback method. | TransferProgressCallback |
SetDoneCallback | Set the download progress status method. | DoneCallback |
Member functions | Description | Return Type |
WaitUntilFinish | Wait for the download to complete. | None |
GetResult | Obtain the Result. | CosResult |
GetAsyncResp | Obtain the Resp response. | AsyncResp |
Member functions | Description | Return Type |
GetXCosHashCrc64Ecma | Obtain the CRC64 of the stored download object. | string |
GetEtag | Obtain the Etag of the stored download object. | string |
GetXCosRequestId | Obtain the request ID. | string |
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 |
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;}}
Was this page helpful?
You can also Contact sales or Submit a Ticket for help.
Help us improve! Rate your documentation experience in 5 mins.
Feedback