BG
This commit is contained in:
76
demo/CMakeLists.txt
Normal file
76
demo/CMakeLists.txt
Normal file
@@ -0,0 +1,76 @@
|
||||
project(cos-demo)
|
||||
|
||||
if (NOT ${OS_TYPE} STREQUAL "WINDOWS")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-function")
|
||||
endif()
|
||||
file(GLOB cos_demo_src "${CMAKE_SOURCE_DIR}/demo/cos_demo.cpp")
|
||||
file(GLOB put_object_demo_src "${CMAKE_SOURCE_DIR}/demo/object_op_demo/put_object_demo.cpp")
|
||||
file(GLOB get_object_url_demo_src "${CMAKE_SOURCE_DIR}/demo/object_op_demo/get_object_url_demo.cpp")
|
||||
file(GLOB head_object_demo_src "${CMAKE_SOURCE_DIR}/demo/object_op_demo/head_object_demo.cpp")
|
||||
file(GLOB get_object_demo_src "${CMAKE_SOURCE_DIR}/demo/object_op_demo/get_object_demo.cpp")
|
||||
file(GLOB delete_object_demo_src "${CMAKE_SOURCE_DIR}/demo/object_op_demo/delete_object_demo.cpp")
|
||||
file(GLOB put_get_object_acl_demo_src "${CMAKE_SOURCE_DIR}/demo/object_op_demo/put_get_object_acl_demo.cpp")
|
||||
file(GLOB put_get_delete_object_tagging_demo_src "${CMAKE_SOURCE_DIR}/demo/object_op_demo/put_get_delete_object_tagging_demo.cpp")
|
||||
file(GLOB restore_object_demo_src "${CMAKE_SOURCE_DIR}/demo/object_op_demo/restore_object_demo.cpp")
|
||||
file(GLOB copy_move_object_demo_src "${CMAKE_SOURCE_DIR}/demo/object_op_demo/copy_move_object_demo.cpp")
|
||||
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")
|
||||
file(GLOB get_bucket_demo_src "${CMAKE_SOURCE_DIR}/demo/bucket_op_demo/get_bucket_demo.cpp")
|
||||
|
||||
link_directories(${POCO_LINK_DIR} ${OPENSSL_LINK_DIR}) #这一行要放到add_executable前面
|
||||
|
||||
add_executable(cos_demo ${cos_demo_src})
|
||||
add_executable(put_object_demo ${put_object_demo_src})
|
||||
add_executable(get_object_url_demo ${get_object_url_demo_src})
|
||||
add_executable(head_object_demo ${head_object_demo_src})
|
||||
add_executable(get_object_demo ${get_object_demo_src})
|
||||
add_executable(delete_object_demo ${delete_object_demo_src})
|
||||
add_executable(put_get_object_acl_demo ${put_get_object_acl_demo_src})
|
||||
add_executable(put_get_delete_object_tagging_demo ${put_get_delete_object_tagging_demo_src})
|
||||
add_executable(restore_object_demo ${restore_object_demo_src})
|
||||
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})
|
||||
add_executable(get_bucket_demo ${get_bucket_demo_src})
|
||||
|
||||
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
|
||||
|
||||
target_link_libraries(cos_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
|
||||
target_link_libraries(put_object_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
|
||||
target_link_libraries(get_object_url_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
|
||||
target_link_libraries(head_object_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
|
||||
target_link_libraries(get_object_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
|
||||
target_link_libraries(delete_object_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
|
||||
target_link_libraries(put_get_object_acl_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
|
||||
target_link_libraries(put_get_delete_object_tagging_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
|
||||
target_link_libraries(restore_object_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
|
||||
target_link_libraries(copy_move_object_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
|
||||
target_link_libraries(multi_put_object_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
|
||||
target_link_libraries(multi_get_object_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
|
||||
target_link_libraries(select_objec_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
|
||||
target_link_libraries(get_bucket_list_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
|
||||
target_link_libraries(delete_bucket_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
|
||||
target_link_libraries(head_bucket_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
|
||||
target_link_libraries(put_bucket_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
|
||||
target_link_libraries(get_bucket_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
|
||||
|
||||
include_directories(${CMAKE_SOURCE_DIR}/include/ ${POCO_INCLUDE_DIR})
|
||||
|
||||
if(${OS_TYPE} STREQUAL "WINDOWS")
|
||||
file(GLOB poco_libs "${CMAKE_SOURCE_DIR}/third_party/lib/${BUILD_TARGET}/poco/*")
|
||||
file(GLOB ssl_libs "${CMAKE_SOURCE_DIR}/third_party/lib/${BUILD_TARGET}/openssl/*")
|
||||
file(COPY ${poco_libs}
|
||||
DESTINATION ${CMAKE_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE})
|
||||
file(COPY ${ssl_libs}
|
||||
DESTINATION ${CMAKE_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE})
|
||||
endif()
|
||||
73
demo/bucket_op_demo/delete_bucket_demo.cpp
Normal file
73
demo/bucket_op_demo/delete_bucket_demo.cpp
Normal file
@@ -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);
|
||||
}
|
||||
78
demo/bucket_op_demo/get_bucket_demo.cpp
Normal file
78
demo/bucket_op_demo/get_bucket_demo.cpp
Normal file
@@ -0,0 +1,78 @@
|
||||
|
||||
#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 GetBucket(qcloud_cos::CosAPI& cos) {
|
||||
qcloud_cos::GetBucketReq req(bucket_name);
|
||||
qcloud_cos::GetBucketResp resp;
|
||||
qcloud_cos::CosResult result = cos.GetBucket(req, &resp);
|
||||
|
||||
std::cout << "===================GetBucket=====================" << std::endl;
|
||||
std::vector<qcloud_cos::Content> cotents = resp.GetContents();
|
||||
for (std::vector<qcloud_cos::Content>::const_iterator itr = cotents.begin(); itr != cotents.end(); ++itr) {
|
||||
const qcloud_cos::Content& content = *itr;
|
||||
std::cout << "key name=" << content.m_key << ", lastmodified ="
|
||||
<< content.m_last_modified << ", size=" << content.m_size << std::endl;
|
||||
}
|
||||
|
||||
PrintResult(result, resp);
|
||||
std::cout << "=========================================================" << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
qcloud_cos::CosAPI cos = InitCosAPI();
|
||||
CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
|
||||
GetBucket(cos);
|
||||
}
|
||||
80
demo/bucket_op_demo/get_bucket_list_demo.cpp
Normal file
80
demo/bucket_op_demo/get_bucket_list_demo.cpp
Normal file
@@ -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
demo/bucket_op_demo/head_bucket_demo.cpp
Normal file
84
demo/bucket_op_demo/head_bucket_demo.cpp
Normal file
@@ -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
demo/bucket_op_demo/put_bucket_demo.cpp
Normal file
85
demo/bucket_op_demo/put_bucket_demo.cpp
Normal file
@@ -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;
|
||||
|
||||
}
|
||||
20
demo/config.json
Normal file
20
demo/config.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"SecretId":"*****************************",
|
||||
"SecretKey":"****************************",
|
||||
"Region":"ap-guangzhou",
|
||||
"SignExpiredTime":360,
|
||||
"ConnectTimeoutInms":6000,
|
||||
"ReceiveTimeoutInms":5000,
|
||||
"AsynThreadPoolSize":2,
|
||||
"UploadPartSize":10485760,
|
||||
"UploadCopyPartSize":10485760,
|
||||
"UploadThreadPoolSize":5,
|
||||
"LogoutType":1,
|
||||
"LogLevel":3,
|
||||
"DownloadThreadPoolSize":5,
|
||||
"DownloadSliceSize":4194304,
|
||||
"IsDomainSameToHost":false,
|
||||
"DestDomain":"",
|
||||
"IsUseIntranet":false,
|
||||
"IntranetAddr":""
|
||||
}
|
||||
4185
demo/cos_demo.cpp
Normal file
4185
demo/cos_demo.cpp
Normal file
File diff suppressed because it is too large
Load Diff
194
demo/object_op_demo/copy_move_object_demo.cpp
Normal file
194
demo/object_op_demo/copy_move_object_demo.cpp
Normal file
@@ -0,0 +1,194 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
/*
|
||||
* 该 Demo 示范如何拷贝一个对象至当前 CosAPI 指定的存储桶中
|
||||
* 最大支持拷贝不超过5GB的对象,支持跨区域复制
|
||||
*/
|
||||
void PutObjectCopyDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string object_name = "test_dst.txt"; // 复制目标对象名称
|
||||
std::string source = bucket_name + ".cos." + region + ".myqcloud.com/test.txt"; // 复制源对象
|
||||
qcloud_cos::PutObjectCopyReq req(bucket_name, object_name);
|
||||
req.SetXCosCopySource(source);
|
||||
qcloud_cos::PutObjectCopyResp resp;
|
||||
qcloud_cos::CosResult result = cos.PutObjectCopy(req, &resp);
|
||||
|
||||
std::cout << "===================PutObjectCopyResponse====================" << std::endl;
|
||||
PrintResult(result, resp);
|
||||
std::cout << "============================================================" << std::endl;
|
||||
}
|
||||
|
||||
/*
|
||||
* 该 Demo 示范如何使用高级复制接口拷贝对象至当前 CosAPI 指定的存储桶中
|
||||
* 该接口封装了简单拷贝和分块拷贝,根据文件大小智能的选择拷贝对象的方式
|
||||
* 支持大对象,支持跨区域复制。推荐调用此接口
|
||||
* 通过全局可以设置分块拷贝分块大小
|
||||
*/
|
||||
void CopyDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string object_name = "test_dst.txt"; // 复制后对象名称
|
||||
std::string source = bucket_name + ".cos." + region + ".myqcloud.com/test_src.txt"; ; // 复制源对象
|
||||
|
||||
// 设置分块拷贝的分块大小,默认20MB,最大支持5GB,最小支持1MB
|
||||
// 此配置是全局性配置,主动设置后,后续初始化的所有CopyReq,都使用此配置
|
||||
CosSysConfig::SetUploadCopyPartSize(20 * 1024 * 1024);
|
||||
|
||||
qcloud_cos::CopyReq req(bucket_name, object_name);
|
||||
qcloud_cos::CopyResp resp;
|
||||
|
||||
req.SetXCosCopySource(source);
|
||||
qcloud_cos::CosResult result = cos.Copy(req, &resp);
|
||||
|
||||
std::cout << "===========================Copy=============================" << std::endl;
|
||||
PrintResult(result, resp);
|
||||
std::cout << "============================================================" << std::endl;
|
||||
}
|
||||
|
||||
/*
|
||||
* 该 Demo 示范如何在当前CosAPI的存储桶中移动一个对象
|
||||
* 支持大对象移动,但仅支持当前存储桶
|
||||
* 如需要移动跨桶对象,请使用 PutObjectCopy 或 Copy 接口复制后删除原文件
|
||||
*/
|
||||
void MoveObjectDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string src_object = "test_src.txt";
|
||||
std::string dst_object = "test_dst.txt";
|
||||
qcloud_cos::MoveObjectReq req(bucket_name, src_object, dst_object);
|
||||
|
||||
qcloud_cos::CosResult result = cos.MoveObject(req);
|
||||
|
||||
std::cout << "========================MoveObject==========================" << std::endl;
|
||||
if (result.IsSucc()) {
|
||||
std::cout << "MoveObject Succ." << std::endl;
|
||||
} else {
|
||||
std::cout << "MoveObject Fail, ErrorMsg: " << result.GetErrorMsg() << std::endl;
|
||||
}
|
||||
std::cout << "============================================================" << std::endl;
|
||||
}
|
||||
|
||||
/*
|
||||
* 该 Demo 示范用户如何自行组合分块操作拷贝对象到目标存储桶
|
||||
* 分块操作基于初始化、拷贝分块、完成分块三个接口实现将整个源对象切分为多个分块,然后再将这些分块拷贝到目标存储桶中
|
||||
* 支持大对象,支持跨区域,其中每个分块最大支持 5GB,最小支持 1MB,最后一个分块可以小于 1MB
|
||||
*/
|
||||
void CopyPartDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string dst_object = "test_dst.txt";
|
||||
std::string source = bucket_name + ".cos." + region + ".myqcloud.com/test_src.txt"; // 复制源对象
|
||||
|
||||
// 1. InitMultiUpload
|
||||
qcloud_cos::InitMultiUploadReq init_req(bucket_name, dst_object);
|
||||
qcloud_cos::InitMultiUploadResp init_resp;
|
||||
qcloud_cos::CosResult init_result = cos.InitMultiUpload(init_req, &init_resp);
|
||||
|
||||
std::cout << "=====================InitMultiUpload=====================" << std::endl;
|
||||
PrintResult(init_result, init_resp);
|
||||
std::cout << "=========================================================" << std::endl;
|
||||
|
||||
// 2. UploadPartCopyData
|
||||
// UploadPartCopyData 部分,可以根据实际选择分块数量和每次分块的 range,这里以 2 个分块为例
|
||||
std::vector<std::string> etags;
|
||||
std::vector<uint64_t> part_numbers;
|
||||
std::string upload_id = init_resp.GetUploadId();
|
||||
{
|
||||
uint64_t part_number = 1;
|
||||
qcloud_cos::UploadPartCopyDataReq req(bucket_name, dst_object, upload_id, part_number);
|
||||
req.SetXCosCopySource(source);
|
||||
req.SetXCosCopySourceRange("bytes=0-104857599"); // 可根据实际选择 range
|
||||
qcloud_cos::UploadPartCopyDataResp resp;
|
||||
qcloud_cos::CosResult result = cos.UploadPartCopyData(req, &resp);
|
||||
std::cout << "==================UploadPartCopyDataResp1=====================" << std::endl;
|
||||
PrintResult(result, resp);
|
||||
std::cout << "==============================================================" << std::endl;
|
||||
if (result.IsSucc()) {
|
||||
part_numbers.push_back(1);
|
||||
etags.push_back(resp.GetEtag());
|
||||
}
|
||||
}
|
||||
{
|
||||
uint64_t part_number = 2;
|
||||
qcloud_cos::UploadPartCopyDataReq req(bucket_name, dst_object, upload_id, part_number);
|
||||
req.SetXCosCopySource(source);
|
||||
req.SetXCosCopySourceRange("bytes=104857600-209715199"); // 可根据实际选择 range
|
||||
qcloud_cos::UploadPartCopyDataResp resp;
|
||||
qcloud_cos::CosResult result = cos.UploadPartCopyData(req, &resp);
|
||||
|
||||
std::cout << "==================UploadPartCopyDataResp2=====================" << std::endl;
|
||||
PrintResult(result, resp);
|
||||
std::cout << "==============================================================" << std::endl;
|
||||
if (result.IsSucc()) {
|
||||
part_numbers.push_back(2);
|
||||
etags.push_back(resp.GetEtag());
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Complete
|
||||
CompleteMultiUploadReq comp_req(bucket_name, dst_object, upload_id);
|
||||
CompleteMultiUploadResp comp_resp;
|
||||
|
||||
comp_req.SetEtags(etags);
|
||||
comp_req.SetPartNumbers(part_numbers);
|
||||
|
||||
qcloud_cos::CosResult result = cos.CompleteMultiUpload(comp_req, &comp_resp);
|
||||
|
||||
std::cout << "===================Complete=============================" << std::endl;
|
||||
PrintResult(result, comp_resp);
|
||||
std::cout << "========================================================" << std::endl;
|
||||
|
||||
return;
|
||||
}
|
||||
int main() {
|
||||
qcloud_cos::CosAPI cos = InitCosAPI();
|
||||
CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
|
||||
PutObjectCopyDemo(cos);
|
||||
CopyDemo(cos);
|
||||
MoveObjectDemo(cos);
|
||||
CopyPartDemo(cos);
|
||||
}
|
||||
152
demo/object_op_demo/delete_object_demo.cpp
Normal file
152
demo/object_op_demo/delete_object_demo.cpp
Normal file
@@ -0,0 +1,152 @@
|
||||
#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 DeleteObjectDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string object_name = "test.txt";
|
||||
qcloud_cos::DeleteObjectReq req(bucket_name, object_name);
|
||||
// req.SetXCosVersionId("xxxxx");// 可以指定删除的版本
|
||||
qcloud_cos::DeleteObjectResp resp;
|
||||
qcloud_cos::CosResult result = cos.DeleteObject(req, &resp);
|
||||
|
||||
std::cout << "===================DeleteObjectResponse=====================" << std::endl;
|
||||
PrintResult(result, resp);
|
||||
std::cout << "============================================================" << std::endl;
|
||||
}
|
||||
|
||||
/*
|
||||
* 该 Demo 示范如何批量删除对象
|
||||
* 该方法是否成功,需要判断resp.GetErrorMsgs()是否为空
|
||||
*/
|
||||
void DeleteObjectsDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::vector<ObjectVersionPair> to_be_deleted;
|
||||
{
|
||||
ObjectVersionPair pair;
|
||||
std::string object_name = "test_dir/audio.mp3";
|
||||
std::string version_id = ""; // 有需要可以指定删除的版本,如果为空则不指定
|
||||
to_be_deleted.push_back(ObjectVersionPair(object_name, version_id));
|
||||
}
|
||||
{
|
||||
std::string object_name = "test_dir/video.mp4";
|
||||
std::string version_id = ""; // 有需要可以指定删除的版本,如果为空则不指定
|
||||
to_be_deleted.push_back(ObjectVersionPair(object_name, version_id));
|
||||
}
|
||||
|
||||
qcloud_cos::DeleteObjectsReq req(bucket_name, to_be_deleted);
|
||||
// req.SetQuiet(); // 设置为 Quiet模式, 则不返回删除成功的对象信息,默认为 Verbose 模式
|
||||
qcloud_cos::DeleteObjectsResp resp;
|
||||
qcloud_cos::CosResult result = cos.DeleteObjects(req, &resp);
|
||||
|
||||
std::cout << "===================DeleteObjectsResponse=====================" << std::endl;
|
||||
std::vector<DeletedInfo> deleted_infos = resp.GetDeletedInfos(); // 单个删除成功的对象条目,仅当使用 Verbose 模式才会返回该元素
|
||||
std::vector<ErrorInfo> error_infos = resp.GetErrorMsgs(); // 单个删除失败的对象条目
|
||||
if (!error_infos.empty()) {
|
||||
std::cout << "==================Failed part message==================" << std::endl;
|
||||
for (ErrorInfo& error_info : error_infos) {
|
||||
std::cout << "key: " << error_info.m_key << "\ncode: " << error_info.m_code << "\nmessage: " << error_info.m_message << std::endl;
|
||||
std::cout << "====================================" << std::endl;
|
||||
}
|
||||
} else {
|
||||
std::cout << "DeleteObjects All Succ." << std::endl;
|
||||
}
|
||||
std::cout << "=============================================================" << std::endl;
|
||||
}
|
||||
|
||||
void DeleteDirectoryDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string directory_name = "test_dir/"; // 目录名称,注意末尾需要有/
|
||||
DeleteObjectsByPrefixReq req(bucket_name, directory_name);
|
||||
DeleteObjectsByPrefixResp resp;
|
||||
CosResult result = cos.DeleteObjects(req, &resp);
|
||||
|
||||
std::cout << "===================DeleteDirectory=====================" << std::endl;
|
||||
if (result.IsSucc()) {
|
||||
std::cout << "DeleteDirectory Succ." << std::endl;
|
||||
} else {
|
||||
std::cout << "DeleteDirectory Fail, ErrorMsg: " << result.GetErrorMsg() << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "Succ del objs:" << std::endl;
|
||||
for (auto& obj : resp.m_succ_del_objs) {
|
||||
std::cout << obj << std::endl;
|
||||
}
|
||||
std::cout << "=======================================================" << std::endl;
|
||||
}
|
||||
|
||||
void DeleteObjectsByPrefixDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string prefix = "test_dir"; // 指定前缀,test_dir目录下的所有对象均会被删除,且以test_dir开头的对象也会被删除
|
||||
DeleteObjectsByPrefixReq req(bucket_name, prefix);
|
||||
DeleteObjectsByPrefixResp resp;
|
||||
CosResult result = cos.DeleteObjects(req, &resp);
|
||||
|
||||
std::cout << "===================DeleteObjectsByPrefix=====================" << std::endl;
|
||||
if (result.IsSucc()) {
|
||||
std::cout << "DeleteObjectsByPrefix Succ." << std::endl;
|
||||
} else {
|
||||
std::cout << "DeleteObjectsByPrefix Fail, ErrorMsg: "
|
||||
<< result.GetErrorMsg() << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "Succ del objs:" << std::endl;
|
||||
for (auto& obj : resp.m_succ_del_objs) {
|
||||
std::cout << obj << std::endl;
|
||||
}
|
||||
std::cout << "=============================================================" << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
qcloud_cos::CosAPI cos = InitCosAPI();
|
||||
CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
|
||||
DeleteObjectDemo(cos);
|
||||
DeleteObjectsDemo(cos);
|
||||
DeleteDirectoryDemo(cos);
|
||||
DeleteObjectsByPrefixDemo(cos);
|
||||
}
|
||||
167
demo/object_op_demo/get_object_demo.cpp
Normal file
167
demo/object_op_demo/get_object_demo.cpp
Normal file
@@ -0,0 +1,167 @@
|
||||
#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"
|
||||
#include <openssl/ssl.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";
|
||||
|
||||
/**
|
||||
* 本方法为 SSL_CTX 的回调方法,用户可以在此方法中配置 SSL_CTX 信息
|
||||
*/
|
||||
int SslCtxCallback(void *ssl_ctx, void *data) {
|
||||
std::cout << "ssl_ctx: " << ssl_ctx << " data: " << data << std::endl;
|
||||
|
||||
SSL_CTX *ctx = (SSL_CTX *)ssl_ctx;
|
||||
std::cout << "ssl_ctx in" << std::endl;
|
||||
SSL_CTX_use_PrivateKey_file(ctx, "/data/cert/client.key", SSL_FILETYPE_PEM);
|
||||
SSL_CTX_use_certificate_chain_file(ctx, "/data/cert/client.crt");
|
||||
std::cout << "ssl_ctx out" << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 本方法包含调用是否正常的判断,和请求结果的输出
|
||||
* 可通过本方法判断是否请求成功,并输出结果信息
|
||||
*/
|
||||
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 GetObjectByFileDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string object_name = "test_src.txt";
|
||||
std::string file_path = "./test_file/text2.txt";
|
||||
qcloud_cos::GetObjectByFileReq req(bucket_name, object_name, file_path);
|
||||
// 限速上传对象,默认单位为 bit/s,限速值设置范围为 819200 - 838860800
|
||||
// uint64_t traffic_limit = 0;
|
||||
// req.SetTrafficLimit(traffic_limit);
|
||||
qcloud_cos::GetObjectByFileResp resp;
|
||||
|
||||
qcloud_cos::CosResult result = cos.GetObject(req, &resp);
|
||||
std::cout << "===================GetObjectResponse=====================" << std::endl;
|
||||
PrintResult(result, resp);
|
||||
std::cout << "=========================================================" << std::endl;
|
||||
}
|
||||
|
||||
void GetObjectByStreamDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string object_name = "test.txt";
|
||||
std::ostringstream os;
|
||||
qcloud_cos::GetObjectByStreamReq req(bucket_name, object_name, os);
|
||||
// 限速上传对象,默认单位为 bit/s,限速值设置范围为 819200 - 838860800
|
||||
// uint64_t traffic_limit = 0;
|
||||
// req.SetTrafficLimit(traffic_limit);
|
||||
qcloud_cos::GetObjectByStreamResp resp;
|
||||
|
||||
qcloud_cos::CosResult result = cos.GetObject(req, &resp);
|
||||
std::cout << "===================GetObjectResponse=====================" << std::endl;
|
||||
PrintResult(result, resp);
|
||||
std::cout << "=========================================================" << std::endl;
|
||||
std::cout << os.str() << std::endl;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用 https 双向认证
|
||||
*/
|
||||
void GetObjectByStreamDemoWithMutualAuthentication(qcloud_cos::CosAPI& cos) {
|
||||
std::string object_name = "index.html";
|
||||
std::ostringstream os;
|
||||
qcloud_cos::GetObjectByStreamReq req(bucket_name, object_name, os);
|
||||
req.SetHttps();
|
||||
req.SetSSLCtxCallback(SslCtxCallback, nullptr);
|
||||
qcloud_cos::GetObjectByStreamResp resp;
|
||||
|
||||
qcloud_cos::CosResult result = cos.GetObject(req, &resp);
|
||||
std::cout << "===================GetObjectResponse=====================" << std::endl;
|
||||
PrintResult(result, resp);
|
||||
std::cout << "=========================================================" << std::endl;
|
||||
std::cout << os.str() << std::endl;
|
||||
}
|
||||
|
||||
void GetBucketDemo(qcloud_cos::CosAPI& cos) {
|
||||
qcloud_cos::GetBucketReq req(bucket_name);
|
||||
// 设置列出的对象名以 prefix 为前缀
|
||||
req.SetPrefix("test");
|
||||
// 设置最大列出多少个对象, 一次 listobject 最大支持1000
|
||||
req.SetMaxKeys(10);
|
||||
qcloud_cos::GetBucketResp resp;
|
||||
qcloud_cos::CosResult result = cos.GetBucket(req, &resp);
|
||||
|
||||
std::cout << "===================GetBucketResponse=====================" << std::endl;
|
||||
if (result.IsSucc()) {
|
||||
// object contents 表示此次列出的对象列表
|
||||
std::vector<Content> contents = resp.GetContents();
|
||||
for (const Content& content : contents) {
|
||||
// 对象的 key
|
||||
std::string key = content.m_key;
|
||||
// 对象的 etag
|
||||
std::string etag = content.m_etag;
|
||||
// 对象的长度
|
||||
std::string file_size = content.m_size;
|
||||
// 对象的存储类型
|
||||
std::string storage_classes = content.m_storage_class;
|
||||
std::cout << "key:" << key << "\netag:" << etag << "\nfile_size:" << file_size << "\nstorage_classes" << storage_classes << std::endl;
|
||||
std::cout << "==================================" << std::endl;
|
||||
}
|
||||
|
||||
if (resp.IsTruncated()) {
|
||||
// 表示还没有列完,被截断了
|
||||
|
||||
// 下一次开始的位置
|
||||
std::string next_marker = resp.GetNextMarker();
|
||||
std::cout << "next_marker:" << next_marker << std::endl;
|
||||
}
|
||||
} else {
|
||||
std::cout << "GetBucket Fail, ErrorMsg: " << result.GetErrorMsg() << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "=========================================================" << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
qcloud_cos::CosAPI cos = InitCosAPI();
|
||||
CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
|
||||
GetObjectByFileDemo(cos);
|
||||
GetObjectByStreamDemo(cos);
|
||||
// GetObjectByStreamDemoWithMutualAuthentication(cos);
|
||||
GetBucketDemo(cos);
|
||||
}
|
||||
86
demo/object_op_demo/get_object_url_demo.cpp
Normal file
86
demo/object_op_demo/get_object_url_demo.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
#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 获取对象链接
|
||||
* 包括:预签名链接、对象访问 URL
|
||||
*/
|
||||
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 GeneratePresignedUrlDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string object_name = "test.txt";
|
||||
qcloud_cos::GeneratePresignedUrlReq req(bucket_name, object_name, qcloud_cos::HTTP_GET); // 可设置请求方法
|
||||
// 下方代码块可选则是否设置。默认签名生效的开始时间是本地当前时间,默认有效期 60s
|
||||
{
|
||||
req.SetHttps(); // 是否为 https
|
||||
uint64_t start_time_in_s = time(NULL);
|
||||
req.SetStartTimeInSec(start_time_in_s); // 设置签名生效的开始时间
|
||||
req.SetExpiredTimeInSec(60); // 设置签名的有效期
|
||||
}
|
||||
std::string presigned_url = cos.GeneratePresignedUrl(req);
|
||||
|
||||
std::cout << "===================GeneratePresignedUrl=====================" << std::endl;
|
||||
std::cout << "Presigend Url=[" << presigned_url << "]" << std::endl;
|
||||
std::cout << "============================================================" << std::endl;
|
||||
}
|
||||
|
||||
void GetObjectUrlDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string object_name = "test.txt";
|
||||
bool is_https = true; // 是否为 https
|
||||
std::string object_url = cos.GetObjectUrl(bucket_name, object_name, is_https);
|
||||
|
||||
std::cout << "=======================GetObjectUrl=========================" << std::endl;
|
||||
std::cout << "object url=[" << object_url << "]" << std::endl;
|
||||
std::cout << "============================================================" << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
qcloud_cos::CosAPI cos = InitCosAPI();
|
||||
CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
|
||||
GeneratePresignedUrlDemo(cos);
|
||||
GetObjectUrlDemo(cos);
|
||||
}
|
||||
82
demo/object_op_demo/head_object_demo.cpp
Normal file
82
demo/object_op_demo/head_object_demo.cpp
Normal file
@@ -0,0 +1,82 @@
|
||||
#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 进行 Head Object 相关操作
|
||||
* 包括:Head Object、判断对象是否存在
|
||||
*/
|
||||
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 IsObjectExistDemo(qcloud_cos::CosAPI& cos) {
|
||||
bool is_exist = cos.IsObjectExist(bucket_name, "test.txt");
|
||||
|
||||
std::cout << "=====================IsObjectExist=======================" << std::endl;
|
||||
std::cout << (is_exist ? "true" : "false") << std::endl;
|
||||
std::cout << "=========================================================" << std::endl;
|
||||
}
|
||||
|
||||
void HeadObjectDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string object_name = "test.txt";
|
||||
qcloud_cos::HeadObjectReq req(bucket_name, object_name);
|
||||
qcloud_cos::HeadObjectResp resp;
|
||||
qcloud_cos::CosResult result = cos.HeadObject(req, &resp);
|
||||
|
||||
std::cout << "===================HeadObjectResponse=====================" << std::endl;
|
||||
std::map<std::string, std::string> cos_metas = resp.GetXCosMetas(); // 获取自定义的元数据map
|
||||
std::string cos_meta = resp.GetXCosMeta("x-cos-meta-*"); // 获取指定自定义的元数据
|
||||
std::string restore = resp.GetXCosRestore(); // 获得 archive 类型对象的当前恢复状态
|
||||
std::string sse = resp.GetXCosServerSideEncryption(); // Server端加密使用的算法
|
||||
PrintResult(result, resp);
|
||||
std::cout << "==========================================================" << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
qcloud_cos::CosAPI cos = InitCosAPI();
|
||||
CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
|
||||
IsObjectExistDemo(cos);
|
||||
HeadObjectDemo(cos);
|
||||
}
|
||||
273
demo/object_op_demo/multi_get_object_demo.cpp
Normal file
273
demo/object_op_demo/multi_get_object_demo.cpp
Normal file
@@ -0,0 +1,273 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
/*
|
||||
* 该 Demo 示范如何使用多线程下载接口下载对象
|
||||
* 仅支持下载到文件,利用了分 range 多线程同时下载的方式下载对象
|
||||
* 可通过全局设置下载线程池大小、range 大小。该线程池是每次上传独立的
|
||||
*/
|
||||
void MultiGetObjectDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string file_path = "test_file/big_file.txt";
|
||||
std::string object_name = "big_file.txt";
|
||||
// 此配置是全局性配置,主动设置后,后续涉及多线程下载的接口,都使用此配置
|
||||
CosSysConfig::SetDownThreadPoolSize(10); // 下载线程池大小 默认10
|
||||
CosSysConfig::SetDownSliceSize(4 * 1024 * 1024); // 下载 range 大小 默认4M
|
||||
|
||||
qcloud_cos::MultiGetObjectReq req(bucket_name, object_name, file_path);
|
||||
qcloud_cos::MultiGetObjectResp resp;
|
||||
|
||||
qcloud_cos::CosResult result = cos.MultiGetObject(req, &resp);
|
||||
std::cout << "===================GetObjectResponse=====================" << std::endl;
|
||||
PrintResult(result, resp);
|
||||
std::cout << "=========================================================" << std::endl;
|
||||
}
|
||||
|
||||
/*
|
||||
* 该 Demo 示范如何使用断点下载接口下载对象
|
||||
* 仅支持下载到文件,利用了分 range 多线程同时下载的方式下载对象,支持断点下载
|
||||
* 可通过全局设置下载线程池大小、range 大小。该线程池是每次上传独立的。
|
||||
*/
|
||||
void ResumableGetObjectDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string file_path = "test_file/big_file.txt";
|
||||
std::string object_name = "big_file.txt";
|
||||
|
||||
qcloud_cos::GetObjectByFileReq req(bucket_name, object_name, file_path);
|
||||
qcloud_cos::GetObjectByFileResp resp;
|
||||
|
||||
CosResult result = cos.ResumableGetObject(req, &resp);
|
||||
std::cout << "===================ResumableGetObject====================" << std::endl;
|
||||
PrintResult(result, resp);
|
||||
std::cout << "=========================================================" << std::endl;
|
||||
}
|
||||
|
||||
/*
|
||||
* 该方法是异步下载对象的进度回调示例
|
||||
*/
|
||||
void ProgressCallback(uint64_t transferred_size, uint64_t total_size, void* user_data) {
|
||||
qcloud_cos::ObjectReq* req = static_cast<qcloud_cos::ObjectReq*>(user_data);
|
||||
if (0 == transferred_size % 1048576) {
|
||||
std::cout << "ObjectName:" << req->GetObjectName() << ", TranferedSize:" << transferred_size << ",TotalSize:" << total_size << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 该方法是异步下载对象的完成回调示例
|
||||
*/
|
||||
void GetObjectAsyncDoneCallback(const SharedAsyncContext& context, void* user_data) {
|
||||
UNUSED_PARAM(user_data)
|
||||
std::cout << "MultiGetObjectAsyncDoneCallback, BucketName:"
|
||||
<< context->GetBucketName()
|
||||
<< ", ObjectName:" << context->GetObjectName()
|
||||
<< ", LocalFile:" << context->GetLocalFilePath() << std::endl;
|
||||
// qcloud_cos::MultiGetObjectReq对应的响应为qcloud_cos::GetObjectByFileResp
|
||||
if (context->GetResult().IsSucc()) {
|
||||
// 获取响应
|
||||
std::cout << "AsyncMultiGetObject succeed" << std::endl;
|
||||
std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
|
||||
AsyncResp resp = context->GetAsyncResp();
|
||||
std::cout << "ETag:" << resp.GetEtag() << std::endl;
|
||||
std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
|
||||
} else {
|
||||
std::cout << "AsyncMultiGetObject failed" << std::endl;
|
||||
std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 该 Demo 示范如何通过异步多线程接口下载对象
|
||||
* 仅支持下载到文件,利用了分 range 多线程同时下载的方式下载对象
|
||||
* 可通过全局设置异步线程池大小、下载线程池大小、range 大小
|
||||
* 注意:异步线程池是全局共用的,供所有的异步调用使用。下载线程池、range 大小与所有多线程类型下载接口共用配置
|
||||
* 例如调用两次异步接口,则两个异步操作会使用异步线程池进行调度。而每个异步操作中,可能会调用多线程相关接口创建出供本次使用的下载线程池,从而并发下载对象。
|
||||
* 支持下载进度回调和下载状态回调
|
||||
*/
|
||||
void AsyncMultiGetObjectDemo(qcloud_cos::CosAPI& cos) {
|
||||
CosSysConfig::SetAsynThreadPoolSize(2); // 设置异步线程池大小 默认2
|
||||
|
||||
std::string local_file = "test_file/big_file.txt";
|
||||
std::string object_name = "big_file.txt";
|
||||
qcloud_cos::AsyncMultiGetObjectReq req(bucket_name, object_name, local_file);
|
||||
// 设置下载进度回调
|
||||
req.SetTransferProgressCallback(&ProgressCallback);
|
||||
// 设置下载状态回调
|
||||
req.SetDoneCallback(&GetObjectAsyncDoneCallback);
|
||||
// 设置私有数据,对应回调中的 user_data
|
||||
req.SetUserData(&req);
|
||||
|
||||
// 开始下载
|
||||
SharedAsyncContext context = cos.AsyncMultiGetObject(req);
|
||||
|
||||
std::cout << "===================AsyncMultiGetObject======================" << std::endl;
|
||||
// 等待下载结束
|
||||
std::cout << "wait finish..." << std::endl;
|
||||
context->WaitUntilFinish();
|
||||
// context->WaitUntilFinish()阻塞完毕的逻辑是:异步线程在下载时文件落盘完毕。
|
||||
// 此时您的主线程如果立即结束,异步线程可能正在进行资源释放、结束线程等过程。此时可能会产生crash。
|
||||
// 此问题并不会对文件的下载造成影响,仅仅是体验上可能会出现意料之外的崩溃信息。
|
||||
|
||||
// 检查结果
|
||||
if (context->GetResult().IsSucc()) {
|
||||
// 获取响应
|
||||
std::cout << "AsyncMultiGetObject succeed" << std::endl;
|
||||
std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
|
||||
AsyncResp resp = context->GetAsyncResp();
|
||||
std::cout << "ETag:" << resp.GetEtag() << std::endl;
|
||||
std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
|
||||
} else {
|
||||
std::cout << "AsyncMultiGetObject failed" << std::endl;
|
||||
std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;
|
||||
}
|
||||
std::cout << "============================================================" << std::endl;
|
||||
}
|
||||
|
||||
/*
|
||||
* 该 Demo 示范如何通过异步断点下载接口下载对象
|
||||
* 仅支持下载到文件,利用了分 range 多线程同时下载的方式下载对象,支持断点下载
|
||||
* 可通过全局设置异步线程池大小、下载线程池大小、range 大小。
|
||||
* 注意:异步线程池是全局共用的,供所有的异步调用使用。下载线程池、range 大小与所有多线程类型下载接口共用配置
|
||||
* 例如调用两次异步接口,则两个异步操作会使用异步线程池进行调度。而每个异步操作中,可能会调用多线程相关接口创建出供本次使用的下载线程池,从而并发下载对象。
|
||||
* 支持下载进度回调和下载状态回调
|
||||
*/
|
||||
void AsyncResumableGetObjectDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string local_file = "test_file/big_file.txt";
|
||||
std::string object_name = "big_file.txt";
|
||||
qcloud_cos::AsyncGetObjectReq req(bucket_name, object_name, local_file);
|
||||
// 设置下载进度回调
|
||||
req.SetTransferProgressCallback(&ProgressCallback);
|
||||
// 设置下载状态回调
|
||||
req.SetDoneCallback(&GetObjectAsyncDoneCallback);
|
||||
// 设置私有数据,对应回调中的 user_data
|
||||
req.SetUserData(&req);
|
||||
|
||||
// 开始下载
|
||||
SharedAsyncContext context = cos.AsyncResumableGetObject(req);
|
||||
|
||||
std::cout << "===================AsyncResumableGetObject======================" << std::endl;
|
||||
// 等待下载结束
|
||||
std::cout << "wait finish..." << std::endl;
|
||||
context->WaitUntilFinish();
|
||||
// context->WaitUntilFinish()阻塞完毕的逻辑是:异步线程在下载时文件落盘完毕。
|
||||
// 此时您的主线程如果立即结束,异步线程可能正在进行资源释放、结束线程等过程。此时可能会产生crash。
|
||||
// 此问题并不会对文件的下载造成影响,仅仅是体验上可能会出现意料之外的崩溃信息。
|
||||
|
||||
// 检查结果
|
||||
if (context->GetResult().IsSucc()) {
|
||||
// 获取响应
|
||||
std::cout << "AsyncResumableGetObject succeed" << std::endl;
|
||||
std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
|
||||
AsyncResp resp = context->GetAsyncResp();
|
||||
std::cout << "ETag:" << resp.GetEtag() << std::endl;
|
||||
std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
|
||||
} else {
|
||||
std::cout << "AsyncResumableGetObject failed" << std::endl;
|
||||
std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;
|
||||
}
|
||||
std::cout << "================================================================" << std::endl;
|
||||
}
|
||||
|
||||
/*
|
||||
* 该 Demo 示范如何通过异步简单接口下载对象
|
||||
* 仅支持下载到文件
|
||||
* 可通过全局设置异步线程池大小
|
||||
* 注意:异步线程池是全局共用的,供异步调用使用
|
||||
* 例如调用两次异步接口,则两个异步操作会使用异步线程池进行调度
|
||||
* 支持下载进度回调和下载状态回调
|
||||
*/
|
||||
void AsyncGetObjectDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string local_file = "test_file/text.txt";
|
||||
std::string object_name = "text.txt";
|
||||
qcloud_cos::AsyncGetObjectReq req(bucket_name, object_name, local_file);
|
||||
req.SetRecvTimeoutInms(1000 * 60);
|
||||
// 设置下载进度回调
|
||||
req.SetTransferProgressCallback(&ProgressCallback);
|
||||
// 设置下载状态回调
|
||||
req.SetDoneCallback(&GetObjectAsyncDoneCallback);
|
||||
// 设置私有数据,对应回调中的 user_data
|
||||
req.SetUserData(&req);
|
||||
|
||||
// 开始下载
|
||||
SharedAsyncContext context = cos.AsyncGetObject(req);
|
||||
std::cout << "===================AsyncGetObject======================" << std::endl;
|
||||
|
||||
// 等待下载结束
|
||||
std::cout << "wait finish..." << std::endl;
|
||||
context->WaitUntilFinish();
|
||||
// context->WaitUntilFinish()阻塞完毕的逻辑是:异步线程在下载时文件落盘完毕。
|
||||
// 此时您的主线程如果立即结束,异步线程可能正在进行资源释放、结束线程等过程。此时可能会产生crash。
|
||||
// 此问题并不会对文件的下载造成影响,仅仅是体验上可能会出现意料之外的崩溃信息。
|
||||
|
||||
// 检查结果
|
||||
if (context->GetResult().IsSucc()) {
|
||||
// 获取响应
|
||||
std::cout << "AsyncGetObject succeed" << std::endl;
|
||||
std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
|
||||
AsyncResp resp = context->GetAsyncResp();
|
||||
std::cout << "ETag:" << resp.GetEtag() << std::endl;
|
||||
std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
|
||||
} else {
|
||||
std::cout << "AsyncGetObject failed" << std::endl;
|
||||
std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;
|
||||
}
|
||||
std::cout << "=======================================================" << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
qcloud_cos::CosAPI cos = InitCosAPI();
|
||||
CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
|
||||
MultiGetObjectDemo(cos);
|
||||
ResumableGetObjectDemo(cos);
|
||||
AsyncMultiGetObjectDemo(cos);
|
||||
AsyncResumableGetObjectDemo(cos);
|
||||
AsyncGetObjectDemo(cos);
|
||||
}
|
||||
439
demo/object_op_demo/multi_put_object_demo.cpp
Normal file
439
demo/object_op_demo/multi_put_object_demo.cpp
Normal file
@@ -0,0 +1,439 @@
|
||||
#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"
|
||||
// #include <openssl/ssl.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 对象,请注释
|
||||
// config.SetDestDomain("xxx.xxxx.com"); // 配置自定义域名
|
||||
// config.SetDomainSameToHost(true); // 配置自定义域名签名
|
||||
qcloud_cos::CosAPI cos_tmp(config);
|
||||
return cos_tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* 本方法为 SSL_CTX 的回调方法,用户可以在此方法中配置 SSL_CTX 信息
|
||||
*/
|
||||
// int SslCtxCallback(void *ssl_ctx, void *data) {
|
||||
// std::cout << "ssl_ctx: " << ssl_ctx << " data: " << data << std::endl;
|
||||
|
||||
// SSL_CTX *ctx = (SSL_CTX *)ssl_ctx;
|
||||
// std::cout << "ssl_ctx in" << std::endl;
|
||||
// SSL_CTX_use_PrivateKey_file(ctx, "/data/cert/client.key", SSL_FILETYPE_PEM);
|
||||
// SSL_CTX_use_certificate_chain_file(ctx, "/data/cert/client.crt");
|
||||
// std::cout << "ssl_ctx out" << std::endl;
|
||||
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
/*
|
||||
* 该 Demo 示范如何使用高级上传接口进行对象上传
|
||||
* 仅支持文件上传,不支持流式上传,封装了分块上传的各接口
|
||||
* 可通过全局设置上传线程池大小、分块大小。该上传线程池是每次上传独立的。
|
||||
*/
|
||||
void MultiUploadObjectDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string local_file = "test_file/big_file.txt";
|
||||
std::string object_name = "big_file.txt";
|
||||
// 此配置是全局性配置,主动设置后,后续的高级上传,都使用此配置
|
||||
CosSysConfig::SetUploadThreadPoolSize(5); // 上传线程池大小 默认5
|
||||
CosSysConfig::SetUploadPartSize(10 * 1024 * 1024); // 上传分块大小 默认10M
|
||||
|
||||
qcloud_cos::MultiPutObjectReq req(bucket_name, object_name, local_file);
|
||||
// req.SetHttps(); // 设置 https 请求
|
||||
// req.SetSSLCtxCallback(SslCtxCallback, nullptr); //双向认证回调
|
||||
qcloud_cos::MultiPutObjectResp resp;
|
||||
qcloud_cos::CosResult result = cos.MultiPutObject(req, &resp);
|
||||
|
||||
std::cout << "===================MultiUploadObject=======================" << std::endl;
|
||||
if (result.IsSucc()) {
|
||||
std::cout << "MultiUpload Succ." << std::endl;
|
||||
std::cout << resp.GetLocation() << std::endl;
|
||||
std::cout << resp.GetKey() << std::endl;
|
||||
std::cout << resp.GetBucket() << std::endl;
|
||||
std::cout << resp.GetEtag() << std::endl;
|
||||
} else {
|
||||
std::cout << "MultiUpload Fail." << std::endl;
|
||||
// 获取具体失败在哪一步
|
||||
std::string resp_tag = resp.GetRespTag();
|
||||
if ("Init" == resp_tag) {
|
||||
// print result
|
||||
} else if ("Upload" == resp_tag) {
|
||||
// print result
|
||||
} else if ("Complete" == resp_tag) {
|
||||
// print result
|
||||
}
|
||||
PrintResult(result, resp);
|
||||
}
|
||||
std::cout << "===========================================================" << std::endl;
|
||||
}
|
||||
|
||||
/*
|
||||
* 该方法是异步上传对象的进度回调示例
|
||||
*/
|
||||
void ProgressCallback(uint64_t transferred_size, uint64_t total_size, void* user_data) {
|
||||
qcloud_cos::ObjectReq* req = static_cast<qcloud_cos::ObjectReq*>(user_data);
|
||||
if (0 == transferred_size % 1048576) {
|
||||
std::cout << "ObjectName:" << req->GetObjectName() << ", TranferedSize:" << transferred_size << ",TotalSize:" << total_size << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 该方法是异步上传对象的完成回调示例
|
||||
*/
|
||||
void PutObjectAsyncDoneCallback(const SharedAsyncContext& context, void* user_data) {
|
||||
UNUSED_PARAM(user_data)
|
||||
std::cout << "PutObjectAsyncDoneCallback, BucketName:" << context->GetBucketName() << ", ObjectName:" << context->GetObjectName() << ", LocalFile:" << context->GetLocalFilePath() << std::endl;
|
||||
if (context->GetResult().IsSucc()) {
|
||||
// 获取响应
|
||||
std::cout << "AsyncMultiPutObject succeed" << std::endl;
|
||||
std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
|
||||
AsyncResp resp = context->GetAsyncResp();
|
||||
// std::cout << "Location:" << resp.GetLocation() << std::endl;
|
||||
// std::cout << "Bucket:" << resp.GetBucket() << std::endl;
|
||||
// std::cout << "Key:" << resp.GetKey() << std::endl;
|
||||
std::cout << "ETag:" << resp.GetEtag() << std::endl;
|
||||
std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
|
||||
} else {
|
||||
std::cout << "AsyncMultiGetObject failed" << std::endl;
|
||||
std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 该 Demo 示范如何通过异步高级上传对象进行对象上传
|
||||
* 仅支持文件上传,不支持流式上传,封装了分块上传的各接口、
|
||||
* 可通过全局设置异步线程池大小、上传线程池大小、分块大小
|
||||
* 注意:异步线程池是全局共用的,供异步调用使用。上传线程池、分块大小是和高级接口共用配置
|
||||
* 例如调用两次异步接口,则两个异步操作会使用异步线程池进行调度。而每个异步操作会调用高级接口创建出供本次使用的上传线程池,从而并发上传对象。
|
||||
* 支持上传进度回调和上传状态回调
|
||||
*/
|
||||
void AsyncMultiPutObjectDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string local_file = "test_file/big_file.txt";
|
||||
std::string object_name = "big_file.txt";
|
||||
qcloud_cos::AsyncMultiPutObjectReq req(bucket_name, object_name, local_file);
|
||||
req.SetRecvTimeoutInms(1000 * 60);
|
||||
// 设置上传进度回调
|
||||
req.SetTransferProgressCallback(&ProgressCallback);
|
||||
// 设置上传状态回调
|
||||
req.SetDoneCallback(&PutObjectAsyncDoneCallback);
|
||||
// 设置私有数据,对应回调中的 user_data
|
||||
req.SetUserData(&req);
|
||||
|
||||
// 开始上传
|
||||
SharedAsyncContext context = cos.AsyncMultiPutObject(req);
|
||||
|
||||
std::cout << "===================AsyncMultiPutObject======================" << std::endl;
|
||||
// 等待上传结束
|
||||
std::cout << "wait finish..." << std::endl;
|
||||
context->WaitUntilFinish();
|
||||
|
||||
// 检查结果
|
||||
if (context->GetResult().IsSucc()) {
|
||||
// 获取响应
|
||||
std::cout << "AsyncMultiPutObject succeed" << std::endl;
|
||||
std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
|
||||
AsyncResp resp = context->GetAsyncResp();
|
||||
// std::cout << "Location:" << resp.GetLocation() << std::endl;
|
||||
// std::cout << "Bucket:" << resp.GetBucket() << std::endl;
|
||||
// std::cout << "Key:" << resp.GetKey() << std::endl;
|
||||
std::cout << "ETag:" << resp.GetEtag() << std::endl;
|
||||
std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
|
||||
} else {
|
||||
std::cout << "AsyncMultiPutObject failed" << std::endl;
|
||||
std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;
|
||||
}
|
||||
std::cout << "============================================================" << std::endl;
|
||||
}
|
||||
|
||||
/*
|
||||
* 该 Demo 示范如何通过异步普通接口上传对象
|
||||
* 包括流和文件两种方式上传
|
||||
* 可通过全局设置异步线程池大小
|
||||
* 注意:异步线程池是全局共用的,供异步调用使用
|
||||
* 例如调用两次异步接口,则两个异步操作会使用异步线程池进行调度
|
||||
* 支持上传进度回调和上传状态回调
|
||||
*/
|
||||
void AsyncPutObjectDemo(qcloud_cos::CosAPI& cos) {
|
||||
// 流上传
|
||||
{
|
||||
std::string object_name = "text.txt";
|
||||
std::istringstream iss("put object");
|
||||
qcloud_cos::AsyncPutObjectByStreamReq req(bucket_name, object_name, iss);
|
||||
req.SetRecvTimeoutInms(1000 * 60);
|
||||
// 设置上传进度回调
|
||||
req.SetTransferProgressCallback(&ProgressCallback);
|
||||
// 设置上传状态回调
|
||||
req.SetDoneCallback(&PutObjectAsyncDoneCallback);
|
||||
// 设置私有数据,对应回调中的 user_data
|
||||
req.SetUserData(&req);
|
||||
|
||||
// 开始上传
|
||||
SharedAsyncContext context = cos.AsyncPutObject(req);
|
||||
|
||||
std::cout << "===================AsyncPutObjectByStream======================" << std::endl;
|
||||
// 等待上传结束
|
||||
std::cout << "wait finish..." << std::endl;
|
||||
context->WaitUntilFinish();
|
||||
|
||||
// 检查结果
|
||||
if (context->GetResult().IsSucc()) {
|
||||
// 获取响应
|
||||
std::cout << "AsyncPutObjectByStream succeed" << std::endl;
|
||||
std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
|
||||
AsyncResp resp = context->GetAsyncResp();
|
||||
// std::cout << "Location:" << resp.GetLocation() << std::endl;
|
||||
// std::cout << "Bucket:" << resp.GetBucket() << std::endl;
|
||||
// std::cout << "Key:" << resp.GetKey() << std::endl;
|
||||
std::cout << "ETag:" << resp.GetEtag() << std::endl;
|
||||
std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
|
||||
} else {
|
||||
std::cout << "AsyncPutObjectByStream failed" << std::endl;
|
||||
std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;
|
||||
}
|
||||
std::cout << "===============================================================" << std::endl;
|
||||
}
|
||||
// 文件上传
|
||||
{
|
||||
std::string local_file = "test_file/text.txt";
|
||||
std::string object_name = "text.txt";
|
||||
qcloud_cos::AsyncPutObjectReq req(bucket_name, object_name, local_file);
|
||||
req.SetRecvTimeoutInms(1000 * 60);
|
||||
// 设置上传进度回调
|
||||
req.SetTransferProgressCallback(&ProgressCallback);
|
||||
// 设置上传状态回调
|
||||
req.SetDoneCallback(&PutObjectAsyncDoneCallback);
|
||||
// 设置私有数据,对应回调中的 user_data
|
||||
req.SetUserData(&req);
|
||||
|
||||
// 开始上传
|
||||
SharedAsyncContext context = cos.AsyncPutObject(req);
|
||||
std::cout << "===================AsyncPutObjectByFile======================" << std::endl;
|
||||
|
||||
// 等待上传结束
|
||||
std::cout << "wait finish..." << std::endl;
|
||||
context->WaitUntilFinish();
|
||||
|
||||
// 检查结果
|
||||
if (context->GetResult().IsSucc()) {
|
||||
// 获取响应
|
||||
std::cout << "AsyncPutObjectByFile succeed" << std::endl;
|
||||
std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
|
||||
AsyncResp resp = context->GetAsyncResp();
|
||||
// std::cout << "Location:" << resp.GetLocation() << std::endl;
|
||||
// std::cout << "Bucket:" << resp.GetBucket() << std::endl;
|
||||
// std::cout << "Key:" << resp.GetKey() << std::endl;
|
||||
std::cout << "ETag:" << resp.GetEtag() << std::endl;
|
||||
std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
|
||||
} else {
|
||||
std::cout << "AsyncPutObjectByFile failed" << std::endl;
|
||||
std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;
|
||||
}
|
||||
std::cout << "=============================================================" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 该 Demo 示范用户如何自行组合分块上传各接口进行对象上传
|
||||
* 分块操作基于初始化、上传分块、完成分块三个接口可以实现将对象切分为多个分块,然后再将这些分块上传到 cos,最后发起 Complete 完成分块上传
|
||||
* 本 Demo 中的上传分块接口 UploadPartData 仅支持传入流,最多支持10000分块,每个分块大小为1MB - 5GB,最后一个分块可以小于1MB
|
||||
*/
|
||||
|
||||
void PutPartDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string object_name = "big_file.txt";
|
||||
|
||||
// 1. InitMultiUpload
|
||||
qcloud_cos::InitMultiUploadReq init_req(bucket_name, object_name);
|
||||
qcloud_cos::InitMultiUploadResp init_resp;
|
||||
qcloud_cos::CosResult init_result = cos.InitMultiUpload(init_req, &init_resp);
|
||||
|
||||
std::cout << "=====================InitMultiUpload=====================" << std::endl;
|
||||
PrintResult(init_result, init_resp);
|
||||
std::cout << "=========================================================" << std::endl;
|
||||
|
||||
// 2. UploadPartData
|
||||
// UploadPartData 部分,可以根据实际选择分块数量和分块大小,这里以 2 个分块为例
|
||||
|
||||
// Complete 需要的两个列表:
|
||||
std::vector<std::string> etags;
|
||||
std::vector<uint64_t> part_numbers;
|
||||
|
||||
std::string upload_id = init_resp.GetUploadId();
|
||||
{
|
||||
uint64_t part_number = 1;
|
||||
// 模拟上传分块数据,这里以 100M 为例
|
||||
uint64_t copy_size = 1024 * 1024 * 100;
|
||||
std::vector<char> data(copy_size, 'A');
|
||||
std::string content(data.begin(), data.end());
|
||||
std::istringstream iss(content);
|
||||
qcloud_cos::UploadPartDataReq req(bucket_name, object_name, upload_id, iss);
|
||||
req.SetPartNumber(part_number);
|
||||
// 限速上传对象,默认单位为 bit/s,限速值设置范围为 819200 - 838860800, 即800Kb/s-800Mb/s
|
||||
uint64_t traffic_limit = 8192*1024*10; // 100MB 文件 5M
|
||||
req.SetTrafficLimit(traffic_limit);
|
||||
qcloud_cos::UploadPartDataResp resp;
|
||||
std::chrono::time_point<std::chrono::steady_clock> start_ts, end_ts;
|
||||
start_ts = std::chrono::steady_clock::now();
|
||||
qcloud_cos::CosResult result = cos.UploadPartData(req, &resp);
|
||||
end_ts = std::chrono::steady_clock::now();
|
||||
auto time_consumed_ms =
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(end_ts - start_ts)
|
||||
.count();
|
||||
float rate =
|
||||
((float)copy_size / 1024 / 1024) / ((float)time_consumed_ms / 1000);
|
||||
SDK_LOG_ERR("send part_number: %d, send_size: %" PRIu64 " MB, time_consumed: %" PRIu64
|
||||
" ms, rate: %.2f MB/s , traffic_limit : %.2f MB", part_number, copy_size/ 1024 / 1024, time_consumed_ms, rate, traffic_limit/1024/1024/8.0);
|
||||
std::cout << "==================UploadPartDataResp1=====================" << std::endl;
|
||||
PrintResult(result, resp);
|
||||
std::cout << "==========================================================" << std::endl;
|
||||
if (result.IsSucc()) {
|
||||
part_numbers.push_back(part_number);
|
||||
etags.push_back(resp.GetEtag());
|
||||
}
|
||||
}
|
||||
{
|
||||
uint64_t part_number = 2;
|
||||
uint64_t copy_size = 1024 * 1024 * 100;
|
||||
std::vector<char> data(copy_size, 'A');
|
||||
std::string content(data.begin(), data.end());
|
||||
std::istringstream iss(content);
|
||||
qcloud_cos::UploadPartDataReq req(bucket_name, object_name, upload_id, iss);
|
||||
req.SetPartNumber(part_number);
|
||||
// 限速上传对象,默认单位为 bit/s,限速值设置范围为 819200 - 838860800, 即800Kb/s-800Mb/s
|
||||
uint64_t traffic_limit = 8192 * 1024 * 5 ;
|
||||
req.SetTrafficLimit(traffic_limit);
|
||||
qcloud_cos::UploadPartDataResp resp;
|
||||
qcloud_cos::CosResult result = cos.UploadPartData(req, &resp);
|
||||
std::cout << "==================UploadPartDataResp2=====================" << std::endl;
|
||||
PrintResult(result, resp);
|
||||
std::cout << "==========================================================" << std::endl;
|
||||
if (result.IsSucc()) {
|
||||
part_numbers.push_back(part_number);
|
||||
etags.push_back(resp.GetEtag());
|
||||
}
|
||||
}
|
||||
{
|
||||
uint64_t part_number = 3;
|
||||
uint64_t copy_size = 1024 * 1024 * 10;
|
||||
std::vector<char> data(copy_size, 'A');
|
||||
std::string content(data.begin(), data.end());
|
||||
std::istringstream iss(content);
|
||||
qcloud_cos::UploadPartDataReq req(bucket_name, object_name, upload_id, iss);
|
||||
req.SetPartNumber(part_number);
|
||||
// 限速上传对象,默认单位为 bit/s,限速值设置范围为 819200 - 838860800, 即800Kb/s-800Mb/s
|
||||
uint64_t traffic_limit = 8192 * 1024;
|
||||
req.SetTrafficLimit(traffic_limit);
|
||||
qcloud_cos::UploadPartDataResp resp;
|
||||
qcloud_cos::CosResult result = cos.UploadPartData(req, &resp);
|
||||
std::cout << "==================UploadPartDataResp2=====================" << std::endl;
|
||||
PrintResult(result, resp);
|
||||
std::cout << "==========================================================" << std::endl;
|
||||
if (result.IsSucc()) {
|
||||
part_numbers.push_back(part_number);
|
||||
etags.push_back(resp.GetEtag());
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Complete
|
||||
CompleteMultiUploadReq comp_req(bucket_name, object_name, upload_id);
|
||||
CompleteMultiUploadResp comp_resp;
|
||||
|
||||
comp_req.SetEtags(etags);
|
||||
comp_req.SetPartNumbers(part_numbers);
|
||||
|
||||
qcloud_cos::CosResult result = cos.CompleteMultiUpload(comp_req, &comp_resp);
|
||||
|
||||
std::cout << "===================Complete=============================" << std::endl;
|
||||
PrintResult(result, comp_resp);
|
||||
std::cout << "========================================================" << std::endl;
|
||||
|
||||
return;
|
||||
}
|
||||
void PutObjectResumableSingleThreadSyncDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string local_file = "SingleThreadSync.txt";
|
||||
std::string object_name = "SingleThreadSync.txt";
|
||||
|
||||
qcloud_cos::PutObjectResumableSingleSyncReq req(bucket_name, object_name, local_file);
|
||||
req.AddHeader("x-cos-meta-ssss1","1xxxxxxx");
|
||||
req.AddHeader("x-cos-meta-ssss2","2xxxxxxx");
|
||||
req.AddHeader("x-cos-meta-ssss3","3xxxxxxx");
|
||||
req.AddHeader("x-cos-meta-ssss4","4xxxxxxx");
|
||||
uint64_t traffic_limit = 8192 * 1024;//1MB
|
||||
req.SetTrafficLimit(traffic_limit);
|
||||
//req.SetHttps();
|
||||
//req.SetSSLCtxCallback(SslCtxCallback, nullptr);
|
||||
qcloud_cos::PutObjectResumableSingleSyncResp resp;
|
||||
qcloud_cos::CosResult result = cos.PutObjectResumableSingleThreadSync(req, &resp);
|
||||
if (result.IsSucc()) {
|
||||
std::cout << "MultiUpload Succ." << std::endl;
|
||||
std::cout << resp.GetLocation() << std::endl;
|
||||
std::cout << resp.GetKey() << std::endl;
|
||||
std::cout << resp.GetBucket() << std::endl;
|
||||
std::cout << resp.GetEtag() << std::endl;
|
||||
} else {
|
||||
std::cout << "MultiUpload Fail." << std::endl;
|
||||
// 获取具体失败在哪一步
|
||||
std::string resp_tag = resp.GetRespTag();
|
||||
if ("Init" == resp_tag) {
|
||||
// print result
|
||||
} else if ("Upload" == resp_tag) {
|
||||
// print result
|
||||
} else if ("Complete" == resp_tag) {
|
||||
// print result
|
||||
}
|
||||
PrintResult(result, resp);
|
||||
}
|
||||
std::cout << "===========================================================" << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
qcloud_cos::CosAPI cos = InitCosAPI();
|
||||
CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
|
||||
MultiUploadObjectDemo(cos);
|
||||
AsyncMultiPutObjectDemo(cos);
|
||||
AsyncPutObjectDemo(cos);
|
||||
PutPartDemo(cos);
|
||||
PutObjectResumableSingleThreadSyncDemo(cos);
|
||||
}
|
||||
115
demo/object_op_demo/put_get_delete_object_tagging_demo.cpp
Normal file
115
demo/object_op_demo/put_get_delete_object_tagging_demo.cpp
Normal file
@@ -0,0 +1,115 @@
|
||||
#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";
|
||||
std::string uin = "23300000000";
|
||||
|
||||
/*
|
||||
* 本方法包含调用是否正常的判断,和请求结果的输出
|
||||
* 可通过本方法判断是否请求成功,并输出结果信息
|
||||
*/
|
||||
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 PutObjectTaggingDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string object_name = "test.txt";
|
||||
qcloud_cos::PutObjectTaggingReq req(bucket_name, object_name);
|
||||
qcloud_cos::PutObjectTaggingResp resp;
|
||||
std::vector<Tag> tagset;
|
||||
Tag tag1;
|
||||
tag1.SetKey("age");
|
||||
tag1.SetValue("19");
|
||||
|
||||
Tag tag2;
|
||||
tag2.SetKey("name");
|
||||
tag2.SetValue("xiaoming");
|
||||
|
||||
Tag tag3;
|
||||
tag3.SetKey("sex");
|
||||
tag3.SetValue("male");
|
||||
|
||||
tagset.push_back(tag1);
|
||||
tagset.push_back(tag2);
|
||||
tagset.push_back(tag3);
|
||||
req.SetTagSet(tagset);
|
||||
|
||||
qcloud_cos::CosResult result = cos.PutObjectTagging(req, &resp);
|
||||
std::cout << "=========================PutBucketTagging===========================" << std::endl;
|
||||
PrintResult(result, resp);
|
||||
std::cout << "====================================================================" << std::endl;
|
||||
}
|
||||
|
||||
void GetObjectTaggingDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string object_name = "test.txt";
|
||||
qcloud_cos::GetObjectTaggingReq req(bucket_name, object_name);
|
||||
qcloud_cos::GetObjectTaggingResp resp;
|
||||
|
||||
qcloud_cos::CosResult result = cos.GetObjectTagging(req, &resp);
|
||||
std::cout << "=========================GetObjectTagging===========================" << std::endl;
|
||||
std::vector<Tag> tagset = resp.GetTagSet();
|
||||
for (std::vector<Tag>::iterator it = tagset.begin(); it != tagset.end(); ++it) {
|
||||
std::cout << it->GetKey() << ":" << it->GetValue() << std::endl;
|
||||
}
|
||||
PrintResult(result, resp);
|
||||
std::cout << "====================================================================" << std::endl;
|
||||
}
|
||||
|
||||
void DeleteObjectTaggingDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string object_name = "test.txt";
|
||||
qcloud_cos::DeleteObjectTaggingReq req(bucket_name, object_name);
|
||||
qcloud_cos::DeleteObjectTaggingResp resp;
|
||||
|
||||
qcloud_cos::CosResult result = cos.DeleteObjectTagging(req, &resp);
|
||||
std::cout << "=======================DeleteObjectTagging==========================" << std::endl;
|
||||
PrintResult(result, resp);
|
||||
std::cout << "====================================================================" << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
qcloud_cos::CosAPI cos = InitCosAPI();
|
||||
CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
|
||||
PutObjectTaggingDemo(cos);
|
||||
GetObjectTaggingDemo(cos);
|
||||
DeleteObjectTaggingDemo(cos);
|
||||
}
|
||||
115
demo/object_op_demo/put_get_object_acl_demo.cpp
Normal file
115
demo/object_op_demo/put_get_object_acl_demo.cpp
Normal file
@@ -0,0 +1,115 @@
|
||||
#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 进行对象 ACL 的设置和获取
|
||||
* 包括:通过 body 和 head 两种方式设置对象的 ACL、获取对象的 ACL
|
||||
*/
|
||||
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";
|
||||
std::string uin = "23300000000";
|
||||
std::string sub_uin = "23300000000";
|
||||
|
||||
/*
|
||||
* 本方法包含调用是否正常的判断,和请求结果的输出
|
||||
* 可通过本方法判断是否请求成功,并输出结果信息
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/*
|
||||
* 该 Demo 示范如何上传对象的 ACL
|
||||
* 本样例先通过 body 的方式为主账号持有的对象设置子账号 FULL_CONTROL 访问权限。再通过 head 的方式设置子账号 READ 访问权限
|
||||
*/
|
||||
void PutObjectACLDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string object_name = "test.txt";
|
||||
// 1 通过Body设置ACL配置 (设置ACL可以通过Body、Header两种方式,但只能二选一,否则会有冲突)
|
||||
// ACL 请求构造参考 api 文档:https://cloud.tencent.com/document/product/436/7748
|
||||
{
|
||||
qcloud_cos::PutObjectACLReq req(bucket_name, object_name);
|
||||
qcloud_cos::Owner owner = {"qcs::cam::uin/" + uin + ":uin/" + uin,
|
||||
"testacl"};
|
||||
qcloud_cos::Grant grant;
|
||||
req.SetOwner(owner);
|
||||
grant.m_grantee.m_type = "CanonicalUser";
|
||||
grant.m_grantee.m_id = "qcs::cam::uin/" + uin + ":uin/" + sub_uin;
|
||||
grant.m_perm = "FULL_CONTROL";
|
||||
req.AddAccessControlList(grant);// 可以加入多条grant
|
||||
|
||||
qcloud_cos::PutObjectACLResp resp;
|
||||
qcloud_cos::CosResult result = cos.PutObjectACL(req, &resp);
|
||||
std::cout << "===================PutObjectACLResponse======================" << std::endl;
|
||||
PrintResult(result, resp);
|
||||
std::cout << "=============================================================" << std::endl;
|
||||
}
|
||||
|
||||
// 2 通过Header设置ACL配置
|
||||
{
|
||||
qcloud_cos::PutObjectACLReq req(bucket_name, object_name);
|
||||
req.SetXCosAcl("default"); // 对应 api 中的 x-cos-acl
|
||||
std::string grant_read = "id=\"" + uin + "/"+ sub_uin + "\"";
|
||||
req.SetXCosGrantRead(grant_read); // 对应 api 中的 x-cos-grant-read
|
||||
// std::string grant_full_control = "id=\"" + uin + "/"+ sub_uin + "/\"";
|
||||
// req.SetXCosGrantFullControl(grant_full_control); // 对应 api 中的 x-cos-grant-full-control
|
||||
|
||||
qcloud_cos::PutObjectACLResp resp;
|
||||
qcloud_cos::CosResult result = cos.PutObjectACL(req, &resp);
|
||||
std::cout << "===================PutObjectACLResponse======================" << std::endl;
|
||||
PrintResult(result, resp);
|
||||
std::cout << "=============================================================" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void GetObjectACLDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string object_name = "test.txt";
|
||||
qcloud_cos::GetObjectACLReq req(bucket_name, object_name);
|
||||
qcloud_cos::GetObjectACLResp resp;
|
||||
qcloud_cos::CosResult result = cos.GetObjectACL(req, &resp);
|
||||
|
||||
std::cout << "======================GetObjectACLResponse========================" << std::endl;
|
||||
PrintResult(result, resp);
|
||||
std::cout << "==================================================================" << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
qcloud_cos::CosAPI cos = InitCosAPI();
|
||||
CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
|
||||
PutObjectACLDemo(cos);
|
||||
GetObjectACLDemo(cos);
|
||||
}
|
||||
129
demo/object_op_demo/put_object_demo.cpp
Normal file
129
demo/object_op_demo/put_object_demo.cpp
Normal file
@@ -0,0 +1,129 @@
|
||||
#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 PutObjectByFileDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string object_name = "test.txt";
|
||||
std::string file_path = "./test_file/text.txt";
|
||||
qcloud_cos::PutObjectByFileReq req(bucket_name, object_name, file_path);
|
||||
// 限速上传对象,默认单位为 bit/s,限速值设置范围为 819200 - 838860800
|
||||
// uint64_t traffic_limit = 0;
|
||||
// req.SetTrafficLimit(traffic_limit);
|
||||
qcloud_cos::PutObjectByFileResp resp;
|
||||
qcloud_cos::CosResult result = cos.PutObject(req, &resp);
|
||||
|
||||
std::cout << "====================PutObjectByFile======================" << std::endl;
|
||||
PrintResult(result, resp);
|
||||
std::cout << "=========================================================" << std::endl;
|
||||
}
|
||||
|
||||
void PutObjectByStreamDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::istringstream iss("put object");
|
||||
std::string object_name = "text.txt";
|
||||
qcloud_cos::PutObjectByStreamReq req(bucket_name, object_name, iss);
|
||||
// 限速上传对象,默认单位为 bit/s,限速值设置范围为 819200 - 838860800
|
||||
// uint64_t traffic_limit = 0;
|
||||
// req.SetTrafficLimit(traffic_limit);
|
||||
qcloud_cos::PutObjectByStreamResp resp;
|
||||
qcloud_cos::CosResult result = cos.PutObject(req, &resp);
|
||||
|
||||
std::cout << "===================PutObjectByStream=====================" << std::endl;
|
||||
PrintResult(result, resp);
|
||||
std::cout << "=========================================================" << std::endl;
|
||||
}
|
||||
|
||||
void PutDirectoryDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string directory_name = "test_dir/"; // 目录名称,注意末尾需要有/
|
||||
PutDirectoryReq req(bucket_name, directory_name);
|
||||
PutDirectoryResp resp;
|
||||
CosResult result = cos.PutDirectory(req, &resp);
|
||||
|
||||
std::cout << "=====================PutDirectory========================" << std::endl;
|
||||
PrintResult(result, resp);
|
||||
std::cout << "=========================================================" << std::endl;
|
||||
}
|
||||
|
||||
/*
|
||||
* 该 Demo 示范如何上传本地文件夹到 cos
|
||||
* 本示例会将本地 test_file 目录上传到 cos 的 test_dir下。
|
||||
* 例如,本地当前路径的 test_file 目录下有 text1.txt 和 text2.txt 文件,上传后 cos 的路径为 test_dir/text1.txt 和 test_dir/text2.txt
|
||||
*/
|
||||
void PutObjectsFromDirectoryDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string directory_name = "test_file"; // 目录名称,注意开头如果加./ 则上传后cos 的路径为 test_dir/test_file/text.txt
|
||||
std::string cos_path = "test_dir/"; // 目录名称,注意末尾需要有/
|
||||
PutObjectsByDirectoryReq req(bucket_name, directory_name);
|
||||
req.SetCosPath(cos_path);
|
||||
PutObjectsByDirectoryResp resp;
|
||||
CosResult result = cos.PutObjects(req, &resp);
|
||||
|
||||
std::cout << "=====================PutDirectory========================" << std::endl;
|
||||
if (result.IsSucc()) {
|
||||
std::cout << "PutObjectsFromDirectory Succ." << std::endl;
|
||||
for (auto& r : resp.m_succ_put_objs) {
|
||||
std::cout << "file_name: " << r.m_file_name << "\nobject_name: " << r.m_object_name << "\nETag: " << r.m_cos_resp.GetEtag() << std::endl;
|
||||
std::cout << "====================================" << std::endl;
|
||||
}
|
||||
} else {
|
||||
std::cout << "PutObjectsFromDirectoryToCosPath Fail, ErrorMsg: "
|
||||
<< result.GetErrorMsg() << std::endl;
|
||||
}
|
||||
std::cout << "=========================================================" << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
qcloud_cos::CosAPI cos = InitCosAPI();
|
||||
CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
|
||||
PutObjectByFileDemo(cos);
|
||||
PutObjectByStreamDemo(cos);
|
||||
PutDirectoryDemo(cos);
|
||||
PutObjectsFromDirectoryDemo(cos);
|
||||
}
|
||||
71
demo/object_op_demo/restore_object_demo.cpp
Normal file
71
demo/object_op_demo/restore_object_demo.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
#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 PostObjectRestoreDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string object_name = "test.txt";
|
||||
qcloud_cos::PostObjectRestoreReq req(bucket_name, object_name); // 也可以传入第三个参数,version_id
|
||||
req.SetExiryDays(1);// 设置临时恢复的天数
|
||||
qcloud_cos::PostObjectRestoreResp resp;
|
||||
|
||||
qcloud_cos::CosResult result = cos.PostObjectRestore(req, &resp);
|
||||
|
||||
std::cout << "===================PostObjectRestore=====================" << std::endl;
|
||||
PrintResult(result, resp);
|
||||
std::cout << "=========================================================" << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
qcloud_cos::CosAPI cos = InitCosAPI();
|
||||
CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
|
||||
PostObjectRestoreDemo(cos);
|
||||
}
|
||||
77
demo/object_op_demo/select_objec_demo.cpp
Normal file
77
demo/object_op_demo/select_objec_demo.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
#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 SelectObjectContentDemo(qcloud_cos::CosAPI& cos) {
|
||||
std::string object_name = "test.csv.gz";
|
||||
int input_file_type = CSV; // 待检索对象的格式为CSV或者JSON
|
||||
int input_compress_type = COMPRESS_GZIP; // 压缩类型,COMPRESS_NONE, COMPRESS_GZIP, COMPRESS_BZIP2
|
||||
int out_file_type = CSV; // 输出格式为CSV或者JSON
|
||||
|
||||
qcloud_cos::SelectObjectContentReq req(bucket_name, object_name, input_file_type, input_compress_type, out_file_type);
|
||||
req.SetSqlExpression("Select * from COSObject");
|
||||
qcloud_cos::SelectObjectContentResp resp;
|
||||
qcloud_cos::CosResult result = cos.SelectObjectContent(req, &resp);
|
||||
|
||||
std::cout << "=====================IsObjectExist=======================" << std::endl;
|
||||
PrintResult(result, resp);
|
||||
// 支持打印最终结果至终端或写入本地文件
|
||||
// resp.WriteResultToLocalFile("file_name");
|
||||
resp.PrintResult();
|
||||
std::cout << "=========================================================" << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
qcloud_cos::CosAPI cos = InitCosAPI();
|
||||
CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
|
||||
SelectObjectContentDemo(cos);
|
||||
}
|
||||
394
demo/stable_test.cpp
Normal file
394
demo/stable_test.cpp
Normal file
@@ -0,0 +1,394 @@
|
||||
// Copyright (c) 2022, Tencent Inc.
|
||||
// All rights reserved.
|
||||
|
||||
#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"
|
||||
#include "util/test_utils.h"
|
||||
|
||||
using namespace qcloud_cos;
|
||||
|
||||
static void PrintResult(const qcloud_cos::CosResult& result) {
|
||||
std::stringstream oss;
|
||||
oss << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
|
||||
oss << "HttpStatus=" << result.GetHttpStatus() << std::endl;
|
||||
oss << "ErrorCode=" << result.GetErrorCode() << std::endl;
|
||||
oss << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
|
||||
oss << "ResourceAddr=" << result.GetResourceAddr() << std::endl;
|
||||
oss << "XCosRequestId=" << result.GetXCosRequestId() << std::endl;
|
||||
oss << "XCosTraceId=" << result.GetXCosTraceId() << std::endl;
|
||||
SDK_LOG_INFO("%s", oss.str().c_str());
|
||||
}
|
||||
|
||||
enum PutOpType { SIMPLE_PUT = 1, MULTI_PUT, ASYNC_PUT, ASYNC_MULTI_PUT };
|
||||
enum GetOpType { SIMPLE_GET = 6, MULTI_GET, ASYNC_GET, ASYNC_MULTI_GET };
|
||||
|
||||
#define GENERATE_LOCAL_FILE(size) \
|
||||
std::string object_name = "stable_test_size_" + std::to_string(size); \
|
||||
std::string local_file = "./" + object_name; \
|
||||
SDK_LOG_INFO("generate local file: %s", local_file.c_str()); \
|
||||
TestUtils::WriteRandomDatatoFile(local_file, file_size); \
|
||||
uint64_t file_crc64_origin = FileUtil::GetFileCrc64(local_file); \
|
||||
std::string file_md5_origin = FileUtil::GetFileMd5(local_file); \
|
||||
SDK_LOG_INFO("file_crc64_origin: %lu", file_crc64_origin); \
|
||||
SDK_LOG_INFO("file_md5_origin: %s", file_md5_origin.c_str());
|
||||
|
||||
#define CHECK_COMMON_RESULT(result) \
|
||||
assert(result.IsSucc()); \
|
||||
assert(!result.GetXCosRequestId().empty()); \
|
||||
assert(result.GetHttpStatus() >= 200 && result.GetHttpStatus() < 300);
|
||||
|
||||
#define CHECK_COMMON_RESP(resp) \
|
||||
assert(resp.GetServer() == "tencent-cos"); \
|
||||
assert(!resp.GetDate().empty()); \
|
||||
assert(!resp.GetConnection().empty()); \
|
||||
assert(!resp.GetXCosRequestId().empty());
|
||||
|
||||
#define CHECK_PUT_RESP(resp, op_type) \
|
||||
assert(resp.GetXCosHashCrc64Ecma() == std::to_string(file_crc64_origin)); \
|
||||
if (op_type == MULTI_PUT || op_type == ASYNC_MULTI_PUT) { \
|
||||
assert(!resp.GetContentType().empty()); \
|
||||
assert(resp.GetEtag() != file_md5_origin); \
|
||||
} else { \
|
||||
assert(resp.GetEtag() == file_md5_origin); \
|
||||
}
|
||||
|
||||
#define CHECK_HEAD_RESP(resp) \
|
||||
assert(resp.GetXCosHashCrc64Ecma() == std::to_string(file_crc64_origin)); \
|
||||
assert(resp.GetContentLength() == file_size); \
|
||||
assert(!resp.GetLastModified().empty());
|
||||
|
||||
#define CHECK_GET_RESP(resp, op_type) \
|
||||
assert(resp.GetXCosHashCrc64Ecma() == std::to_string(file_crc64_origin)); \
|
||||
assert(!resp.GetLastModified().empty()); \
|
||||
std::string file_md5_download = TestUtils::CalcFileMd5(local_file_download); \
|
||||
assert(file_md5_download == file_md5_origin); \
|
||||
if (op_type == SIMPLE_GET || op_type == ASYNC_GET) { \
|
||||
assert(resp.GetContentLength() == file_size); \
|
||||
}
|
||||
|
||||
class TestStatistics {
|
||||
public:
|
||||
TestStatistics()
|
||||
: put_object_succ(0),
|
||||
put_object_fail(0),
|
||||
multi_put_object_succ(0),
|
||||
multi_put_object_fail(0),
|
||||
async_put_object_succ(0),
|
||||
async_put_object_fail(0),
|
||||
async_multi_put_object_succ(0),
|
||||
async_multi_put_object_fail(0),
|
||||
get_object_succ(0),
|
||||
get_object_fail(0),
|
||||
multi_get_object_succ(0),
|
||||
multi_get_object_fail(0),
|
||||
async_get_object_succ(0),
|
||||
async_get_object_fail(0),
|
||||
async_multi_get_object_succ(0),
|
||||
async_multi_get_object_fail(0),
|
||||
head_object_succ(0),
|
||||
head_object_fail(0),
|
||||
delete_object_succ(0),
|
||||
delete_object_fail(0) {}
|
||||
|
||||
int put_object_succ;
|
||||
int put_object_fail;
|
||||
int multi_put_object_succ;
|
||||
int multi_put_object_fail;
|
||||
int async_put_object_succ;
|
||||
int async_put_object_fail;
|
||||
int async_multi_put_object_succ;
|
||||
int async_multi_put_object_fail;
|
||||
int get_object_succ;
|
||||
int get_object_fail;
|
||||
int multi_get_object_succ;
|
||||
int multi_get_object_fail;
|
||||
int async_get_object_succ;
|
||||
int async_get_object_fail;
|
||||
int async_multi_get_object_succ;
|
||||
int async_multi_get_object_fail;
|
||||
int head_object_succ;
|
||||
int head_object_fail;
|
||||
int delete_object_succ;
|
||||
int delete_object_fail;
|
||||
void UpdataStat(bool succ, int op_type) {
|
||||
switch (op_type) {
|
||||
case SIMPLE_PUT:
|
||||
succ ? put_object_succ++ : put_object_fail++;
|
||||
case MULTI_PUT:
|
||||
succ ? multi_put_object_succ++ : multi_put_object_fail++;
|
||||
case ASYNC_PUT:
|
||||
succ ? async_put_object_succ++ : async_put_object_fail++;
|
||||
case ASYNC_MULTI_PUT:
|
||||
succ ? async_multi_put_object_succ++ : async_multi_put_object_fail++;
|
||||
case SIMPLE_GET:
|
||||
succ ? get_object_succ++ : get_object_fail++;
|
||||
case MULTI_GET:
|
||||
succ ? multi_get_object_succ++ : multi_get_object_fail++;
|
||||
case ASYNC_GET:
|
||||
succ ? async_get_object_succ++ : async_get_object_fail++;
|
||||
case ASYNC_MULTI_GET:
|
||||
succ ? async_multi_get_object_succ++ : async_multi_get_object_fail++;
|
||||
}
|
||||
}
|
||||
void PrintStat() {
|
||||
std::stringstream oss;
|
||||
oss << std::endl;
|
||||
oss << "put_object_succ:" << put_object_succ << std::endl;
|
||||
oss << "put_object_fail:" << put_object_fail << std::endl;
|
||||
oss << "multi_put_object_succ:" << put_object_succ << std::endl;
|
||||
oss << "multi_put_object_fail:" << put_object_fail << std::endl;
|
||||
oss << "async_put_object_succ:" << put_object_succ << std::endl;
|
||||
oss << "async_put_object_fail:" << put_object_fail << std::endl;
|
||||
oss << "async_multi_put_object_succ:" << put_object_succ << std::endl;
|
||||
oss << "async_multi_put_object_fail:" << put_object_fail << std::endl;
|
||||
oss << "get_object_succ:" << put_object_succ << std::endl;
|
||||
oss << "get_object_fail:" << put_object_fail << std::endl;
|
||||
oss << "multi_get_object_succ:" << put_object_succ << std::endl;
|
||||
oss << "multi_get_object_fail:" << put_object_fail << std::endl;
|
||||
oss << "async_get_object_succ:" << put_object_succ << std::endl;
|
||||
oss << "async_get_object_fail:" << put_object_fail << std::endl;
|
||||
oss << "async_multi_get_object_succ:" << put_object_succ << std::endl;
|
||||
oss << "async_multi_get_object_fail:" << put_object_fail << std::endl;
|
||||
oss << "head_object_succ:" << put_object_succ << std::endl;
|
||||
oss << "head_object_fail:" << put_object_fail << std::endl;
|
||||
oss << "delete_object_succ:" << put_object_succ << std::endl;
|
||||
oss << "delete_object_fail:" << put_object_fail << std::endl;
|
||||
SDK_LOG_INFO("%s", oss.str().c_str());
|
||||
}
|
||||
};
|
||||
|
||||
void TestLogCallback(const std::string& log) {
|
||||
std::ofstream ofs;
|
||||
ofs.open("./stable_test.log", std::ios_base::app);
|
||||
ofs << log;
|
||||
ofs.close();
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
UNUSED_PARAM(argc)
|
||||
UNUSED_PARAM(argv)
|
||||
qcloud_cos::CosConfig config("./config.json");
|
||||
config.SetLogCallback(&TestLogCallback);
|
||||
qcloud_cos::CosAPI cos(config);
|
||||
TestStatistics stat;
|
||||
std::string bucket_name = "test-12345678";
|
||||
size_t max_file_size = 100 * 1024 * 1024;
|
||||
while (true) {
|
||||
std::vector<int> put_op_type_v = {SIMPLE_PUT, MULTI_PUT, ASYNC_PUT,
|
||||
ASYNC_MULTI_PUT};
|
||||
for (auto& op_type : put_op_type_v) {
|
||||
size_t file_size = rand() % max_file_size;
|
||||
GENERATE_LOCAL_FILE(file_size)
|
||||
|
||||
bool done = false;
|
||||
std::mutex mutex;
|
||||
std::condition_variable cond;
|
||||
|
||||
auto put_done_cb = [file_crc64_origin, file_md5_origin, &done, &mutex,
|
||||
&cond, &stat](const SharedAsyncContext& context,
|
||||
void* user_data) {
|
||||
int op_type = (*reinterpret_cast<int*>(user_data));
|
||||
CHECK_COMMON_RESULT(context->GetResult())
|
||||
if (context->GetResult().IsSucc()) {
|
||||
stat.UpdataStat(true, op_type);
|
||||
} else {
|
||||
stat.UpdataStat(false, op_type);
|
||||
}
|
||||
AsyncResp resp = context->GetAsyncResp();
|
||||
CHECK_COMMON_RESP(resp)
|
||||
CHECK_PUT_RESP(resp, op_type)
|
||||
done = true;
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
cond.notify_one();
|
||||
};
|
||||
|
||||
// put object
|
||||
CosResult result;
|
||||
PutObjectByFileResp resp1;
|
||||
MultiPutObjectResp resp2;
|
||||
if (op_type == SIMPLE_PUT) {
|
||||
SDK_LOG_INFO("put object, object_name: %s", object_name.c_str());
|
||||
PutObjectByFileReq req(bucket_name, object_name, local_file);
|
||||
result = cos.PutObject(req, &resp1);
|
||||
} else if (op_type == MULTI_PUT) {
|
||||
SDK_LOG_INFO("multi put object, object_name: %s", object_name.c_str());
|
||||
MultiPutObjectReq req(bucket_name, object_name, local_file);
|
||||
result = cos.MultiPutObject(req, &resp2);
|
||||
} else if (op_type == ASYNC_PUT) {
|
||||
SDK_LOG_INFO("async put object, object_name: %s", object_name.c_str());
|
||||
AsyncPutObjectReq req(bucket_name, object_name, local_file);
|
||||
req.SetDoneCallback(put_done_cb);
|
||||
req.SetUserData(reinterpret_cast<void*>(&op_type));
|
||||
MultiPutObjectResp resp;
|
||||
cos.AsyncPutObject(req);
|
||||
} else if (op_type == ASYNC_MULTI_PUT) {
|
||||
SDK_LOG_INFO("async multi put object, object_name: %s",
|
||||
object_name.c_str());
|
||||
AsyncMultiPutObjectReq req(bucket_name, object_name, local_file);
|
||||
req.SetUserData(reinterpret_cast<void*>(&op_type));
|
||||
req.SetDoneCallback(put_done_cb);
|
||||
cos.AsyncMultiPutObject(req);
|
||||
}
|
||||
if (op_type == SIMPLE_PUT || op_type == MULTI_PUT) {
|
||||
if (!result.IsSucc()) {
|
||||
SDK_LOG_ERR("failed to put object, object_name: %s",
|
||||
object_name.c_str());
|
||||
PrintResult(result);
|
||||
stat.UpdataStat(false, op_type);
|
||||
} else {
|
||||
stat.UpdataStat(true, op_type);
|
||||
CHECK_COMMON_RESULT(result)
|
||||
if (op_type == SIMPLE_PUT) {
|
||||
CHECK_COMMON_RESP(resp1)
|
||||
CHECK_PUT_RESP(resp1, op_type)
|
||||
} else {
|
||||
CHECK_COMMON_RESP(resp2)
|
||||
CHECK_PUT_RESP(resp2, op_type)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
while (!done) {
|
||||
SDK_LOG_INFO("wait for put completing, object_name: %s",
|
||||
object_name.c_str());
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
cond.wait_until(
|
||||
lock, std::chrono::system_clock::now() + std::chrono::seconds(1));
|
||||
}
|
||||
}
|
||||
|
||||
// head object
|
||||
HeadObjectReq head_req(bucket_name, object_name);
|
||||
HeadObjectResp headresp;
|
||||
SDK_LOG_INFO("head object, object_name: %s", object_name.c_str());
|
||||
result = cos.HeadObject(head_req, &headresp);
|
||||
if (!result.IsSucc()) {
|
||||
SDK_LOG_ERR("failed to head object, object_name: %s",
|
||||
object_name.c_str());
|
||||
PrintResult(result);
|
||||
stat.head_object_fail++;
|
||||
} else {
|
||||
stat.head_object_succ++;
|
||||
CHECK_COMMON_RESULT(result)
|
||||
CHECK_COMMON_RESP(headresp)
|
||||
CHECK_HEAD_RESP(headresp)
|
||||
}
|
||||
|
||||
// get object
|
||||
// 使用多个接口下载同一个对象
|
||||
done = false;
|
||||
std::string local_file_download = local_file + "_download";
|
||||
auto get_done_cb = [file_size, local_file_download, file_crc64_origin,
|
||||
file_md5_origin, &done, &mutex, &cond,
|
||||
&stat](const SharedAsyncContext& context,
|
||||
void* user_data) {
|
||||
int op_type = (*reinterpret_cast<int*>(user_data));
|
||||
CHECK_COMMON_RESULT(context->GetResult())
|
||||
if (context->GetResult().IsSucc()) {
|
||||
stat.UpdataStat(true, op_type);
|
||||
} else {
|
||||
stat.UpdataStat(false, op_type);
|
||||
}
|
||||
AsyncResp resp = context->GetAsyncResp();
|
||||
CHECK_COMMON_RESP(resp)
|
||||
CHECK_GET_RESP(resp, op_type)
|
||||
done = true;
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
cond.notify_one();
|
||||
};
|
||||
std::vector<int> get_op_type_v = {SIMPLE_GET, MULTI_GET, ASYNC_GET,
|
||||
ASYNC_MULTI_GET};
|
||||
for (auto& op_type : get_op_type_v) {
|
||||
GetObjectByFileResp resp1;
|
||||
MultiGetObjectResp resp2;
|
||||
if (op_type == SIMPLE_GET) {
|
||||
SDK_LOG_INFO("get object, object_name: %s", object_name.c_str());
|
||||
GetObjectByFileReq req(bucket_name, object_name, local_file_download);
|
||||
result = cos.GetObject(req, &resp1);
|
||||
} else if (op_type == MULTI_GET) {
|
||||
SDK_LOG_INFO("multi get object, object_name: %s",
|
||||
object_name.c_str());
|
||||
MultiGetObjectReq req(bucket_name, object_name, local_file_download);
|
||||
result = cos.MultiGetObject(req, &resp2);
|
||||
} else if (op_type == ASYNC_GET) {
|
||||
SDK_LOG_INFO("async get object, object_name: %s",
|
||||
object_name.c_str());
|
||||
AsyncGetObjectReq req(bucket_name, object_name, local_file_download);
|
||||
req.SetDoneCallback(get_done_cb);
|
||||
req.SetUserData(reinterpret_cast<void*>(&op_type));
|
||||
cos.AsyncGetObject(req);
|
||||
} else if (op_type == ASYNC_MULTI_GET) {
|
||||
SDK_LOG_INFO("async multi get object, object_name: %s",
|
||||
object_name.c_str());
|
||||
AsyncMultiGetObjectReq req(bucket_name, object_name,
|
||||
local_file_download);
|
||||
req.SetUserData(reinterpret_cast<void*>(&op_type));
|
||||
req.SetDoneCallback(get_done_cb);
|
||||
cos.AsyncMultiGetObject(req);
|
||||
}
|
||||
if (op_type == SIMPLE_GET || op_type == MULTI_GET) {
|
||||
if (!result.IsSucc()) {
|
||||
SDK_LOG_ERR("failed to get object, object_name: %s",
|
||||
object_name.c_str());
|
||||
PrintResult(result);
|
||||
stat.UpdataStat(false, op_type);
|
||||
} else {
|
||||
stat.UpdataStat(true, op_type);
|
||||
CHECK_COMMON_RESULT(result)
|
||||
if (op_type == SIMPLE_GET) {
|
||||
CHECK_COMMON_RESP(resp1)
|
||||
CHECK_GET_RESP(resp1, op_type)
|
||||
} else {
|
||||
CHECK_COMMON_RESP(resp2)
|
||||
CHECK_GET_RESP(resp2, op_type)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
while (!done) {
|
||||
SDK_LOG_INFO("wait for get completing, object_name: %s",
|
||||
object_name.c_str());
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
cond.wait_until(lock, std::chrono::system_clock::now() +
|
||||
std::chrono::seconds(1));
|
||||
}
|
||||
done = false;
|
||||
}
|
||||
}
|
||||
|
||||
// delete object
|
||||
SDK_LOG_INFO("delete object, object_name: %s", object_name.c_str());
|
||||
qcloud_cos::DeleteObjectReq del_req(bucket_name, object_name);
|
||||
qcloud_cos::DeleteObjectResp del_resp;
|
||||
result = cos.DeleteObject(del_req, &del_resp);
|
||||
if (!result.IsSucc()) {
|
||||
SDK_LOG_ERR("failed to delete object, object_name: %s",
|
||||
object_name.c_str());
|
||||
PrintResult(result);
|
||||
stat.delete_object_fail++;
|
||||
} else {
|
||||
stat.delete_object_succ++;
|
||||
CHECK_COMMON_RESULT(result)
|
||||
CHECK_COMMON_RESP(del_resp)
|
||||
}
|
||||
|
||||
// delete local file
|
||||
TestUtils::RemoveFile(local_file);
|
||||
TestUtils::RemoveFile(local_file_download);
|
||||
}
|
||||
stat.PrintStat();
|
||||
}
|
||||
|
||||
#if defined(_WIN32)
|
||||
system("pause");
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
BIN
demo/test_file/audio.mp3
Normal file
BIN
demo/test_file/audio.mp3
Normal file
Binary file not shown.
BIN
demo/test_file/document.docx
Normal file
BIN
demo/test_file/document.docx
Normal file
Binary file not shown.
BIN
demo/test_file/douyin-00000.ts
Normal file
BIN
demo/test_file/douyin-00000.ts
Normal file
Binary file not shown.
7
demo/test_file/douyin.m3u8
Normal file
7
demo/test_file/douyin.m3u8
Normal file
@@ -0,0 +1,7 @@
|
||||
#EXTM3U
|
||||
#EXT-X-VERSION:3
|
||||
#EXT-X-TARGETDURATION:5
|
||||
#EXT-X-MEDIA-SEQUENCE:0
|
||||
#EXTINF:5.000000,
|
||||
douyin-00000.ts
|
||||
#EXT-X-ENDLIST
|
||||
BIN
demo/test_file/test.jpg
Normal file
BIN
demo/test_file/test.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 22 KiB |
7
demo/test_file/test.srt
Normal file
7
demo/test_file/test.srt
Normal file
@@ -0,0 +1,7 @@
|
||||
1
|
||||
00:00:04,160 --> 00:00:08,640
|
||||
srt字幕啊,我一杯品尝你的啡,留下唇印的嘴。蝴蝶玫瑰名字写错谁告白节说风吹的对街微笑在脸上飞。哼,和你说你有点难追,想让我知难而退。礼物不屑掉,最贵只要乡榭的落叶营造浪漫的约回,不害怕留在一切拥有。
|
||||
|
||||
2
|
||||
00:00:08,640 --> 00:00:12,680
|
||||
你就拥有全世界。亲爱的,爱上你从那天起甜蜜的痕迹,亲爱的,别人性你的眼睛在说,我愿意咖啡,我说一杯品尝你的啡,留下唇印的嘴,蝴蝶,玫瑰。
|
||||
BIN
demo/test_file/test.zip
Normal file
BIN
demo/test_file/test.zip
Normal file
Binary file not shown.
1
demo/test_file/text.txt
Normal file
1
demo/test_file/text.txt
Normal file
@@ -0,0 +1 @@
|
||||
texttest
|
||||
BIN
demo/test_file/video.mp4
Normal file
BIN
demo/test_file/video.mp4
Normal file
Binary file not shown.
Reference in New Issue
Block a user