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