Skip to content

Commit 549a3af

Browse files
Merge pull request #170 from tencentyun/feature_huberyxxiao_d0fbb83b
demo 优化
2 parents c991e09 + bd1f35c commit 549a3af

15 files changed

+561
-4
lines changed

demo/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ file(GLOB copy_move_object_demo_src "${CMAKE_SOURCE_DIR}/demo/object_op_demo/cop
1616
file(GLOB multi_put_object_demo_src "${CMAKE_SOURCE_DIR}/demo/object_op_demo/multi_put_object_demo.cpp")
1717
file(GLOB multi_get_object_demo_src "${CMAKE_SOURCE_DIR}/demo/object_op_demo/multi_get_object_demo.cpp")
1818
file(GLOB select_objec_demo_src "${CMAKE_SOURCE_DIR}/demo/object_op_demo/select_objec_demo.cpp")
19+
file(GLOB get_bucket_list_demo_src "${CMAKE_SOURCE_DIR}/demo/bucket_op_demo/get_bucket_list_demo.cpp")
20+
file(GLOB delete_bucket_demo_src "${CMAKE_SOURCE_DIR}/demo/bucket_op_demo/delete_bucket_demo.cpp")
21+
file(GLOB head_bucket_demo_src "${CMAKE_SOURCE_DIR}/demo/bucket_op_demo/head_bucket_demo.cpp")
22+
file(GLOB put_bucket_demo_src "${CMAKE_SOURCE_DIR}/demo/bucket_op_demo/put_bucket_demo.cpp")
1923

2024
link_directories(${POCO_LINK_DIR} ${OPENSSL_LINK_DIR}) #这一行要放到add_executable前面
2125

@@ -32,6 +36,10 @@ add_executable(copy_move_object_demo ${copy_move_object_demo_src})
3236
add_executable(multi_put_object_demo ${multi_put_object_demo_src})
3337
add_executable(multi_get_object_demo ${multi_get_object_demo_src})
3438
add_executable(select_objec_demo ${select_objec_demo_src})
39+
add_executable(get_bucket_list_demo ${get_bucket_list_demo_src})
40+
add_executable(delete_bucket_demo ${delete_bucket_demo_src})
41+
add_executable(head_bucket_demo ${head_bucket_demo_src})
42+
add_executable(put_bucket_demo ${put_bucket_demo_src})
3543

3644
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
3745

@@ -48,6 +56,10 @@ target_link_libraries(copy_move_object_demo cossdk ssl crypto ${POCO_LIBS} ${OPE
4856
target_link_libraries(multi_put_object_demo cossdk ssl crypto ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
4957
target_link_libraries(multi_get_object_demo cossdk ssl crypto ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
5058
target_link_libraries(select_objec_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
59+
target_link_libraries(get_bucket_list_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
60+
target_link_libraries(delete_bucket_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
61+
target_link_libraries(head_bucket_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
62+
target_link_libraries(put_bucket_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
5163

5264
include_directories(${CMAKE_SOURCE_DIR}/include/ ${POCO_INCLUDE_DIR})
5365

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
#include <stdlib.h>
3+
#include <sys/stat.h>
4+
5+
#include <iostream>
6+
#include <map>
7+
#include <string>
8+
#include <thread>
9+
#include <vector>
10+
11+
#include "cos_api.h"
12+
#include "cos_sys_config.h"
13+
#include "util/auth_tool.h"
14+
15+
/**
16+
* 本样例演示了如何使用 COS C++ SDK 进行存储桶的删除
17+
*/
18+
using namespace qcloud_cos;
19+
20+
uint64_t appid = 12500000000;
21+
std::string tmp_secret_id = "AKIDXXXXXXXX";
22+
std::string tmp_secret_key = "1A2Z3YYYYYYYYYY";
23+
std::string region = "ap-guangzhou";
24+
std::string bucket_name = "examplebucket-12500000000";
25+
std::string tmp_token = "token";
26+
27+
/*
28+
* 本方法包含调用是否正常的判断,和请求结果的输出
29+
* 可通过本方法判断是否请求成功,并输出结果信息
30+
*/
31+
void PrintResult(const qcloud_cos::CosResult& result, const qcloud_cos::BaseResp& resp) {
32+
if (result.IsSucc()) {
33+
std::cout << "Request Succ." << std::endl;
34+
std::cout << resp.DebugString() << std::endl;
35+
} else {
36+
std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
37+
std::cout << "HttpStatus=" << result.GetHttpStatus() << std::endl;
38+
std::cout << "ErrorCode=" << result.GetErrorCode() << std::endl;
39+
std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
40+
std::cout << "ResourceAddr=" << result.GetResourceAddr() << std::endl;
41+
std::cout << "XCosRequestId=" << result.GetXCosRequestId() << std::endl;
42+
std::cout << "XCosTraceId=" << result.GetXCosTraceId() << std::endl;
43+
}
44+
}
45+
46+
/*
47+
* 通过参数形式初始化 CosAPI 对象
48+
*/
49+
qcloud_cos::CosAPI InitCosAPI() {
50+
qcloud_cos::CosConfig config(appid, tmp_secret_id, tmp_secret_key, region);
51+
config.SetTmpToken(tmp_token); // 推荐使用临时密钥初始化 CosAPI 对象, 如果您使用永久密钥初始化 CosAPI 对象,请注释
52+
qcloud_cos::CosAPI cos_tmp(config);
53+
return cos_tmp;
54+
}
55+
56+
void DeleteBucket(qcloud_cos::CosAPI& cos) {
57+
qcloud_cos::DeleteBucketReq req(bucket_name);
58+
qcloud_cos::DeleteBucketResp resp;
59+
qcloud_cos::CosResult result = cos.DeleteBucket(req, &resp);
60+
61+
std::cout << "===================DeleteBucketResponse====================="
62+
<< std::endl;
63+
PrintResult(result, resp);
64+
std::cout << "========================================================="
65+
<< std::endl;
66+
}
67+
68+
69+
int main() {
70+
qcloud_cos::CosAPI cos = InitCosAPI();
71+
CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
72+
DeleteBucket(cos);
73+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
2+
#include <stdlib.h>
3+
#include <sys/stat.h>
4+
5+
#include <iostream>
6+
#include <map>
7+
#include <string>
8+
#include <thread>
9+
#include <vector>
10+
11+
#include "cos_api.h"
12+
#include "cos_sys_config.h"
13+
#include "util/auth_tool.h"
14+
15+
/**
16+
* 本样例演示了如何使用 COS C++ SDK 进行存储桶列表的获取
17+
*/
18+
using namespace qcloud_cos;
19+
20+
uint64_t appid = 12500000000;
21+
std::string tmp_secret_id = "AKIDXXXXXXXX";
22+
std::string tmp_secret_key = "1A2Z3YYYYYYYYYY";
23+
std::string region = "ap-guangzhou";
24+
std::string tmp_token = "token";
25+
26+
/*
27+
* 本方法包含调用是否正常的判断,和请求结果的输出
28+
* 可通过本方法判断是否请求成功,并输出结果信息
29+
*/
30+
void PrintResult(const qcloud_cos::CosResult& result, const qcloud_cos::BaseResp& resp) {
31+
if (result.IsSucc()) {
32+
std::cout << "Request Succ." << std::endl;
33+
std::cout << resp.DebugString() << std::endl;
34+
} else {
35+
std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
36+
std::cout << "HttpStatus=" << result.GetHttpStatus() << std::endl;
37+
std::cout << "ErrorCode=" << result.GetErrorCode() << std::endl;
38+
std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
39+
std::cout << "ResourceAddr=" << result.GetResourceAddr() << std::endl;
40+
std::cout << "XCosRequestId=" << result.GetXCosRequestId() << std::endl;
41+
std::cout << "XCosTraceId=" << result.GetXCosTraceId() << std::endl;
42+
}
43+
}
44+
45+
/*
46+
* 通过参数形式初始化 CosAPI 对象
47+
*/
48+
qcloud_cos::CosAPI InitCosAPI() {
49+
qcloud_cos::CosConfig config(appid, tmp_secret_id, tmp_secret_key, region);
50+
config.SetTmpToken(tmp_token); // 推荐使用临时密钥初始化 CosAPI 对象, 如果您使用永久密钥初始化 CosAPI 对象,请注释
51+
qcloud_cos::CosAPI cos_tmp(config);
52+
return cos_tmp;
53+
}
54+
55+
void GetService(qcloud_cos::CosAPI& cos) {
56+
qcloud_cos::GetServiceReq req;
57+
qcloud_cos::GetServiceResp resp;
58+
qcloud_cos::CosResult result = cos.GetService(req, &resp);
59+
60+
std::cout << "===================GetService====================="
61+
<< std::endl;
62+
PrintResult(result, resp);
63+
const qcloud_cos::Owner& owner = resp.GetOwner();
64+
const std::vector<qcloud_cos::Bucket>& buckets = resp.GetBuckets();
65+
std::cout << "owner.m_id=" << owner.m_id << ", owner.display_name=" << owner.m_display_name << std::endl;
66+
for (std::vector<qcloud_cos::Bucket>::const_iterator itr = buckets.begin();
67+
itr != buckets.end(); ++itr) {
68+
const qcloud_cos::Bucket& bucket = *itr;
69+
std::cout << "Bucket name=" << bucket.m_name
70+
<< ", location=" << bucket.m_location
71+
<< ", create_date=" << bucket.m_create_date << std::endl;
72+
}
73+
std::cout << "=========================================================" << std::endl;
74+
}
75+
76+
int main() {
77+
qcloud_cos::CosAPI cos = InitCosAPI();
78+
CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
79+
GetService(cos);
80+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
2+
#include <stdlib.h>
3+
#include <sys/stat.h>
4+
5+
#include <iostream>
6+
#include <map>
7+
#include <string>
8+
#include <thread>
9+
#include <vector>
10+
11+
#include "cos_api.h"
12+
#include "cos_sys_config.h"
13+
#include "util/auth_tool.h"
14+
15+
/**
16+
* 本样例演示了如何使用 COS C++ SDK 进行存储桶检索和判断是否存在
17+
*/
18+
using namespace qcloud_cos;
19+
20+
uint64_t appid = 12500000000;
21+
std::string tmp_secret_id = "AKIDXXXXXXXX";
22+
std::string tmp_secret_key = "1A2Z3YYYYYYYYYY";
23+
std::string region = "ap-guangzhou";
24+
std::string bucket_name = "examplebucket-12500000000";
25+
std::string tmp_token = "token";
26+
27+
/*
28+
* 本方法包含调用是否正常的判断,和请求结果的输出
29+
* 可通过本方法判断是否请求成功,并输出结果信息
30+
*/
31+
void PrintResult(const qcloud_cos::CosResult& result, const qcloud_cos::BaseResp& resp) {
32+
if (result.IsSucc()) {
33+
std::cout << "Request Succ." << std::endl;
34+
std::cout << resp.DebugString() << std::endl;
35+
} else {
36+
std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
37+
std::cout << "HttpStatus=" << result.GetHttpStatus() << std::endl;
38+
std::cout << "ErrorCode=" << result.GetErrorCode() << std::endl;
39+
std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
40+
std::cout << "ResourceAddr=" << result.GetResourceAddr() << std::endl;
41+
std::cout << "XCosRequestId=" << result.GetXCosRequestId() << std::endl;
42+
std::cout << "XCosTraceId=" << result.GetXCosTraceId() << std::endl;
43+
}
44+
}
45+
46+
/*
47+
* 通过参数形式初始化 CosAPI 对象
48+
*/
49+
qcloud_cos::CosAPI InitCosAPI() {
50+
qcloud_cos::CosConfig config(appid, tmp_secret_id, tmp_secret_key, region);
51+
config.SetTmpToken(tmp_token); // 推荐使用临时密钥初始化 CosAPI 对象, 如果您使用永久密钥初始化 CosAPI 对象,请注释
52+
qcloud_cos::CosAPI cos_tmp(config);
53+
return cos_tmp;
54+
}
55+
56+
void HeadBucket(qcloud_cos::CosAPI& cos) {
57+
qcloud_cos::HeadBucketReq req(bucket_name);
58+
qcloud_cos::HeadBucketResp resp;
59+
qcloud_cos::CosResult result = cos.HeadBucket(req, &resp);
60+
61+
std::cout << "===================HeadBucketResponse====================="
62+
<< std::endl;
63+
PrintResult(result, resp);
64+
std::cout << "=========================================================="
65+
<< std::endl;
66+
}
67+
68+
void IsBucketExist(qcloud_cos::CosAPI& cos) {
69+
std::cout << "===================IsBucketExist====================="
70+
<< std::endl;
71+
std::cout << (cos.IsBucketExist("abcdefg") ? "true" : "false") << std::endl;
72+
std::cout << (cos.IsBucketExist(bucket_name) ? "true" : "false") << std::endl;
73+
std::cout
74+
<< "===================================================================="
75+
<< std::endl;
76+
}
77+
78+
79+
int main() {
80+
qcloud_cos::CosAPI cos = InitCosAPI();
81+
CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
82+
HeadBucket(cos);
83+
IsBucketExist(cos);
84+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
#include <stdlib.h>
3+
#include <sys/stat.h>
4+
5+
#include <iostream>
6+
#include <map>
7+
#include <string>
8+
#include <thread>
9+
#include <vector>
10+
11+
#include "cos_api.h"
12+
#include "cos_sys_config.h"
13+
#include "util/auth_tool.h"
14+
15+
/**
16+
* 本样例演示了如何使用 COS C++ SDK 进行存储桶的创建
17+
*/
18+
using namespace qcloud_cos;
19+
20+
uint64_t appid = 12500000000;
21+
std::string tmp_secret_id = "AKIDXXXXXXXX";
22+
std::string tmp_secret_key = "1A2Z3YYYYYYYYYY";
23+
std::string region = "ap-guangzhou@xxx";
24+
std::string bucket_name = "examplebucket-12500000000";
25+
std::string tmp_token = "token";
26+
27+
/*
28+
* 本方法包含调用是否正常的判断,和请求结果的输出
29+
* 可通过本方法判断是否请求成功,并输出结果信息
30+
*/
31+
void PrintResult(const qcloud_cos::CosResult& result, const qcloud_cos::BaseResp& resp) {
32+
if (result.IsSucc()) {
33+
std::cout << "Request Succ." << std::endl;
34+
std::cout << resp.DebugString() << std::endl;
35+
} else {
36+
std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
37+
std::cout << "HttpStatus=" << result.GetHttpStatus() << std::endl;
38+
std::cout << "ErrorCode=" << result.GetErrorCode() << std::endl;
39+
std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
40+
std::cout << "ResourceAddr=" << result.GetResourceAddr() << std::endl;
41+
std::cout << "XCosRequestId=" << result.GetXCosRequestId() << std::endl;
42+
std::cout << "XCosTraceId=" << result.GetXCosTraceId() << std::endl;
43+
}
44+
}
45+
46+
/*
47+
* 通过参数形式初始化 CosAPI 对象
48+
*/
49+
qcloud_cos::CosAPI InitCosAPI() {
50+
qcloud_cos::CosConfig config(appid, tmp_secret_id, tmp_secret_key, region);
51+
config.SetTmpToken(tmp_token); // 推荐使用临时密钥初始化 CosAPI 对象, 如果您使用永久密钥初始化 CosAPI 对象,请注释
52+
qcloud_cos::CosAPI cos_tmp(config);
53+
return cos_tmp;
54+
}
55+
56+
void PutBucket(qcloud_cos::CosAPI& cos) {
57+
qcloud_cos::PutBucketReq req(bucket_name);
58+
//创建MAZ存储桶使用下方进行设置
59+
//req.SetMAZBucket();
60+
qcloud_cos::PutBucketResp resp;
61+
qcloud_cos::CosResult result = cos.PutBucket(req, &resp);
62+
63+
std::cout << "===================PutBucketResponse====================="
64+
<< std::endl;
65+
PrintResult(result, resp);
66+
std::cout << "========================================================="
67+
<< std::endl;
68+
}
69+
70+
71+
int main() {
72+
try{
73+
qcloud_cos::CosAPI cos = InitCosAPI();
74+
CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
75+
76+
PutBucket(cos);
77+
}
78+
catch(const std::exception& e){
79+
std::cerr << e.what() << "]]" << '\n';
80+
81+
std::cout << (strcmp(e.what(),"Invalid region configuration in CosConfig :ap-guangzhou@xxx") == 0) << std::endl;
82+
}
83+
std::cout << "Hello, World!" << std::endl;
84+
85+
}

include/cos_config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ class CosConfig {
191191

192192
bool IsDomainSameToHostEnable() const;
193193

194+
bool CheckRegion();
195+
194196
// void SetDomainSameToHostEnable(bool is_domain_same_to_host_enable);
195197

196198
/// \brief 设置日志回调

include/request/bucket_req.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,26 @@
1212
#include "cos_defines.h"
1313
#include "request/base_req.h"
1414
#include "util/string_util.h"
15+
#include "util/illegal_intercept.h"
1516

1617
namespace qcloud_cos {
1718

1819
class BucketReq : public BaseReq {
1920
public:
2021
BucketReq() : m_bucket_name("") {}
21-
explicit BucketReq(const std::string& bucket_name) : m_bucket_name(bucket_name) {}
22+
explicit BucketReq(const std::string& bucket_name) : m_bucket_name(bucket_name) {
23+
if (!IllegalIntercept::CheckBucket(bucket_name)){
24+
throw std::invalid_argument("Invalid bucket_name argument :" + bucket_name);
25+
}
26+
}
2227
virtual ~BucketReq() {}
2328

2429
// getter and setter
2530
std::string GetBucketName() const { return m_bucket_name; }
2631
void SetBucketName(const std::string& bucket_name) {
32+
if (!IllegalIntercept::CheckBucket(bucket_name)){
33+
throw std::invalid_argument("Invalid bucket_name argument :" + bucket_name);
34+
}
2735
m_bucket_name = bucket_name;
2836
}
2937
virtual bool GenerateRequestBody(std::string* body) const { UNUSED_PARAM(body); return true; }

0 commit comments

Comments
 (0)