Skip to content

Commit ea147d7

Browse files
Merge pull request #103 from jackyding2679/dev/async-api-1
Dev/async api
2 parents 1106925 + 19a52ae commit ea147d7

39 files changed

+2119
-1150
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.idea
22
.github
33
.git
4-
.vscode
4+
.vscode
5+
build/*

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
cmake_minimum_required(VERSION 2.8)
22
CMAKE_policy(SET CMP0015 NEW)
33
project(cos-cpp-sdk)
4-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
54
set(CMAKE_VERBOSE_MAKEFILE ON)
65

76
option(BUILD_UNITTEST "Build unittest" OFF)
@@ -20,6 +19,7 @@ endif()
2019
message(STATUS "OS type: ${OS_TYPE}")
2120

2221
if (${OS_TYPE} STREQUAL "WINDOWS")
22+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
2323
set(BUILD_TARGET "Win32")
2424
if (CMAKE_CL_64)
2525
set(BUILD_TARGET "x64")
@@ -38,6 +38,7 @@ if (${OS_TYPE} STREQUAL "WINDOWS")
3838
#需要加该参数,不然VS会报错
3939
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8")
4040
else()
41+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Werror -Wall -Wextra")
4142
set(POCO_LINK_DIR ${CMAKE_SOURCE_DIR}/third_party/lib/linux/poco/)
4243
if (${OS_TYPE} STREQUAL "APPLE")
4344
set(POCO_LINK_DIR ${CMAKE_SOURCE_DIR}/third_party/lib/macOS/poco/)

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/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
project(cos-demo)
22

3-
file(GLOB demo_src "${CMAKE_SOURCE_DIR}/demo/*.cpp")
3+
if (NOT ${OS_TYPE} STREQUAL "WINDOWS")
4+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-function")
5+
endif()
6+
file(GLOB demo_src "${CMAKE_SOURCE_DIR}/demo/cos_demo.cpp")
7+
file(GLOB stable_test_src "${CMAKE_SOURCE_DIR}/demo/stable_test.cpp")
48

59
link_directories(${POCO_LINK_DIR}) #这一行要放到add_executable前面
610
add_executable(${PROJECT_NAME} ${demo_src})
11+
add_executable(stable_test ${stable_test_src})
712
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
813
target_link_libraries(${PROJECT_NAME} cossdk ${POCO_LIBS} ${SYSTEM_LIBS})
14+
target_link_libraries(stable_test cossdk ${POCO_LIBS} ${SYSTEM_LIBS})
915
include_directories(${CMAKE_SOURCE_DIR}/include/ ${POCO_INCLUDE_DIR})
1016

1117
if(${OS_TYPE} STREQUAL "WINDOWS")

demo/cos_demo.cpp

Lines changed: 50 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ void GetBucketLifecycle(qcloud_cos::CosAPI& cos,
241241
qcloud_cos::CosResult result = cos.GetBucketLifecycle(req, &resp);
242242

243243
const std::vector<qcloud_cos::LifecycleRule>& rules = resp.GetRules();
244-
for (int idx = 0; idx != rules.size(); ++idx) {
244+
for (size_t idx = 0; idx != rules.size(); ++idx) {
245245
std::cout << "id = " << rules[idx].GetId() << std::endl;
246246
}
247247

@@ -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()) {
@@ -1662,29 +1637,30 @@ static void ProgressCallback(uint64_t transferred_size, uint64_t total_size,
16621637

16631638
static void MultiGetObjectAsyncDoneCallback(const SharedAsyncContext& context,
16641639
void* user_data) {
1640+
UNUSED_PARAM(user_data)
16651641
std::cout << "MultiGetObjectAsyncDoneCallback, BucketName:"
16661642
<< context->GetBucketName()
16671643
<< ", ObjectName:" << context->GetObjectName()
16681644
<< ", LocalFile:" << context->GetLocalFilePath() << std::endl;
1669-
// qcloud_cos::MultiGetObjectReq对应的响应为qcloud_cos::MultiGetObjectResp
1645+
// qcloud_cos::MultiGetObjectReq对应的响应为qcloud_cos::GetObjectByFileResp
16701646
if (context->GetResult().IsSucc()) {
16711647
// 获取响应
1672-
std::cout << "MultiGetObjectAsync succeed" << std::endl;
1648+
std::cout << "AsyncMultiGetObject succeed" << std::endl;
16731649
std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
1674-
MultiGetObjectResp resp = context->GetMultiGetObjectResp();
1650+
AsyncResp resp = context->GetAsyncResp();
16751651
std::cout << "ETag:" << resp.GetEtag() << std::endl;
16761652
std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
16771653
} else {
1678-
std::cout << "MultiGetObjectAsync failed" << std::endl;
1654+
std::cout << "AsyncMultiGetObject failed" << std::endl;
16791655
std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;
16801656
}
16811657
}
16821658
// 异步下载对象,支持更新下载进度
1683-
void MultiGetObjectAsync(qcloud_cos::CosAPI& cos,
1659+
void AsyncMultiGetObject(qcloud_cos::CosAPI& cos,
16841660
const std::string& bucket_name,
16851661
const std::string& object_name,
16861662
const std::string& file_path) {
1687-
qcloud_cos::MultiGetObjectReq req(bucket_name, object_name, file_path);
1663+
qcloud_cos::AsyncMultiGetObjectReq req(bucket_name, object_name, file_path);
16881664
// 设置进度回调
16891665
req.SetTransferProgressCallback(&ProgressCallback);
16901666
// 设置状态回调
@@ -1693,55 +1669,55 @@ void MultiGetObjectAsync(qcloud_cos::CosAPI& cos,
16931669
req.SetUserData(&req);
16941670

16951671
// 开始下载
1696-
SharedAsyncContext context = cos.GetObjectAsync(req);
1672+
SharedAsyncContext context = cos.AsyncMultiGetObject(req);
16971673

16981674
// 等待下载结束
16991675
context->WaitUntilFinish();
17001676

17011677
// 检查结果
17021678
if (context->GetResult().IsSucc()) {
17031679
// 获取响应
1704-
std::cout << "MultiGetObjectAsync succeed" << std::endl;
1680+
std::cout << "AsyncMultiGetObject succeed" << std::endl;
17051681
std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
1706-
MultiGetObjectResp resp = context->GetMultiGetObjectResp();
1682+
AsyncResp resp = context->GetAsyncResp();
17071683
std::cout << "ETag:" << resp.GetEtag() << std::endl;
17081684
std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
17091685
} else {
1710-
std::cout << "MultiGetObjectAsync failed" << std::endl;
1686+
std::cout << "AsyncMultiGetObject failed" << std::endl;
17111687
std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;
17121688
}
17131689
std::this_thread::sleep_for(std::chrono::seconds(1));
17141690
}
17151691

17161692
static void MultiPutObjectAsyncDoneCallback(const SharedAsyncContext& context,
17171693
void* user_data) {
1694+
UNUSED_PARAM(user_data)
17181695
std::cout << "MultiPutObjectAsyncDoneCallback, BucketName:"
17191696
<< context->GetBucketName()
17201697
<< ", ObjectName:" << context->GetObjectName()
17211698
<< ", LocalFile:" << context->GetLocalFilePath() << std::endl;
1722-
// qcloud_cos::MultiPutObjectReq对应的响应为qcloud_cos::MultiPutObjectResp
17231699
if (context->GetResult().IsSucc()) {
17241700
// 获取响应
1725-
std::cout << "MultiPutObjectAsync succeed" << std::endl;
1701+
std::cout << "AsyncMultiPutObject succeed" << std::endl;
17261702
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;
1703+
AsyncResp resp = context->GetAsyncResp();
1704+
//std::cout << "Location:" << resp.GetLocation() << std::endl;
1705+
//std::cout << "Bucket:" << resp.GetBucket() << std::endl;
1706+
//std::cout << "Key:" << resp.GetKey() << std::endl;
17311707
std::cout << "ETag:" << resp.GetEtag() << std::endl;
17321708
std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
17331709
} else {
1734-
std::cout << "MultiGetObjectAsync failed" << std::endl;
1710+
std::cout << "AsyncMultiGetObject failed" << std::endl;
17351711
std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;
17361712
}
17371713
}
17381714

17391715
// 异步多上传对象,支持更新上传进度
1740-
void MultiPutObjectAsync(qcloud_cos::CosAPI& cos,
1716+
void AsyncMultiPutObject(qcloud_cos::CosAPI& cos,
17411717
const std::string& bucket_name,
17421718
const std::string& object_name,
17431719
const std::string& local_file) {
1744-
qcloud_cos::MultiPutObjectReq req(bucket_name, object_name, local_file);
1720+
qcloud_cos::AsyncMultiPutObjectReq req(bucket_name, object_name, local_file);
17451721
req.SetRecvTimeoutInms(1000 * 60);
17461722
// 设置上传进度回调
17471723
req.SetTransferProgressCallback(&ProgressCallback);
@@ -1751,7 +1727,7 @@ void MultiPutObjectAsync(qcloud_cos::CosAPI& cos,
17511727
req.SetUserData(&req);
17521728

17531729
// 开始上传
1754-
SharedAsyncContext context = cos.PutObjectAsync(req);
1730+
SharedAsyncContext context = cos.AsyncMultiPutObject(req);
17551731

17561732
// 等待上传结束
17571733
std::cout << "wait finish..." << std::endl;
@@ -1760,16 +1736,16 @@ void MultiPutObjectAsync(qcloud_cos::CosAPI& cos,
17601736
// 检查结果
17611737
if (context->GetResult().IsSucc()) {
17621738
// 获取响应
1763-
std::cout << "MultiPutObjectAsync succeed" << std::endl;
1739+
std::cout << "AsyncMultiPutObject succeed" << std::endl;
17641740
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;
1741+
AsyncResp resp = context->GetAsyncResp();
1742+
//std::cout << "Location:" << resp.GetLocation() << std::endl;
1743+
//std::cout << "Bucket:" << resp.GetBucket() << std::endl;
1744+
//std::cout << "Key:" << resp.GetKey() << std::endl;
17691745
std::cout << "ETag:" << resp.GetEtag() << std::endl;
17701746
std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
17711747
} else {
1772-
std::cout << "MultiPutObjectAsync failed" << std::endl;
1748+
std::cout << "AsyncMultiPutObject failed" << std::endl;
17731749
std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;
17741750
}
17751751
}
@@ -1926,8 +1902,7 @@ void DeleteObjectsByPrefix(qcloud_cos::CosAPI& cos,
19261902
void MoveObject(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
19271903
const std::string& src_object, const std::string& dst_object) {
19281904
MoveObjectReq req(bucket_name, src_object, dst_object);
1929-
MoveObjectResp resp;
1930-
CosResult result = cos.MoveObject(req, &resp);
1905+
CosResult result = cos.MoveObject(req);
19311906
if (result.IsSucc()) {
19321907
std::cout << "MoveObject Succ." << std::endl;
19331908
} else {
@@ -2480,6 +2455,8 @@ void TestLogCallback(const std::string& log) {
24802455
}
24812456

24822457
int main(int argc, char** argv) {
2458+
UNUSED_PARAM(argc)
2459+
UNUSED_PARAM(argv)
24832460
// config.json中字段的说明,可以参考https://cloud.tencent.com/document/product/436/12301
24842461
qcloud_cos::CosConfig config("./config.json");
24852462
config.SetLogCallback(&TestLogCallback);
@@ -2834,8 +2811,8 @@ int main(int argc, char** argv) {
28342811

28352812
// async
28362813
//{
2837-
// MultiPutObjectAsync(cos, bucket_name, "bigfile", "./bigfile");
2838-
// MultiGetObjectAsync(cos, bucket_name, "bigfile", "./bigfile_download");
2814+
// AsyncMultiPutObject(cos, bucket_name, "bigfile", "./bigfile");
2815+
// AsyncMultiGetObject(cos, bucket_name, "bigfile", "./bigfile_download");
28392816
//}
28402817

28412818
//{

0 commit comments

Comments
 (0)