Skip to content

Commit 8ced1ae

Browse files
author
jackyding
committed
optimize async api
1 parent f1afd5f commit 8ced1ae

26 files changed

+995
-646
lines changed

cos-cpp-sdk.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,7 @@ CosResult GetObject(const GetObjectByFileReq& req, GetObjectByFileResp* resp);
12541254
CosResult GetObject(const GetObjectByStreamReq& req, GetObjectByStreamResp* resp);
12551255
12561256
// 将Object下载到本地文件中(多线程)
1257-
CosResult GetObject(const MultiGetObjectReq& req, MultiGetObjectResp* resp);
1257+
CosResult GetObject(const MultiGetObjectReq& req, GetObjectByFileResp* resp);
12581258
```
12591259

12601260
#### 参数说明
@@ -1289,7 +1289,7 @@ void SetTrafficLimitByParam(const std::string& str);
12891289
void SetTrafficLimitByHeader(const std::string& str);
12901290
```
12911291
1292-
- resp —— GetObjectByFileResp/GetObjectByStreamResp/MultiGetObjectResp GetObject操作的返回
1292+
- resp —— GetObjectByFileResp/GetObjectByStreamResp/GetObjectByFileResp GetObject操作的返回
12931293
12941294
GetObjectResp除了读取公共头部的成员函数外,还提供以下成员函数:
12951295
@@ -1351,10 +1351,10 @@ std::string local_path = "/tmp/object_name";
13511351
{
13521352
// request需要提供appid、bucketname、object,以及本地的路径(包含文件名)
13531353
qcloud_cos::MultiGetObjectReq req(bucket_name, object_name, local_path);
1354-
qcloud_cos::MultiGetObjectResp resp;
1354+
qcloud_cos::GetObjectByFileResp resp;
13551355
qcloud_cos::CosResult result = cos.GetObject(req, &resp);
13561356
if (result.IsSucc()) {
1357-
// 下载成功,可以调用MultiGetObjectResp的成员函数
1357+
// 下载成功,可以调用GetObjectByFileResp的成员函数
13581358
} else {
13591359
// 下载失败,可以调用CosResult的成员函数输出错误信息,比如requestID等
13601360
}

demo/cos_demo.cpp

Lines changed: 30 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -395,14 +395,9 @@ void PutObjectByFileLimitTraffic(qcloud_cos::CosAPI& cos,
395395
const std::string& bucket_name,
396396
const std::string& object_name,
397397
const std::string& file_path,
398-
const uint64_t traffic_limit,
399-
bool set_header = true) {
398+
const uint64_t traffic_limit) {
400399
qcloud_cos::PutObjectByFileReq req(bucket_name, object_name, file_path);
401-
if (set_header) {
402-
req.SetTrafficLimitByHeader(StringUtil::Uint64ToString(traffic_limit));
403-
} else {
404-
req.SetTrafficLimitByParam(StringUtil::Uint64ToString(traffic_limit));
405-
}
400+
req.SetTrafficLimit(traffic_limit);
406401
qcloud_cos::PutObjectByFileResp resp;
407402
qcloud_cos::CosResult result = cos.PutObject(req, &resp);
408403
std::cout << "===================PutObjectFileLimitTrafficResponse==========="
@@ -463,14 +458,9 @@ void GetObjectByFileLimitTraffic(qcloud_cos::CosAPI& cos,
463458
const std::string& bucket_name,
464459
const std::string& object_name,
465460
const std::string& file_path,
466-
const uint64_t traffic_limit,
467-
bool set_header = true) {
461+
const uint64_t traffic_limit) {
468462
qcloud_cos::GetObjectByFileReq req(bucket_name, object_name, file_path);
469-
if (set_header) {
470-
req.SetTrafficLimitByHeader(StringUtil::Uint64ToString(traffic_limit));
471-
} else {
472-
req.SetTrafficLimitByParam(StringUtil::Uint64ToString(traffic_limit));
473-
}
463+
req.SetTrafficLimit(traffic_limit);
474464
qcloud_cos::GetObjectByFileResp resp;
475465

476466
qcloud_cos::CosResult result = cos.GetObject(req, &resp);
@@ -504,7 +494,7 @@ void MultiGetObject(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
504494
qcloud_cos::MultiGetObjectReq req(bucket_name, object_name, file_path);
505495
qcloud_cos::MultiGetObjectResp resp;
506496

507-
qcloud_cos::CosResult result = cos.GetObject(req, &resp);
497+
qcloud_cos::CosResult result = cos.MultiGetObject(req, &resp);
508498
std::cout << "===================GetObjectResponse====================="
509499
<< std::endl;
510500
PrintResult(result, resp);
@@ -517,16 +507,11 @@ void MultiGetObjectLimitTraffic(qcloud_cos::CosAPI& cos,
517507
const std::string& bucket_name,
518508
const std::string& object_name,
519509
const std::string& file_path,
520-
const uint64_t traffic_limit,
521-
bool set_header = true) {
510+
const uint64_t traffic_limit) {
522511
qcloud_cos::MultiGetObjectReq req(bucket_name, object_name, file_path);
523-
if (set_header) {
524-
req.SetTrafficLimitByHeader(StringUtil::Uint64ToString(traffic_limit));
525-
} else {
526-
req.SetTrafficLimitByParam(StringUtil::Uint64ToString(traffic_limit));
527-
}
512+
req.SetTrafficLimit(traffic_limit);
528513
qcloud_cos::MultiGetObjectResp resp;
529-
qcloud_cos::CosResult result = cos.GetObject(req, &resp);
514+
qcloud_cos::CosResult result = cos.MultiGetObject(req, &resp);
530515
std::cout << "===================MultiGetObjectLimitTrafficResponse=========="
531516
"==========="
532517
<< std::endl;
@@ -571,15 +556,10 @@ void UploadPartDataLimitTraffic(qcloud_cos::CosAPI& cos,
571556
const std::string& object_name,
572557
const std::string& upload_id, std::fstream& is,
573558
uint64_t number, std::string* etag,
574-
const uint64_t traffic_limit,
575-
bool set_header = true) {
559+
const uint64_t traffic_limit) {
576560
qcloud_cos::UploadPartDataReq req(bucket_name, object_name, upload_id, is);
577561
req.SetPartNumber(number);
578-
if (set_header) {
579-
req.SetTrafficLimitByHeader(StringUtil::Uint64ToString(traffic_limit));
580-
} else {
581-
req.SetTrafficLimitByParam(StringUtil::Uint64ToString(traffic_limit));
582-
}
562+
req.SetTrafficLimit(traffic_limit);
583563
qcloud_cos::UploadPartDataResp resp;
584564
qcloud_cos::CosResult result = cos.UploadPartData(req, &resp);
585565
*etag = resp.GetEtag();
@@ -629,7 +609,7 @@ void MultiUploadObject(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
629609
qcloud_cos::MultiPutObjectReq req(bucket_name, object_name, local_file);
630610
req.SetRecvTimeoutInms(1000 * 60);
631611
qcloud_cos::MultiPutObjectResp resp;
632-
qcloud_cos::CosResult result = cos.PutObject(req, &resp);
612+
qcloud_cos::CosResult result = cos.MultiPutObject(req, &resp);
633613

634614
if (result.IsSucc()) {
635615
std::cout << "MultiUpload Succ." << std::endl;
@@ -667,7 +647,7 @@ void MultiUploadObjectWideChar(qcloud_cos::CosAPI& cos,
667647
req.SetWideCharPath(); // 需要调用该函数
668648
req.SetRecvTimeoutInms(1000 * 60);
669649
qcloud_cos::MultiPutObjectResp resp;
670-
qcloud_cos::CosResult result = cos.PutObject(req, &resp);
650+
qcloud_cos::CosResult result = cos.MultiPutObject(req, &resp);
671651

672652
if (result.IsSucc()) {
673653
std::cout << "MultiUploadObjectWideChar Succ." << std::endl;
@@ -692,17 +672,12 @@ void MultiUploadObjectLimitTraffic(qcloud_cos::CosAPI& cos,
692672
const std::string& bucket_name,
693673
const std::string& object_name,
694674
const std::string& local_file,
695-
const uint64_t traffic_limit,
696-
bool set_header = true) {
675+
const uint64_t traffic_limit) {
697676
qcloud_cos::MultiPutObjectReq req(bucket_name, object_name, local_file);
698-
if (set_header) {
699-
req.SetTrafficLimitByHeader(StringUtil::Uint64ToString(traffic_limit));
700-
} else {
701-
req.SetTrafficLimitByParam(StringUtil::Uint64ToString(traffic_limit));
702-
}
677+
req.SetTrafficLimit(traffic_limit);
703678
req.SetRecvTimeoutInms(1000 * 60);
704679
qcloud_cos::MultiPutObjectResp resp;
705-
qcloud_cos::CosResult result = cos.PutObject(req, &resp);
680+
qcloud_cos::CosResult result = cos.MultiPutObject(req, &resp);
706681

707682
if (result.IsSucc()) {
708683
std::cout << "MultiUpload Succ." << std::endl;
@@ -1633,8 +1608,8 @@ static void ResumableGetObject(qcloud_cos::CosAPI& cos,
16331608
const std::string& bucket_name,
16341609
const std::string& object_name,
16351610
const std::string& file_path) {
1636-
qcloud_cos::MultiGetObjectReq req(bucket_name, object_name, file_path);
1637-
qcloud_cos::MultiGetObjectResp resp;
1611+
qcloud_cos::GetObjectByFileReq req(bucket_name, object_name, file_path);
1612+
qcloud_cos::GetObjectByFileResp resp;
16381613

16391614
CosResult resule = cos.ResumableGetObject(req, &resp);
16401615
if (resule.IsSucc()) {
@@ -1666,12 +1641,12 @@ static void MultiGetObjectAsyncDoneCallback(const SharedAsyncContext& context,
16661641
<< context->GetBucketName()
16671642
<< ", ObjectName:" << context->GetObjectName()
16681643
<< ", LocalFile:" << context->GetLocalFilePath() << std::endl;
1669-
// qcloud_cos::MultiGetObjectReq对应的响应为qcloud_cos::MultiGetObjectResp
1644+
// qcloud_cos::MultiGetObjectReq对应的响应为qcloud_cos::GetObjectByFileResp
16701645
if (context->GetResult().IsSucc()) {
16711646
// 获取响应
16721647
std::cout << "MultiGetObjectAsync succeed" << std::endl;
16731648
std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
1674-
MultiGetObjectResp resp = context->GetMultiGetObjectResp();
1649+
AsyncResp resp = context->GetAsyncResp();
16751650
std::cout << "ETag:" << resp.GetEtag() << std::endl;
16761651
std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
16771652
} else {
@@ -1684,7 +1659,7 @@ void MultiGetObjectAsync(qcloud_cos::CosAPI& cos,
16841659
const std::string& bucket_name,
16851660
const std::string& object_name,
16861661
const std::string& file_path) {
1687-
qcloud_cos::MultiGetObjectReq req(bucket_name, object_name, file_path);
1662+
qcloud_cos::GetObjectAsyncReq req(bucket_name, object_name, file_path);
16881663
// 设置进度回调
16891664
req.SetTransferProgressCallback(&ProgressCallback);
16901665
// 设置状态回调
@@ -1703,7 +1678,7 @@ void MultiGetObjectAsync(qcloud_cos::CosAPI& cos,
17031678
// 获取响应
17041679
std::cout << "MultiGetObjectAsync succeed" << std::endl;
17051680
std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
1706-
MultiGetObjectResp resp = context->GetMultiGetObjectResp();
1681+
AsyncResp resp = context->GetAsyncResp();
17071682
std::cout << "ETag:" << resp.GetEtag() << std::endl;
17081683
std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
17091684
} else {
@@ -1719,15 +1694,14 @@ static void MultiPutObjectAsyncDoneCallback(const SharedAsyncContext& context,
17191694
<< context->GetBucketName()
17201695
<< ", ObjectName:" << context->GetObjectName()
17211696
<< ", LocalFile:" << context->GetLocalFilePath() << std::endl;
1722-
// qcloud_cos::MultiPutObjectReq对应的响应为qcloud_cos::MultiPutObjectResp
17231697
if (context->GetResult().IsSucc()) {
17241698
// 获取响应
17251699
std::cout << "MultiPutObjectAsync succeed" << std::endl;
17261700
std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
1727-
MultiPutObjectResp resp = context->GetMultiPutObjectResp();
1728-
std::cout << "Location:" << resp.GetLocation() << std::endl;
1729-
std::cout << "Bucket:" << resp.GetBucket() << std::endl;
1730-
std::cout << "Key:" << resp.GetKey() << std::endl;
1701+
AsyncResp resp = context->GetAsyncResp();
1702+
//std::cout << "Location:" << resp.GetLocation() << std::endl;
1703+
//std::cout << "Bucket:" << resp.GetBucket() << std::endl;
1704+
//std::cout << "Key:" << resp.GetKey() << std::endl;
17311705
std::cout << "ETag:" << resp.GetEtag() << std::endl;
17321706
std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
17331707
} else {
@@ -1741,7 +1715,7 @@ void MultiPutObjectAsync(qcloud_cos::CosAPI& cos,
17411715
const std::string& bucket_name,
17421716
const std::string& object_name,
17431717
const std::string& local_file) {
1744-
qcloud_cos::MultiPutObjectReq req(bucket_name, object_name, local_file);
1718+
qcloud_cos::PutObjectAsyncReq req(bucket_name, object_name, local_file);
17451719
req.SetRecvTimeoutInms(1000 * 60);
17461720
// 设置上传进度回调
17471721
req.SetTransferProgressCallback(&ProgressCallback);
@@ -1762,10 +1736,10 @@ void MultiPutObjectAsync(qcloud_cos::CosAPI& cos,
17621736
// 获取响应
17631737
std::cout << "MultiPutObjectAsync succeed" << std::endl;
17641738
std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
1765-
MultiPutObjectResp resp = context->GetMultiPutObjectResp();
1766-
std::cout << "Location:" << resp.GetLocation() << std::endl;
1767-
std::cout << "Bucket:" << resp.GetBucket() << std::endl;
1768-
std::cout << "Key:" << resp.GetKey() << std::endl;
1739+
AsyncResp resp = context->GetAsyncResp();
1740+
//std::cout << "Location:" << resp.GetLocation() << std::endl;
1741+
//std::cout << "Bucket:" << resp.GetBucket() << std::endl;
1742+
//std::cout << "Key:" << resp.GetKey() << std::endl;
17691743
std::cout << "ETag:" << resp.GetEtag() << std::endl;
17701744
std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
17711745
} else {

0 commit comments

Comments
 (0)