Skip to content

Commit 19a52ae

Browse files
author
jackyding(丁磊)
committed
1.optimize async api 2.add stable test tool
1 parent 8ced1ae commit 19a52ae

28 files changed

+1282
-662
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/)

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: 22 additions & 19 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

@@ -1637,29 +1637,30 @@ static void ProgressCallback(uint64_t transferred_size, uint64_t total_size,
16371637

16381638
static void MultiGetObjectAsyncDoneCallback(const SharedAsyncContext& context,
16391639
void* user_data) {
1640+
UNUSED_PARAM(user_data)
16401641
std::cout << "MultiGetObjectAsyncDoneCallback, BucketName:"
16411642
<< context->GetBucketName()
16421643
<< ", ObjectName:" << context->GetObjectName()
16431644
<< ", LocalFile:" << context->GetLocalFilePath() << std::endl;
16441645
// qcloud_cos::MultiGetObjectReq对应的响应为qcloud_cos::GetObjectByFileResp
16451646
if (context->GetResult().IsSucc()) {
16461647
// 获取响应
1647-
std::cout << "MultiGetObjectAsync succeed" << std::endl;
1648+
std::cout << "AsyncMultiGetObject succeed" << std::endl;
16481649
std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
16491650
AsyncResp resp = context->GetAsyncResp();
16501651
std::cout << "ETag:" << resp.GetEtag() << std::endl;
16511652
std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
16521653
} else {
1653-
std::cout << "MultiGetObjectAsync failed" << std::endl;
1654+
std::cout << "AsyncMultiGetObject failed" << std::endl;
16541655
std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;
16551656
}
16561657
}
16571658
// 异步下载对象,支持更新下载进度
1658-
void MultiGetObjectAsync(qcloud_cos::CosAPI& cos,
1659+
void AsyncMultiGetObject(qcloud_cos::CosAPI& cos,
16591660
const std::string& bucket_name,
16601661
const std::string& object_name,
16611662
const std::string& file_path) {
1662-
qcloud_cos::GetObjectAsyncReq req(bucket_name, object_name, file_path);
1663+
qcloud_cos::AsyncMultiGetObjectReq req(bucket_name, object_name, file_path);
16631664
// 设置进度回调
16641665
req.SetTransferProgressCallback(&ProgressCallback);
16651666
// 设置状态回调
@@ -1668,35 +1669,36 @@ void MultiGetObjectAsync(qcloud_cos::CosAPI& cos,
16681669
req.SetUserData(&req);
16691670

16701671
// 开始下载
1671-
SharedAsyncContext context = cos.GetObjectAsync(req);
1672+
SharedAsyncContext context = cos.AsyncMultiGetObject(req);
16721673

16731674
// 等待下载结束
16741675
context->WaitUntilFinish();
16751676

16761677
// 检查结果
16771678
if (context->GetResult().IsSucc()) {
16781679
// 获取响应
1679-
std::cout << "MultiGetObjectAsync succeed" << std::endl;
1680+
std::cout << "AsyncMultiGetObject succeed" << std::endl;
16801681
std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
16811682
AsyncResp resp = context->GetAsyncResp();
16821683
std::cout << "ETag:" << resp.GetEtag() << std::endl;
16831684
std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
16841685
} else {
1685-
std::cout << "MultiGetObjectAsync failed" << std::endl;
1686+
std::cout << "AsyncMultiGetObject failed" << std::endl;
16861687
std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;
16871688
}
16881689
std::this_thread::sleep_for(std::chrono::seconds(1));
16891690
}
16901691

16911692
static void MultiPutObjectAsyncDoneCallback(const SharedAsyncContext& context,
16921693
void* user_data) {
1694+
UNUSED_PARAM(user_data)
16931695
std::cout << "MultiPutObjectAsyncDoneCallback, BucketName:"
16941696
<< context->GetBucketName()
16951697
<< ", ObjectName:" << context->GetObjectName()
16961698
<< ", LocalFile:" << context->GetLocalFilePath() << std::endl;
16971699
if (context->GetResult().IsSucc()) {
16981700
// 获取响应
1699-
std::cout << "MultiPutObjectAsync succeed" << std::endl;
1701+
std::cout << "AsyncMultiPutObject succeed" << std::endl;
17001702
std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
17011703
AsyncResp resp = context->GetAsyncResp();
17021704
//std::cout << "Location:" << resp.GetLocation() << std::endl;
@@ -1705,17 +1707,17 @@ static void MultiPutObjectAsyncDoneCallback(const SharedAsyncContext& context,
17051707
std::cout << "ETag:" << resp.GetEtag() << std::endl;
17061708
std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
17071709
} else {
1708-
std::cout << "MultiGetObjectAsync failed" << std::endl;
1710+
std::cout << "AsyncMultiGetObject failed" << std::endl;
17091711
std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;
17101712
}
17111713
}
17121714

17131715
// 异步多上传对象,支持更新上传进度
1714-
void MultiPutObjectAsync(qcloud_cos::CosAPI& cos,
1716+
void AsyncMultiPutObject(qcloud_cos::CosAPI& cos,
17151717
const std::string& bucket_name,
17161718
const std::string& object_name,
17171719
const std::string& local_file) {
1718-
qcloud_cos::PutObjectAsyncReq req(bucket_name, object_name, local_file);
1720+
qcloud_cos::AsyncMultiPutObjectReq req(bucket_name, object_name, local_file);
17191721
req.SetRecvTimeoutInms(1000 * 60);
17201722
// 设置上传进度回调
17211723
req.SetTransferProgressCallback(&ProgressCallback);
@@ -1725,7 +1727,7 @@ void MultiPutObjectAsync(qcloud_cos::CosAPI& cos,
17251727
req.SetUserData(&req);
17261728

17271729
// 开始上传
1728-
SharedAsyncContext context = cos.PutObjectAsync(req);
1730+
SharedAsyncContext context = cos.AsyncMultiPutObject(req);
17291731

17301732
// 等待上传结束
17311733
std::cout << "wait finish..." << std::endl;
@@ -1734,7 +1736,7 @@ void MultiPutObjectAsync(qcloud_cos::CosAPI& cos,
17341736
// 检查结果
17351737
if (context->GetResult().IsSucc()) {
17361738
// 获取响应
1737-
std::cout << "MultiPutObjectAsync succeed" << std::endl;
1739+
std::cout << "AsyncMultiPutObject succeed" << std::endl;
17381740
std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
17391741
AsyncResp resp = context->GetAsyncResp();
17401742
//std::cout << "Location:" << resp.GetLocation() << std::endl;
@@ -1743,7 +1745,7 @@ void MultiPutObjectAsync(qcloud_cos::CosAPI& cos,
17431745
std::cout << "ETag:" << resp.GetEtag() << std::endl;
17441746
std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
17451747
} else {
1746-
std::cout << "MultiPutObjectAsync failed" << std::endl;
1748+
std::cout << "AsyncMultiPutObject failed" << std::endl;
17471749
std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;
17481750
}
17491751
}
@@ -1900,8 +1902,7 @@ void DeleteObjectsByPrefix(qcloud_cos::CosAPI& cos,
19001902
void MoveObject(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
19011903
const std::string& src_object, const std::string& dst_object) {
19021904
MoveObjectReq req(bucket_name, src_object, dst_object);
1903-
MoveObjectResp resp;
1904-
CosResult result = cos.MoveObject(req, &resp);
1905+
CosResult result = cos.MoveObject(req);
19051906
if (result.IsSucc()) {
19061907
std::cout << "MoveObject Succ." << std::endl;
19071908
} else {
@@ -2454,6 +2455,8 @@ void TestLogCallback(const std::string& log) {
24542455
}
24552456

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

28092812
// async
28102813
//{
2811-
// MultiPutObjectAsync(cos, bucket_name, "bigfile", "./bigfile");
2812-
// MultiGetObjectAsync(cos, bucket_name, "bigfile", "./bigfile_download");
2814+
// AsyncMultiPutObject(cos, bucket_name, "bigfile", "./bigfile");
2815+
// AsyncMultiGetObject(cos, bucket_name, "bigfile", "./bigfile_download");
28132816
//}
28142817

28152818
//{

0 commit comments

Comments
 (0)