Skip to content

Commit

Permalink
Merge pull request #170 from tencentyun/feature_huberyxxiao_d0fbb83b
Browse files Browse the repository at this point in the history
demo 优化
  • Loading branch information
Huberyxiao authored Dec 9, 2024
2 parents c991e09 + bd1f35c commit 549a3af
Show file tree
Hide file tree
Showing 15 changed files with 561 additions and 4 deletions.
12 changes: 12 additions & 0 deletions demo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ file(GLOB copy_move_object_demo_src "${CMAKE_SOURCE_DIR}/demo/object_op_demo/cop
file(GLOB multi_put_object_demo_src "${CMAKE_SOURCE_DIR}/demo/object_op_demo/multi_put_object_demo.cpp")
file(GLOB multi_get_object_demo_src "${CMAKE_SOURCE_DIR}/demo/object_op_demo/multi_get_object_demo.cpp")
file(GLOB select_objec_demo_src "${CMAKE_SOURCE_DIR}/demo/object_op_demo/select_objec_demo.cpp")
file(GLOB get_bucket_list_demo_src "${CMAKE_SOURCE_DIR}/demo/bucket_op_demo/get_bucket_list_demo.cpp")
file(GLOB delete_bucket_demo_src "${CMAKE_SOURCE_DIR}/demo/bucket_op_demo/delete_bucket_demo.cpp")
file(GLOB head_bucket_demo_src "${CMAKE_SOURCE_DIR}/demo/bucket_op_demo/head_bucket_demo.cpp")
file(GLOB put_bucket_demo_src "${CMAKE_SOURCE_DIR}/demo/bucket_op_demo/put_bucket_demo.cpp")

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

Expand All @@ -32,6 +36,10 @@ add_executable(copy_move_object_demo ${copy_move_object_demo_src})
add_executable(multi_put_object_demo ${multi_put_object_demo_src})
add_executable(multi_get_object_demo ${multi_get_object_demo_src})
add_executable(select_objec_demo ${select_objec_demo_src})
add_executable(get_bucket_list_demo ${get_bucket_list_demo_src})
add_executable(delete_bucket_demo ${delete_bucket_demo_src})
add_executable(head_bucket_demo ${head_bucket_demo_src})
add_executable(put_bucket_demo ${put_bucket_demo_src})

set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)

