97 lines
3.3 KiB
C++
97 lines
3.3 KiB
C++
#include "sgu_stdafx.h"
|
|
#include "sgu_download_cos.h"
|
|
|
|
#if 0
|
|
bool downloadFileFromCOS(const char* cosFilePathName, const char* localFilePath)
|
|
{
|
|
return true;
|
|
}
|
|
#else
|
|
#include "cos_api.h"
|
|
#include "cos_sys_config.h"
|
|
#include "cos_defines.h"
|
|
|
|
static void TestLogCallback(const std::string& log)
|
|
{
|
|
//printf("ERR:%s\n", log.c_str());
|
|
}
|
|
|
|
//
|
|
// https://console.cloud.tencent.com/cam/capi
|
|
//
|
|
//const uint64_t gAppID = 1313389434ULL;
|
|
//const std::string gSecretId = "AKIDGMFKI2av3g6JPTKzRffS1NWR9eXDgwym";
|
|
//const std::string gSecretKey = "f7LMDtNdnlXDpEQRGxVqxrbBYjlcbP2U";
|
|
//const std::string gRegion = "ap-shanghai";
|
|
//const std::string gBucketName = "dnftest-1313389434";
|
|
|
|
|
|
const std::string gSecretId = "AKIDfBzQo25vgJl170rV1LRNLExTp27GZhho";
|
|
const std::string gSecretKey = "2qvqX0FKCrHkBQehYDe2JaggePA4nLir";
|
|
const std::string gRegion = "ap-nanjing";
|
|
const std::string gBucketName = "client-fwf-1258765125";
|
|
|
|
bool downloadFileFromCOS(const char* cosFilePathName, const char* localFilePath)
|
|
{
|
|
qcloud_cos::CosConfig config(0, gSecretId, gSecretKey, gRegion);
|
|
config.SetLogCallback(&TestLogCallback);
|
|
qcloud_cos::CosAPI cos(config);
|
|
|
|
// 2. 构造下载对象的请求
|
|
std::string objectName = cosFilePathName;
|
|
std::string localPath = localFilePath;
|
|
// request 需要提供 appid、bucketname、object,以及本地的路径(包含文件名)
|
|
qcloud_cos::GetObjectByFileReq req(gBucketName, objectName, localPath);
|
|
qcloud_cos::GetObjectByFileResp resp;
|
|
|
|
// 3. 调用下载对象接口
|
|
qcloud_cos::CosResult result = cos.GetObject(req, &resp);
|
|
|
|
// 4. 处理调用结果
|
|
if(result.IsSucc()) {
|
|
// 下载文件成功
|
|
std::cout << "Download file success!" << std::endl;
|
|
}
|
|
else {
|
|
// 下载文件失败,可以调用 CosResult 的成员函数输出错误信息,例如 requestID 等
|
|
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;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool uploadFileToCos(const char* localFilePath, const char* cosFilePath)
|
|
{
|
|
qcloud_cos::CosConfig config(0, gSecretId, gSecretKey, gRegion);
|
|
config.SetLogCallback(&TestLogCallback);
|
|
qcloud_cos::CosAPI cos(config);
|
|
|
|
// 构造下载对象的请求
|
|
std::string objectName = cosFilePath;
|
|
std::string localPath = localFilePath;
|
|
|
|
// 上传对象构造
|
|
qcloud_cos::PutObjectByFileReq req(gBucketName, objectName, localPath);
|
|
qcloud_cos::PutObjectByFileResp resp;
|
|
qcloud_cos::CosResult result = cos.PutObject(req, &resp);
|
|
if (result.IsSucc()) {
|
|
// 上传文件成功
|
|
std::cout << "Upload file success!" << std::endl;
|
|
}
|
|
else {
|
|
// 上传文件失败,可以调用 CosResult 的成员函数输出错误信息,例如 requestID 等
|
|
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;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
#endif |