1、Poco的安装和编译,依赖本地的openssl库,这个ssl库可能跟项目用的库相冲突(1.0和1.1的openssl库冲突)。
这时候需要安装项目的openssl版本,编译poco库完成后,再产生cossdk.a,否则版本冲突。
2、编译cossdk.a时,注意设置openssl版本“SET(OPENSSL_VERSION 1.0.2f)”。同时注意这个版本必须和poco编译时使用的版本相同
3、默认openssl的库,一般在/lib64目录下,建议备份原来的,安装新的。
下面是一个cos demo
// Created: 07/18/17
// Description:
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include "util/auth_tool.h"
#include "cos_api.h"
#include "cos_sys_config.h"
#include "cos_defines.h"
using namespace qcloud_cos;
void PrintResult(const qcloud_cos::CosResult& result, const qcloud_cos::BaseResp& resp) {
if (result.IsSucc()) {
std::cout << resp.DebugString() << std::endl;
} else {
std::cout << "ErrorInfo=" << result.GetErrorInfo() << 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;
// std::cout << "result=" << result << std::endl;
}
}
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;
}
void HeadBucket(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
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 DeleteBucket(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
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;
}
void PutBucket(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
qcloud_cos::PutBucketReq req(bucket_name);
qcloud_cos::PutBucketResp resp;
qcloud_cos::CosResult result = cos.PutBucket(req, &resp);
std::cout << "===================PutBucketResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "=========================================================" << std::endl;
}
void GetBucket(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
qcloud_cos::GetBucketReq req(bucket_name);
qcloud_cos::GetBucketResp resp;
qcloud_cos::CosResult result = cos.GetBucket(req, &resp);
std::cout << "===================GetBucketResponse=====================" << std::endl;
PrintResult(result, resp);
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;
}
std::cout << "=========================================================" << std::endl;
}
void PutBucketVersioning(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
qcloud_cos::PutBucketVersioningReq req(bucket_name);
req.SetStatus(true);
qcloud_cos::PutBucketVersioningResp resp;
qcloud_cos::CosResult result = cos.PutBucketVersioning(req, &resp);
std::cout << "===================PutBucketVersioningResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
void GetBucketVersioning(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
qcloud_cos::GetBucketVersioningReq req(bucket_name);
qcloud_cos::GetBucketVersioningResp resp;
qcloud_cos::CosResult result = cos.GetBucketVersioning(req, &resp);
std::cout << "Bucket status= " << resp.GetStatus() << std::endl;
std::cout << "===================GetBucketVersioningResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
void PutBucketReplication(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
qcloud_cos::PutBucketReplicationReq req(bucket_name);
req.SetRole("qcs::cam::uin/2779643970:uin/2779643970");
qcloud_cos::ReplicationRule rule("",
"qcs:id/0:cos:cn-south:appid/1251668577:sevenyoutest1251668577",
"", "", true);
req.AddReplicationRule(rule);
qcloud_cos::PutBucketReplicationResp resp;
qcloud_cos::CosResult result = cos.PutBucketReplication(req, &resp);
std::cout << "===================PutBucketReplicationResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
void GetBucketReplication(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
qcloud_cos::GetBucketReplicationReq req(bucket_name);
qcloud_cos::GetBucketReplicationResp resp;
qcloud_cos::CosResult result = cos.GetBucketReplication(req, &resp);
std::cout << "===================GetBucketReplicationResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
void DeleteBucketReplication(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
qcloud_cos::DeleteBucketReplicationReq req(bucket_name);
qcloud_cos::DeleteBucketReplicationResp resp;
qcloud_cos::CosResult result = cos.DeleteBucketReplication(req, &resp);
std::cout << "===================DeleteBucketReplicationResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
void PutBucketLifecycle(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
qcloud_cos::PutBucketLifecycleReq req(bucket_name);
{
qcloud_cos::LifecycleRule rule;
rule.SetIsEnable(true);
rule.SetId("lifecycle_rule00");
qcloud_cos::LifecycleFilter filter;
filter.SetPrefix("sevenyou_e1");
rule.SetFilter(filter);
qcloud_cos::LifecycleTransition transition;
transition.SetDays(30);
transition.SetStorageClass("Standard_IA");
rule.AddTransition(transition);
req.AddRule(rule);
}
{
qcloud_cos::LifecycleRule rule;
rule.SetIsEnable(true);
rule.SetId("lifecycle_rule01");
qcloud_cos::LifecycleFilter filter;
filter.SetPrefix("sevenyou_e1");
rule.SetFilter(filter);
qcloud_cos::LifecycleTransition transition;
transition.SetDays(1);
transition.SetStorageClass("Standard");
rule.AddTransition(transition);
qcloud_cos::LifecycleTransition transition2;
transition2.SetDays(2);
transition2.SetStorageClass("Standard_IA");
rule.AddTransition(transition2);
req.AddRule(rule);
}
qcloud_cos::PutBucketLifecycleResp resp;
qcloud_cos::CosResult result = cos.PutBucketLifecycle(req, &resp);
std::cout << "===================PutBucketLifecycleResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
void GetBucketLifecycle(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
qcloud_cos::GetBucketLifecycleReq req(bucket_name);
qcloud_cos::GetBucketLifecycleResp resp;
qcloud_cos::CosResult result = cos.GetBucketLifecycle(req, &resp);
const std::vector<qcloud_cos::LifecycleRule>& rules = resp.GetRules();
for (int idx = 0; idx != rules.size(); ++idx) {
std::cout << "id = " << rules[idx].GetId() << std::endl;
}
std::cout << "===================GetBucketLifecycleResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
void DeleteBucketLifecycle(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
qcloud_cos::DeleteBucketLifecycleReq req(bucket_name);
qcloud_cos::DeleteBucketLifecycleResp resp;
qcloud_cos::CosResult result = cos.DeleteBucketLifecycle(req, &resp);
std::cout << "===================DeleteBucketLifecycleResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
void PutBucketACL(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
// 1. 设置ACL配置(通过Body, 设置ACL可以通过Body、Header两种方式,但只能二选一,否则会有冲突)
{
qcloud_cos::PutBucketACLReq req(bucket_name);
qcloud_cos::Owner owner = {"qcs::cam::uin/491107627:uin/491107627", "qcs::cam::uin/491107627:uin/491107627" };
qcloud_cos::Grant grant;
req.SetOwner(owner);
grant.m_grantee.m_type = "Group";
grant.m_grantee.m_uri = "http://cam.qcloud.com/groups/global/AllUsers";
grant.m_perm = "READ";
req.AddAccessControlList(grant);
qcloud_cos::PutBucketACLResp resp;
qcloud_cos::CosResult result = cos.PutBucketACL(req, &resp);
std::cout << "===================PutBucketACLResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
#if 0
// 2. 设置ACL配置(通过Header, 设置ACL可以通过Body、Header两种方式,但只能二选一,否则会有冲突)
{
qcloud_cos::PutBucketACLReq req(bucket_name);
req.SetXCosAcl("public-read-write");
qcloud_cos::PutBucketACLResp resp;
qcloud_cos::CosResult result = cos.PutBucketACL(req, &resp);
std::cout << "===================PutBucketACLResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
#endif
}
void GetBucketACL(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
qcloud_cos::GetBucketACLReq req(bucket_name);
qcloud_cos::GetBucketACLResp resp;
qcloud_cos::CosResult result = cos.GetBucketACL(req, &resp);
std::vector<qcloud_cos::Grant> grants = resp.GetAccessControlList();
for (size_t idx = 0; idx < grants.size(); ++idx) {
std::cout << "Type is " << grants[idx].m_grantee.m_type << std::endl;
}
std::cout << "===================GetBucketACLResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
void PutBucketCORS(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
qcloud_cos::PutBucketCORSReq req(bucket_name);
qcloud_cos::CORSRule rule;
rule.m_id = "123";
rule.m_allowed_headers.push_back("x-cos-meta-test");
rule.m_allowed_origins.push_back("http://www.qq.com");
rule.m_allowed_origins.push_back("http://www.qcloud.com");
rule.m_allowed_methods.push_back("PUT");
rule.m_allowed_methods.push_back("GET");
rule.m_max_age_secs = "600";
rule.m_expose_headers.push_back("x-cos-expose");
req.AddRule(rule);
qcloud_cos::PutBucketCORSResp resp;
qcloud_cos::CosResult result = cos.PutBucketCORS(req, &resp);
std::cout << "===================PutBucketCORSResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
void GetBucketCORS(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
qcloud_cos::GetBucketCORSReq req(bucket_name);
qcloud_cos::GetBucketCORSResp resp;
qcloud_cos::CosResult result = cos.GetBucketCORS(req, &resp);
std::cout << "===================GetBucketCORSResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
void DeleteBucketCORS(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
qcloud_cos::DeleteBucketCORSReq req(bucket_name);
qcloud_cos::DeleteBucketCORSResp resp;
qcloud_cos::CosResult result = cos.DeleteBucketCORS(req, &resp);
std::cout << "===================DeleteBucketCORSResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
void PutObjectByFile(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
const std::string& object_name, const std::string& file_path) {
qcloud_cos::PutObjectByFileReq req(bucket_name, object_name, file_path);
//req.AddParam("versionId", "MTg0NDY3NDI1NjQ4NjUyMjQ1MTA");
qcloud_cos::PutObjectByFileResp resp;
qcloud_cos::CosResult result = cos.PutObject(req, &resp);
std::cout << "===================PutObjectResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "=========================================================" << std::endl;
}
void PutObjectByStream(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
std::istringstream iss("put object");
qcloud_cos::PutObjectByStreamReq req(bucket_name, "sevenyou_10m", iss);
qcloud_cos::PutObjectByStreamResp resp;
qcloud_cos::CosResult result = cos.PutObject(req, &resp);
std::cout << "===================PutObjectResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "=========================================================" << std::endl;
}
void HeadObject(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
const std::string& object_name) {
qcloud_cos::HeadObjectReq req(bucket_name, object_name);
//req.AddParam("versionId", "MTg0NDY3NDI1NjQ4NjUyMjQ1MTA");
qcloud_cos::HeadObjectResp resp;
qcloud_cos::CosResult result = cos.HeadObject(req, &resp);
std::cout << "===================HeadObjectResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "==========================================================" << std::endl;
}
void GetObjectByFile(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
const std::string& object_name, const std::string& file_path) {
qcloud_cos::GetObjectByFileReq req(bucket_name, object_name, file_path);
//req.AddParam("versionId", "MTg0NDY3NDI1NjQwODU3NzU2MTA");
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 GetObjectByStream(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
const std::string& object_name) {
std::ostringstream os;
qcloud_cos::GetObjectByStreamReq req(bucket_name,
object_name, os);
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;
}
// 将 Object 下载到本地文件中(多线程)
void MultiGetObject(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
const std::string& object_name, const std::string& file_path) {
qcloud_cos::MultiGetObjectReq req(bucket_name,
object_name, file_path);
qcloud_cos::MultiGetObjectResp resp;
qcloud_cos::CosResult result = cos.GetObject(req, &resp);
std::cout << "===================GetObjectResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "=========================================================" << std::endl;
}
void InitMultiUpload(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
const std::string& object_name, std::string* upload_id) {
qcloud_cos::InitMultiUploadReq req(bucket_name, object_name);
qcloud_cos::InitMultiUploadResp resp;
qcloud_cos::CosResult result = cos.InitMultiUpload(req, &resp);
std::cout << "=====================InitMultiUpload=====================";
PrintResult(result, resp);
std::cout << "=========================================================";
*upload_id = resp.GetUploadId();
}
void UploadPartData(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
const std::string& object_name, const std::string& upload_id,
std::fstream& is, uint64_t number,
std::string* etag) {
qcloud_cos::UploadPartDataReq req(bucket_name, object_name,
upload_id, is);
req.SetPartNumber(number);
qcloud_cos::UploadPartDataResp resp;
qcloud_cos::CosResult result = cos.UploadPartData(req, &resp);
*etag = resp.GetEtag();
std::cout << "======================UploadPartData=====================";
PrintResult(result, resp);
std::cout << "=========================================================";
}
void AbortMultiUpload(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
const std::string& object_name, const std::string& upload_id) {
qcloud_cos::AbortMultiUploadReq req(bucket_name, object_name,
upload_id);
qcloud_cos::AbortMultiUploadResp resp;
qcloud_cos::CosResult result = cos.AbortMultiUpload(req, &resp);
std::cout << "======================AbortUploadPart=====================";
PrintResult(result, resp);
std::cout << "=========================================================";
}
void CompleteMultiUpload(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
const std::string& object_name, const std::string& upload_id,
const std::vector<std::string>& etags,
const std::vector<uint64_t>& numbers) {
qcloud_cos::CompleteMultiUploadReq req(bucket_name, object_name, upload_id);
qcloud_cos::CompleteMultiUploadResp resp;
req.AddParam("versionId", "MTg0NDY3NDI1NjQ4NjUyMjQ1MTA");
req.SetEtags(etags);
req.SetPartNumbers(numbers);
qcloud_cos::CosResult result = cos.CompleteMultiUpload(req, &resp);
std::cout << "===================Complete=============================" << std::endl;
PrintResult(result, resp);
std::cout << "========================================================" << std::endl;
}
void MultiUploadObject(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
const std::string& object_name, const std::string& local_file) {
qcloud_cos::MultiUploadObjectReq req(bucket_name,
object_name, local_file);
// Complete 接口内部 chunk 保活,建议设置较长时间的 timeout。
req.SetRecvTimeoutInms(1000 * 60);
qcloud_cos::MultiUploadObjectResp resp;
qcloud_cos::CosResult result = cos.MultiUploadObject(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;
/*
注意GetLocation不带http://
private-deploy-package-1256237915.cos.ap-beijing.myqcloud.com/test/tmp3
test/tmp3
private-deploy-package-1256237915
829eb686be4045b02c8f78167928a930-1
*/
std::string url = "http://" + resp.GetLocation();
std::cout << "url = " << url << std::endl;
} else {
std::cout << "MultiUpload Fail." << std::endl;
// 获取具体失败在哪一步
std::string resp_tag = resp.GetRespTag();
std::cout << "resp_tag=" << resp_tag << std::endl;
if ("Init" == resp_tag) {
// print result
} else if ("Upload" == resp_tag) {
// print result
} else if ("Complete" == resp_tag) {
// print result
}
}
std::cout << "===================MultiUpload=============================" << std::endl;
PrintResult(result, resp);
std::cout << "========================================================" << std::endl;
}
void ListParts(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
const std::string& object_name, const std::string& upload_id) {
qcloud_cos::ListPartsReq req(bucket_name, object_name, upload_id);
req.SetMaxParts(1);
req.SetPartNumberMarker("1");
qcloud_cos::ListPartsResp resp;
qcloud_cos::CosResult result = cos.ListParts(req, &resp);
std::cout << "===================ListParts=====================" << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
void PutObjectACL(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
const std::string& object_name) {
// 1 设置ACL配置(通过Body, 设置ACL可以通过Body、Header两种方式,但只能二选一,否则会有冲突)
{
qcloud_cos::PutObjectACLReq req(bucket_name, object_name);
qcloud_cos::Owner owner = {"qcs::cam::uin/491107627:uin/491107627", "qcs::cam::uin/491107627:uin/491107627" };
qcloud_cos::Grant grant;
req.SetOwner(owner);
grant.m_grantee.m_type = "Group";
grant.m_grantee.m_uri = "http://cam.qcloud.com/groups/global/AllUsers";
grant.m_perm = "READ";
req.AddAccessControlList(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 设置ACL配置(通过Header, 设置ACL可以通过Body、Header两种方式,但只能二选一,否则会有冲突)
{
qcloud_cos::PutObjectACLReq req(bucket_name, object_name);
req.SetXCosAcl("public-read-write");
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 GetObjectACL(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
const std::string& object_name) {
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;
}
void PutObjectCopy(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
const std::string& object_name, const std::string& source) {
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;
}
void DeleteObject(qcloud_cos::CosAPI& cos, const std::string& bucket_name, const std::string& object_name) {
qcloud_cos::DeleteObjectReq req(bucket_name, object_name);
qcloud_cos::DeleteObjectResp resp;
qcloud_cos::CosResult result = cos.DeleteObject(req, &resp);
std::cout << "===================DeleteObjectResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "=========================================================" << std::endl;
}
void DeleteObjects(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
const std::vector<ObjectVersionPair>& objects) {
qcloud_cos::DeleteObjectsReq req(bucket_name, objects);
qcloud_cos::DeleteObjectsResp resp;
qcloud_cos::CosResult result = cos.DeleteObjects(req, &resp);
std::cout << "===================DeleteObjectsResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "=========================================================" << std::endl;
std::cout << "Resp body=[" << resp.DebugString() << "]" << std::endl;
}
void UploadPartCopy(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
const std::string& object_name, const std::string& upload_id,
const std::string& source, const std::string& range,
uint64_t number, std::string* etag) {
qcloud_cos::UploadPartCopyDataReq req(bucket_name, object_name, upload_id, number);
req.SetXCosCopySource(source);
req.SetXCosCopySourceRange(range);
qcloud_cos::UploadPartCopyDataResp resp;
qcloud_cos::CosResult result = cos.UploadPartCopyData(req, &resp);
std::cout << "===================UploadPartCopyDataResp=====================" << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
if (result.IsSucc()) {
*etag = resp.GetEtag();
}
}
void Copy(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
const std::string& object_name, const std::string& source) {
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;
}
void ListMultipartUpload(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
qcloud_cos::ListMultipartUploadReq req(bucket_name);
qcloud_cos::ListMultipartUploadResp resp;
qcloud_cos::CosResult result = cos.ListMultipartUpload(req, &resp);
std::vector<Upload> rst = resp.GetUpload();
for (std::vector<qcloud_cos::Upload>::const_iterator itr = rst.begin(); itr != rst.end(); ++itr) {
const qcloud_cos::Upload& upload = *itr;
std::cout << "key = " << upload.m_key << ", uploadid= " << upload.m_uploadid << ", storagen class = "
<< upload.m_storage_class << ", m_initiated= " << upload.m_initiated << std::endl;
}
std::cout << "===================ListMultipartUpload=====================" << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
// 开启bucket日志配置
void PutBucketLogging(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
const std::string& TargetBucketname,
const std::string& TargetPrefix) {
qcloud_cos::PutBucketLoggingReq req(bucket_name);
qcloud_cos::LoggingEnabled rules;
rules.SetTargetBucket(TargetBucketname);
rules.SetTargetPrefix(TargetPrefix);
req.SetLoggingEnabled(rules);
qcloud_cos::PutBucketLoggingResp resp;
qcloud_cos::CosResult result = cos.PutBucketLogging(req, &resp);
std::cout << "===================PutBucketLoggingResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
// 获取bucket日志配置
void GetBucketLogging(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
qcloud_cos::GetBucketLoggingReq req(bucket_name);
qcloud_cos::GetBucketLoggingResp resp;
qcloud_cos::CosResult result = cos.GetBucketLogging(req, &resp);
std::cout << "===================GetBucketLoggingResponse=====================" << std::endl;
const qcloud_cos::LoggingEnabled loggingenabled = resp.GetLoggingEnabled();
std::cout << "TargetBucket = " << loggingenabled.GetTargetBucket() << std::endl;
std::cout << "TargetPrefix = " << loggingenabled.GetTargetPrefix() << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
// 设置存储桶绑定自定义域名
void PutBucketDomain(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
qcloud_cos::PutBucketDomainReq req(bucket_name);
DomainRule rules;
rules.SetStatus("ENABLED");
rules.SetName("www.abc.com");
rules.SetType("REST");
//rules.SetForcedReplacement();
req.SetDomainRule(rules);
qcloud_cos::PutBucketDomainResp resp;
qcloud_cos::CosResult result = cos.PutBucketDomain(req, &resp);
std::cout << "===================PutBucketDomainResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
// 获取存储桶绑定自定义域名
void GetBucketDomain(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
qcloud_cos::GetBucketDomainReq req(bucket_name);
qcloud_cos::GetBucketDomainResp resp;
qcloud_cos::CosResult result = cos.GetBucketDomain(req, &resp);
std::cout << "===================GetBucketDomainResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
// 为存储桶配置静态网站
void PutBucketWebsite(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
qcloud_cos::PutBucketWebsiteReq req(bucket_name);
qcloud_cos::PutBucketWebsiteResp resp;
req.SetSuffix("index.xml"); //必选项
req.SetProtocol("https");
req.SetKey("Error.html");
//设置重定向规则,最多设置100条
//设置第一条规则
qcloud_cos::RoutingRule routerule1;
qcloud_cos::Condition temp_condtion1;
temp_condtion1.SetHttpErrorCodeReturnedEquals(404);//需要设置,默认404
routerule1.SetCondition(temp_condtion1);
qcloud_cos::Redirect temp_redirect1;
temp_redirect1.SetProtocol("https");
temp_redirect1.SetReplaceKeyWith("404.htmp");
routerule1.SetRedirect(temp_redirect1);
//设置第二条规则
qcloud_cos::RoutingRule routerule2;
qcloud_cos::Condition temp_condtion2;
temp_condtion2.SetHttpErrorCodeReturnedEquals(403);//需要设置,默认404
routerule2.SetCondition(temp_condtion2);
qcloud_cos::Redirect temp_redirect2;
temp_redirect2.SetProtocol("https");
temp_redirect2.SetReplaceKeyWith("403.htmp");
routerule2.SetRedirect(temp_redirect2);
//设置第三条规则
qcloud_cos::RoutingRule routerule3;
qcloud_cos::Condition temp_condtion3;
temp_condtion3.SetKeyPrefixEquals("img/");
temp_condtion3.SetHttpErrorCodeReturnedEquals(402);
routerule3.SetCondition(temp_condtion3);
qcloud_cos::Redirect temp_redirect3;
temp_redirect3.SetProtocol("https");
temp_redirect3.SetReplaceKeyWith("401.htmp");
routerule3.SetRedirect(temp_redirect3);
qcloud_cos::RoutingRule routerule4;
qcloud_cos::Condition temp_condtion4;
temp_condtion4.SetKeyPrefixEquals("img1/");
routerule4.SetCondition(temp_condtion4);
qcloud_cos::Redirect temp_redirect4;
temp_redirect4.SetProtocol("https");
temp_redirect4.SetReplaceKeyPrefixWith("402.htmp");
routerule4.SetRedirect(temp_redirect4);
req.AddRoutingRule(routerule1);
req.AddRoutingRule(routerule2);
req.AddRoutingRule(routerule3);
req.AddRoutingRule(routerule4);
qcloud_cos::CosResult result = cos.PutBucketWebsite(req, &resp);
std::cout << "===================PutBucketWebsite=====================" << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
// 获取与存储桶关联的静态网站配置信息
void GetBucketWebsite(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
qcloud_cos::GetBucketWebsiteReq req(bucket_name);
qcloud_cos::GetBucketWebsiteResp resp;
qcloud_cos::CosResult result = cos.GetBucketWebsite(req, &resp);
std::cout << "===================GetBucketWebsite=====================" << std::endl;
std::cout << "Suffix:" << resp.GetSuffix() << std::endl;
std::cout << "Protocol:" << resp.GetProtocol() << std::endl;
std::cout << "ErrorDocument:" << resp.GetKey() << std::endl;
std::vector<RoutingRule> routingrules = resp.GetRoutingRules();
std::vector<RoutingRule>::iterator it = routingrules.begin();
for(; it != routingrules.end(); ++it) {
const Condition& condition = it->GetCondition();
std::cout << "condition httpcode" << condition.GetHttpErrorCodeReturnedEquals() << std::endl;
std::cout << "condition KeyPrefixEquals:" << condition.GetKeyPrefixEquals() << std::endl;
const Redirect& redirect = it->GetRedirect();
std::cout << "redirect Protocol:" <<redirect.GetProtocol() << std::endl;
std::cout << "redirect ReplaceKeyWith" << redirect.GetReplaceKeyWith() << std::endl;
std::cout << "redirect ReplaceKeyPrefixWith" <<redirect.GetReplaceKeyPrefixWith() << std::endl;
std::cout << std::endl;
}
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
// 删除存储桶中的静态网站配置
void DeleteBucketWebsite(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
qcloud_cos::DeleteBucketWebsiteReq req(bucket_name);
qcloud_cos::DeleteBucketWebsiteResp resp;
qcloud_cos::CosResult result = cos.DeleteBucketWebsite(req, &resp);
std::cout << "===================DeleteBucketWebsite=====================" << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
// 为已存在的 Bucket 设置标签(Tag)
void PutBucketTagging(qcloud_cos::CosAPI& cos, const std::string& bucket_name, std::vector<Tag>& tagset) {
qcloud_cos::PutBucketTaggingReq req(bucket_name);
qcloud_cos::PutBucketTaggingResp resp;
req.SetTagSet(tagset);
qcloud_cos::CosResult result = cos.PutBucketTagging(req, &resp);
std::cout << "===================PutBucketTagging=====================" << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
//用于查询指定存储桶下已有的存储桶标签
void GetBucketTagging(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
qcloud_cos::GetBucketTaggingReq req(bucket_name);
qcloud_cos::GetBucketTaggingResp resp;
qcloud_cos::CosResult result = cos.GetBucketTagging(req, &resp);
std::cout << "===================GetBucketTagging=====================" << 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 DeleteBucketTagging(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
qcloud_cos::DeleteBucketTaggingReq req(bucket_name);
qcloud_cos::DeleteBucketTaggingResp resp;
qcloud_cos::CosResult result = cos.DeleteBucketTagging(req, &resp);
std::cout << "===================DeleteBucketTagging=====================" << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
// 在存储桶中创建清单任务
void PutBucketInventory(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
qcloud_cos::PutBucketInventoryReq req(bucket_name);
//req.SetId("list3");
//COSBucketDestination destination;
//destination.SetFormat("CSV");
//destination.SetAccountId("100010974959");
//destination.SetBucket("qcs::cos:ap-guangzhou::loggtest-1234567890");
//destination.SetPrefix("/");
//destination.SetEncryption(true);
//OptionalFields fields;
// fields.SetIsSize(true);
// fields.SetIsLastModified(true);
// fields.SetIsStorageClass(true);
// fields.SetIsMultipartUploaded(true);
// fields.SetIsReplicationStatus(true);
// fields.SetIsEtag(true);
//
// Inventory inventory;
// inventory.SetIsEnable(true);
// inventory.SetIncludedObjectVersions("All");
// inventory.SetFilter("/");
// inventory.SetId(req.GetId());
// inventory.SetFrequency("Daily");
// inventory.SetCOSBucketDestination(destination);
// inventory.SetOptionalFields(fields);
//
// req.SetInventory(inventory);
qcloud_cos::PutBucketInventoryResp resp;
qcloud_cos::CosResult result = cos.PutBucketInventory(req, &resp);
std::cout << "===================PutBucketinventory=====================" << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
// 用于查询存储桶中用户的清单任务信息
void GetBucketInventory(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
qcloud_cos::GetBucketInventoryReq req(bucket_name);
qcloud_cos::GetBucketInventoryResp resp;
req.SetId("list2");
qcloud_cos::CosResult result = cos.GetBucketInventory(req, &resp);
//std::cout << "===================GetBucketinventory=====================" << std::endl;
//
//const Inventory inventory = resp.GetInventory();
//std::cout << "inventory isenabled:" << inventory.GetIsEnable() << std::endl;
//std::cout << "inventory IncludedObjectVersions:" << inventory.GetIncludedObjectVersions() << std::endl;
//std::cout << "inventory filter:" << inventory.GetFilter() << std::endl;
//std::cout << "inventory id:" << inventory.GetId() << std::endl;
//std::cout << "inventory Frequency:" << inventory.GetFrequency() << std::endl;
//std:: cout << "===================================" << std::endl;
//COSBucketDestination destination = inventory.GetCOSBucketDestination();
//std::cout << "destination Format:" << destination.GetFormat() << std::endl;
//std::cout << "destination AccountID:" << destination.GetAccountId() << std::endl;
//std::cout << "destination Bucket:" << destination.GetBucket() << std::endl;
//std::cout << "destination Encryption:" << destination.GetEncryption() << std::endl;
//std:: cout << "===================================" << std::endl;
//
//OptionalFields fields = inventory.GetOptionalFields();
//std::cout << "fields Size:" << fields.GetIsSize() << std::endl;
//std::cout << "fields LastModified:" << fields.GetIsLastModified() << std::endl;
//std::cout << "fields StorageClass:" << fields.GetIsStorageClass() << std::endl;
//std::cout << "fields Region:" << fields.GetIsMultipartUploaded() << std::endl;
//std::cout << "fields ReplicationStatus:" << fields.GetIsReplicationStatus() << std::endl;
//std::cout << "fields Tag:" << fields.GetIsETag() << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
// 请求返回一个存储桶中的所有清单任务
void ListBucketInventoryConfigurations(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
qcloud_cos::ListBucketInventoryConfigurationsReq req(bucket_name);
qcloud_cos::ListBucketInventoryConfigurationsResp resp;
qcloud_cos::CosResult result = cos.ListBucketInventoryConfigurations(req, &resp);
// std::cout << "===================ListBucketInventoryConfigurations=====================" << std::endl;
//
// std::vector<Inventory> inventory_vec = resp.GetInventory();
//
// std::cout << resp.GetIsTruncated() << std::endl;
// std::cout << resp.GetContinuationToken() << std::endl;
// std::cout << resp.GetNextContinuationToken() << std::endl;
//
// std::vector<Inventory>::iterator itr = inventory_vec.begin();
// for(; itr != inventory_vec.end(); ++itr) {
//
// std:: cout << "==============Inventory=============================" << std::endl;
// std::cout << "inventory id:" << itr->GetId() << std::endl;
// std::cout << "inventory isenabled:" << itr->GetIsEnable() << std::endl;
// std::cout << "inventory IncludedObjectVersions:" << itr->GetIncludedObjectVersions() << std::endl;
// std::cout << "inventory filter:" << itr->GetFilter() << std::endl;
// std::cout << "inventory Frequency:" << itr->GetFrequency() << std::endl;
// std:: cout << "==============GetCOSBucketDestination==================" << std::endl;
// COSBucketDestination destination = itr->GetCOSBucketDestination();
// std::cout << "destination Format:" << destination.GetFormat() << std::endl;
// std::cout << "destination AccountID:" << destination.GetAccountId() << std::endl;
// std::cout << "destination Bucket:" << destination.GetBucket() << std::endl;
// std::cout << "destination Encryption:" << destination.GetEncryption() << std::endl;
//
//
// std:: cout << "===================OptionalFields======================" << std::endl;
// OptionalFields fields = itr->GetOptionalFields();
// std::cout << "fields Size:" << fields.GetIsSize() << std::endl;
// std::cout << "fields LastModified:" << fields.GetIsLastModified() << std::endl;
// std::cout << "fields StorageClass:" << fields.GetIsStorageClass() << std::endl;
// std::cout << "fields Region:" << fields.GetIsMultipartUploaded() << std::endl;
// std::cout << "fields ReplicationStatus:" << fields.GetIsReplicationStatus() << std::endl;
// std::cout << "fields Tag:" << fields.GetIsETag() << std::endl;
// }
// std::cout << "===============ListBucketInventoryConfigurations end================" << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
// 用于删除存储桶中指定的清单任务.
void DeleteBucketInventory(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
qcloud_cos::DeleteBucketInventoryReq req(bucket_name);
qcloud_cos::DeleteBucketInventoryResp resp;
req.SetId("list");
qcloud_cos::CosResult result = cos.DeleteBucketInventory(req, &resp);
std::cout << "===================DeleteBucketinventory=====================" << std::endl;
PrintResult(result, resp);
std::cout << "====================================================================" << std::endl;
}
int main(int argc, char** argv) {
//v5版可以重复上传同名文件,不报错,会覆盖,v4的报错
uint64_t appid = 1256237915;
const std::string access_key = "AKIDdDYdYo";
const std::string secret_key = "CAwSlskaBW";
const std::string region = "ap-beijing";
//设置签名超时时间,单位:秒,7天
uint64_t expired_time_s = 60 * 60 * 24 * 7;
//设置连接超时时间,单位:毫秒
uint64_t conn_time_ms = 6000;
// qcloud_cos::CosConfig config("./config.json");
// qcloud_cos::CosConfig config;
// config.SetAccessKey(access_key);
// config.SetSecretKey(secret_key);
// config.SetRegion(region);
qcloud_cos::CosConfig config(appid, access_key, secret_key, region);
//设置签名超时时间,单位:秒,默认是60s
CosSysConfig::SetAuthExpiredTime(expired_time_s);
CosSysConfig::SetConnTimeoutInms(conn_time_ms);
// 日志级别,1:ERR,2:WARN,3:INFO,4:DBG
CosSysConfig::SetLogLevel((LOG_LEVEL)(3));
// 日志输出类型,0:不输出,1:输出到屏幕,2:输出到 syslog
CosSysConfig::SetLogOutType((LOG_OUT_TYPE)(0));
//设置文件分片并发上传线程池大小,默认: 5
CosSysConfig::SetUploadThreadPoolSize(30);
qcloud_cos::CosAPI cos(config);
// std::string bucket_name = "test1-1234567890";
//注意bucket在v4版本中不能带后面的数字,否则有一定几率上传不上去
std::string bucket_name = "private-deploy-package-1256237915";
//cos目录在v4版本中必须前面加/,v5版本没有这个要求,加不加都可以.但前加/会形成2个//形式的签名链接,可用但不规范
std::string object_name = "test/data";
std::string local_file = "/data/data.tar.gz";
std::string download_file_path = "./tmp/";
//一复合上传接口
//void MultiUploadObject(qcloud_cos::CosAPI & cos, const std::string &bucket_name, const std::string &object_name, const std::string &local_file)
MultiUploadObject(cos, bucket_name, object_name, local_file);
qcloud_cos::GeneratePresignedUrlReq req(bucket_name, object_name, qcloud_cos::HTTP_GET);
std::string presigned_url = cos.GeneratePresignedUrl(req);
std::cout << "presigned_url = " << presigned_url << std::endl;
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。