Expand All @@ -48,6 +56,10 @@ target_link_libraries(copy_move_object_demo cossdk ssl crypto ${POCO_LIBS} ${OPE
target_link_libraries(multi_put_object_demo cossdk ssl crypto ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(multi_get_object_demo cossdk ssl crypto ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(select_objec_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(get_bucket_list_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(delete_bucket_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(head_bucket_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(put_bucket_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})

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

Expand Down
73 changes: 73 additions & 0 deletions demo/bucket_op_demo/delete_bucket_demo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

#include <stdlib.h>
#include <sys/stat.h>

#include <iostream>
#include <map>
#include <string>
#include <thread>
#include <vector>

#include "cos_api.h"
#include "cos_sys_config.h"
#include "util/auth_tool.h"

/**
* 本样例演示了如何使用 COS C++ SDK 进行存储桶的删除
*/
using namespace qcloud_cos;

uint64_t appid = 12500000000;
std::string tmp_secret_id = "AKIDXXXXXXXX";
std::string tmp_secret_key = "1A2Z3YYYYYYYYYY";
std::string region = "ap-guangzhou";
std::string bucket_name = "examplebucket-12500000000";
std::string tmp_token = "token";

/*
* 本方法包含调用是否正常的判断,和请求结果的输出
* 可通过本方法判断是否请求成功,并输出结果信息
*/
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;
}
}

/*
* 通过参数形式初始化 CosAPI 对象
*/
qcloud_cos::CosAPI InitCosAPI() {
qcloud_cos::CosConfig config(appid, tmp_secret_id, tmp_secret_key, region);
config.SetTmpToken(tmp_token); // 推荐使用临时密钥初始化 CosAPI 对象, 如果您使用永久密钥初始化 CosAPI 对象,请注释
qcloud_cos::CosAPI cos_tmp(config);
return cos_tmp;
}

void DeleteBucket(qcloud_cos::CosAPI& cos) {
qcloud_cos::DeleteBucketReq req(bucket_name);
qcloud_cos::DeleteBucketResp resp;
qcloud_cos::CosResult result = cos.DeleteBucket(req, &resp);

std::cout << "===================DeleteBucketResponse====================="
<< std::endl;
PrintResult(result, resp);
std::cout << "========================================================="
<< std::endl;
}


int main() {
qcloud_cos::CosAPI cos = InitCosAPI();
CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
DeleteBucket(cos);
}
80 changes: 80 additions & 0 deletions demo/bucket_op_demo/get_bucket_list_demo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

#include <stdlib.h>
#include <sys/stat.h>

#include <iostream>
#include <map>
#include <string>
#include <thread>
#include <vector>

#include "cos_api.h"
#include "cos_sys_config.h"
#include "util/auth_tool.h"

/**
* 本样例演示了如何使用 COS C++ SDK 进行存储桶列表的获取
*/
using namespace qcloud_cos;

uint64_t appid = 12500000000;
std::string tmp_secret_id = "AKIDXXXXXXXX";
std::string tmp_secret_key = "1A2Z3YYYYYYYYYY";
std::string region = "ap-guangzhou";
std::string tmp_token = "token";

/*
* 本方法包含调用是否正常的判断,和请求结果的输出
* 可通过本方法判断是否请求成功,并输出结果信息
*/
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;
}
}

/*
* 通过参数形式初始化 CosAPI 对象
*/
qcloud_cos::CosAPI InitCosAPI() {
qcloud_cos::CosConfig config(appid, tmp_secret_id, tmp_secret_key, region);
config.SetTmpToken(tmp_token); // 推荐使用临时密钥初始化 CosAPI 对象, 如果您使用永久密钥初始化 CosAPI 对象,请注释
qcloud_cos::CosAPI cos_tmp(config);
return cos_tmp;
}

void GetService(qcloud_cos::CosAPI& cos) {
qcloud_cos::GetServiceReq req;
qcloud_cos::GetServiceResp resp;
qcloud_cos::CosResult result = cos.GetService(req, &resp);

std::cout << "===================GetService====================="
<< std::endl;
PrintResult(result, resp);
const qcloud_cos::Owner& owner = resp.GetOwner();
const std::vector<qcloud_cos::Bucket>& buckets = resp.GetBuckets();
std::cout << "owner.m_id=" << owner.m_id << ", owner.display_name=" << owner.m_display_name << std::endl;
for (std::vector<qcloud_cos::Bucket>::const_iterator itr = buckets.begin();
itr != buckets.end(); ++itr) {
const qcloud_cos::Bucket& bucket = *itr;
std::cout << "Bucket name=" << bucket.m_name
<< ", location=" << bucket.m_location
<< ", create_date=" << bucket.m_create_date << std::endl;
}
std::cout << "=========================================================" << std::endl;
}

int main() {
qcloud_cos::CosAPI cos = InitCosAPI();
CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
GetService(cos);
}
84 changes: 84 additions & 0 deletions demo/bucket_op_demo/head_bucket_demo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@

#include <stdlib.h>
#include <sys/stat.h>

#include <iostream>
#include <map>
#include <string>
#include <thread>
#include <vector>

#include "cos_api.h"
#include "cos_sys_config.h"
#include "util/auth_tool.h"

/**
* 本样例演示了如何使用 COS C++ SDK 进行存储桶检索和判断是否存在
*/
using namespace qcloud_cos;

uint64_t appid = 12500000000;
std::string tmp_secret_id = "AKIDXXXXXXXX";
std::string tmp_secret_key = "1A2Z3YYYYYYYYYY";
std::string region = "ap-guangzhou";
std::string bucket_name = "examplebucket-12500000000";
std::string tmp_token = "token";

/*
* 本方法包含调用是否正常的判断,和请求结果的输出
* 可通过本方法判断是否请求成功,并输出结果信息
*/
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;
}
}

/*
* 通过参数形式初始化 CosAPI 对象
*/
qcloud_cos::CosAPI InitCosAPI() {
qcloud_cos::CosConfig config(appid, tmp_secret_id, tmp_secret_key, region);
config.SetTmpToken(tmp_token); // 推荐使用临时密钥初始化 CosAPI 对象, 如果您使用永久密钥初始化 CosAPI 对象,请注释
qcloud_cos::CosAPI cos_tmp(config);
return cos_tmp;
}

void HeadBucket(qcloud_cos::CosAPI& cos) {
qcloud_cos::HeadBucketReq req(bucket_name);
qcloud_cos::HeadBucketResp resp;
qcloud_cos::CosResult result = cos.HeadBucket(req, &resp);

std::cout << "===================HeadBucketResponse====================="
<< std::endl;
PrintResult(result, resp);
std::cout << "=========================================================="
<< std::endl;
}

void IsBucketExist(qcloud_cos::CosAPI& cos) {
std::cout << "===================IsBucketExist====================="
<< std::endl;
std::cout << (cos.IsBucketExist("abcdefg") ? "true" : "false") << std::endl;
std::cout << (cos.IsBucketExist(bucket_name) ? "true" : "false") << std::endl;
std::cout
<< "===================================================================="
<< std::endl;
}


int main() {
qcloud_cos::CosAPI cos = InitCosAPI();
CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
HeadBucket(cos);
IsBucketExist(cos);
}
85 changes: 85 additions & 0 deletions demo/bucket_op_demo/put_bucket_demo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@

#include <stdlib.h>
#include <sys/stat.h>

#include <iostream>
#include <map>
#include <string>
#include <thread>
#include <vector>

#include "cos_api.h"
#include "cos_sys_config.h"
#include "util/auth_tool.h"

/**
* 本样例演示了如何使用 COS C++ SDK 进行存储桶的创建
*/
using namespace qcloud_cos;

uint64_t appid = 12500000000;
std::string tmp_secret_id = "AKIDXXXXXXXX";
std::string tmp_secret_key = "1A2Z3YYYYYYYYYY";
std::string region = "ap-guangzhou@xxx";
std::string bucket_name = "examplebucket-12500000000";
std::string tmp_token = "token";

/*
* 本方法包含调用是否正常的判断,和请求结果的输出
* 可通过本方法判断是否请求成功,并输出结果信息
*/
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;
}
}

