60 lines
2.0 KiB
C++
60 lines
2.0 KiB
C++
#include "sgu_stdafx.h"
|
|
#include "sgu_download_cos.h"
|
|
|
|
#if 1
|
|
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";
|
|
|
|
bool downloadFileFromCOS(const char* cosFilePathName, const char* localFilePath)
|
|
{
|
|
qcloud_cos::CosConfig config(gAppID, 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;
|
|
}
|
|
#endif |