/*
* 通过参数形式初始化 CosAPI 对象
*/
qcloud_cos::CosAPI InitCosAPI() {
qcloud_cos::CosConfig config(appid, tmp_secret_id, tmp_secret_key, region);
config.SetTmpToken(tmp_token); // 推荐使用临时密钥初始化 CosAPI 对象, 如果您使用永久密钥初始化 CosAPI 对象,请注释
qcloud_cos::CosAPI cos_tmp(config);
return cos_tmp;
}

void PutBucket(qcloud_cos::CosAPI& cos) {
qcloud_cos::PutBucketReq req(bucket_name);
//创建MAZ存储桶使用下方进行设置
//req.SetMAZBucket();
qcloud_cos::PutBucketResp resp;
qcloud_cos::CosResult result = cos.PutBucket(req, &resp);

std::cout << "===================PutBucketResponse====================="
<< std::endl;
PrintResult(result, resp);
std::cout << "========================================================="
<< std::endl;
}


int main() {
try{
qcloud_cos::CosAPI cos = InitCosAPI();
CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);

PutBucket(cos);
}
catch(const std::exception& e){
std::cerr << e.what() << "]]" << '\n';

std::cout << (strcmp(e.what(),"Invalid region configuration in CosConfig :ap-guangzhou@xxx") == 0) << std::endl;
}
std::cout << "Hello, World!" << std::endl;

}
2 changes: 2 additions & 0 deletions include/cos_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ class CosConfig {

bool IsDomainSameToHostEnable() const;

bool CheckRegion();

// void SetDomainSameToHostEnable(bool is_domain_same_to_host_enable);

/// \brief 设置日志回调
Expand Down
10 changes: 9 additions & 1 deletion include/request/bucket_req.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,26 @@
#include "cos_defines.h"
#include "request/base_req.h"
#include "util/string_util.h"
#include "util/illegal_intercept.h"

namespace qcloud_cos {

class BucketReq : public BaseReq {
public:
BucketReq() : m_bucket_name("") {}
explicit BucketReq(const std::string& bucket_name) : m_bucket_name(bucket_name) {}
explicit BucketReq(const std::string& bucket_name) : m_bucket_name(bucket_name) {
if (!IllegalIntercept::CheckBucket(bucket_name)){
throw std::invalid_argument("Invalid bucket_name argument :" + bucket_name);
}
}
virtual ~BucketReq() {}

// getter and setter
std::string GetBucketName() const { return m_bucket_name; }
void SetBucketName(const std::string& bucket_name) {
if (!IllegalIntercept::CheckBucket(bucket_name)){
throw std::invalid_argument("Invalid bucket_name argument :" + bucket_name);
}
m_bucket_name = bucket_name;
}
virtual bool GenerateRequestBody(std::string* body) const { UNUSED_PARAM(body); return true; }
Expand Down
Loading

0 comments on commit 549a3af

Please sign in to comment.