Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload part break points, callback module and the first version of windows compatible. #25

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion demo/cos_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Description:

#include <iostream>
#include <stdlib.h>
#include <map>
#include <string>
#include <vector>
Expand All @@ -15,6 +16,8 @@
#include "cos_sys_config.h"
#include "cos_defines.h"

#include "Poco/SharedPtr.h"

using namespace qcloud_cos;
void PrintResult(const qcloud_cos::CosResult& result, const qcloud_cos::BaseResp& resp) {
if (result.IsSucc()) {
Expand Down Expand Up @@ -431,6 +434,7 @@ void CompleteMultiUpload(qcloud_cos::CosAPI& cos, const std::string& bucket_name
std::cout << "========================================================" << std::endl;
}

// Upload object without handler
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,
Expand Down Expand Up @@ -462,6 +466,52 @@ void MultiUploadObject(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
std::cout << "========================================================" << std::endl;
}

void uploadprogress(const MultiUploadObjectReq *req, Poco::SharedPtr<TransferHandler> &handler) {
std::cout << "callback data is :" << handler->GetProgress() << std::endl;
}

void statusprogress(const MultiUploadObjectReq *req, Poco::SharedPtr<TransferHandler> &handler) {
std::cout << "callback status is :" << handler->GetStatusString() << std::endl;
}

// Upload object with handler
void TransferUploadObject(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);
req.SetRecvTimeoutInms(1000 * 60);
req.SetUploadProgressCallback(uploadprogress);
req.SetTransferStatusUpdateCallback(statusprogress);
qcloud_cos::MultiUploadObjectResp resp;
Poco::SharedPtr<TransferHandler> handler = cos.TransferUploadObject(req, &resp);
// The TransferUploadObject is the asynchronization api, can use WaitUntilFinish to block until finish.
// At the same time the handler support the GetTotalSize(), GetProgress(), GetStatus(), Cancel() etc.
handler->WaitUntilFinish();

// Notice when not block with the WaitUntilFinish() the result might not get soon.
if (handler->m_result.IsSucc()) {
std::cout << "MultiUpload Succ." << std::endl;
std::cout << resp.GetLocation() << std::endl;
std::cout << resp.GetKey() << std::endl;
std::cout << resp.GetBucket() << std::endl;
std::cout << resp.GetEtag() << std::endl;
} else {
std::cout << "MultiUpload Fail." << std::endl;
// 获取具体失败在哪一步
std::string resp_tag = resp.GetRespTag();
if ("Init" == resp_tag) {
// print result
} else if ("Upload" == resp_tag) {
// print result
} else if ("Complete" == resp_tag) {
// print result
}
}
std::cout << "===================MultiUpload=============================" << std::endl;
PrintResult(handler->m_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);
Expand Down Expand Up @@ -654,6 +704,8 @@ int main(int argc, char** argv) {
// GetObjectByFile(cos, bucket_name, "sevenyou_e2_abc", "/data/sevenyou/temp/sevenyou_10m_download_03");
//GetObjectByStream(cos, bucket_name, "sevenyou_e2_abc");
// MultiGetObject(cos, bucket_name, "sevenyou_1102_south_multi", "/data/sevenyou/temp/sevenyou_10m_download_03");
// MultiGetObject(cos, bucket_name, "test000part", "./multiget");
// TransferUploadObject(cos, bucket_name, "transfer", "./test6M1");

// {
// std::string upload_id;
Expand Down Expand Up @@ -797,5 +849,9 @@ int main(int argc, char** argv) {
// PrintResult(result, resp);
// std::cout << "=========================================================" << std::endl;
// }

#if defined(_WIN32)
system("pause");
#endif
}


51 changes: 48 additions & 3 deletions include/cos_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
#include "op/cos_result.h"
#include "op/object_op.h"
#include "op/service_op.h"
#include "util/simple_mutex.h"

#include "boost/thread/mutex.hpp"
#include "Poco/SharedPtr.h"
#include "trsf/transfer_handler.h"

namespace qcloud_cos {

Expand Down Expand Up @@ -65,6 +67,7 @@ class CosAPI {
/// \return 本次请求的调用情况(如状态码等)
CosResult PutBucket(const PutBucketReq& request, PutBucketResp* response);


/// \brief 确认Bucket是否存在
/// (详见:https://cloud.tencent.com/document/product/436/7735)
///
Expand Down Expand Up @@ -331,7 +334,14 @@ class CosAPI {
///
/// \return 返回HTTP请求的状态码及错误信息
CosResult MultiUploadObject(const MultiUploadObjectReq& request,
MultiUploadObjectResp* response);
MultiUploadObjectResp* response) ;


Poco::SharedPtr<TransferHandler> TransferUploadObject(const MultiUploadObjectReq& request,
MultiUploadObjectResp* response) ;

Poco::SharedPtr<TransferHandler> CreateUploadHandler(const std::string& bucket_name, const std::string& object_name,
const std::string& local_path) ;

/// \brief 舍弃一个分块上传并删除已上传的块
/// 详见: https://www.qcloud.com/document/product/436/7740
Expand Down Expand Up @@ -416,11 +426,46 @@ class CosAPI {
BucketOp m_bucket_op; // 内部封装bucket相关的操作
ServiceOp m_service_op; // 内部封装service相关的操作

static SimpleMutex s_init_mutex;
mutable boost::mutex s_init_mutex;
static bool s_init;
static bool s_poco_init;
static int s_cos_obj_num;
};

// Use for trsf the param into boost bind function
class AsynArgs {
public:
AsynArgs(ObjectOp* op) : m_op(op) {}
AsynArgs(const AsynArgs& arg) {
this->m_op = arg.m_op;
}
virtual ~AsynArgs() {};
ObjectOp* m_op;
};


class TransferAsynArgs : public AsynArgs {
public:
TransferAsynArgs(ObjectOp* pObj,
const MultiUploadObjectReq& req,
MultiUploadObjectResp *resp,
Poco::SharedPtr<TransferHandler>& handler) : AsynArgs(pObj) , m_req(req), m_resp(resp) {
m_handler = handler;

}

TransferAsynArgs(const TransferAsynArgs& arg)
: AsynArgs(arg),
m_req(arg.m_req),
m_handler(arg.m_handler),
m_resp(arg.m_resp) {
}
virtual ~TransferAsynArgs() {}
public:
MultiUploadObjectReq m_req;
Poco::SharedPtr<TransferHandler> m_handler;
MultiUploadObjectResp* m_resp;
};

} // namespace qcloud_cos
#endif
7 changes: 4 additions & 3 deletions include/cos_config.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#ifndef COS_CONFIG_H
#define COS_CONFIG_H

#include <string>
#include <stdint.h>

#include <string>
#include "util/simple_mutex.h"
#include "boost/thread.hpp"
#include "boost/thread/shared_mutex.hpp"

namespace qcloud_cos{
class CosConfig{
Expand Down Expand Up @@ -116,7 +117,7 @@ class CosConfig{
void SetConfigCredentail(const std::string& access_key, const std::string& secret_key, const std::string& tmp_token);

private:
mutable SimpleRWLock m_lock;
mutable boost::shared_mutex m_lock;
uint64_t m_app_id;
std::string m_access_key;
std::string m_secret_key;
Expand Down
36 changes: 28 additions & 8 deletions include/cos_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
#define COS_DEFINE_H
#include <stdint.h>
#include <stdio.h>

#if !defined(_WIN32)
#include <syslog.h>
#endif

#include <vector>
#include <string>
Expand All @@ -27,6 +30,8 @@ const int kMaxThreadPoolSizeUploadPart = 100;
/// 分块上传的线程池最小数目
const int kMinThreadPoolSizeUploadPart = 1;

const int kMaxPartNumbers = 10000;

/// 分块大小1M
const uint64_t kPartSize1M = 1 * 1024 * 1024;
/// 分块大小5G
Expand Down Expand Up @@ -60,23 +65,38 @@ typedef enum cos_log_level {
(level == COS_LOG_WARN) ? "[WARN] " : \
(level == COS_LOG_ERR) ? "[ERR] " : "[CRIT]")


#if defined(_WIN32)
#define COS_LOW_LOGPRN(level, fmt, ...) \
if (level <= CosSysConfig::GetLogLevel()) { \
if (CosSysConfig::GetLogOutType()== COS_LOG_STDOUT) { \
fprintf(stdout,"%s:%s(%d) " fmt "%s\n", LOG_LEVEL_STRING(level),__func__,__LINE__, __VA_ARGS__); \
fprintf(stdout,"%s:%s(%d) " fmt "\n", LOG_LEVEL_STRING(level),__func__,__LINE__, ##__VA_ARGS__); \
}else if (CosSysConfig::GetLogOutType() == COS_LOG_SYSLOG){ \
syslog(LOG_INFO,"%s:%s(%d) " fmt "%s\n", LOG_LEVEL_STRING(level),__func__,__LINE__, __VA_ARGS__); \
} else { \
} \
} else { \
} \
}
#else
#define COS_LOW_LOGPRN(level, fmt, ...) \
if (level <= CosSysConfig::GetLogLevel()) { \
if (CosSysConfig::GetLogOutType()== COS_LOG_STDOUT) { \
fprintf(stdout,"%s:%s(%d) " fmt "\n", LOG_LEVEL_STRING(level),__func__,__LINE__, ##__VA_ARGS__); \
}else if (CosSysConfig::GetLogOutType() == COS_LOG_SYSLOG){ \
syslog(LOG_INFO,"%s:%s(%d) " fmt "\n", LOG_LEVEL_STRING(level),__func__,__LINE__, ##__VA_ARGS__); \
} else { \
} \
} else { \
}
#endif



#define SDK_LOG_DBG(fmt, ...) COS_LOW_LOGPRN(COS_LOG_DBG, fmt, ##__VA_ARGS__, "")
#define SDK_LOG_INFO(fmt, ...) COS_LOW_LOGPRN(COS_LOG_INFO, fmt, ##__VA_ARGS__, "")
#define SDK_LOG_WARN(fmt, ...) COS_LOW_LOGPRN(COS_LOG_WARN, fmt, ##__VA_ARGS__, "")
#define SDK_LOG_ERR(fmt, ...) COS_LOW_LOGPRN(COS_LOG_ERR, fmt, ##__VA_ARGS__, "")
#define SDK_LOG_COS(level, fmt, ...) COS_LOW_LOGPRN(level, fmt, ##__VA_ARGS__, "")
// For now just support the std output log for windows
#define SDK_LOG_DBG(fmt, ...) COS_LOW_LOGPRN(COS_LOG_DBG, fmt, ##__VA_ARGS__)
#define SDK_LOG_INFO(fmt, ...) COS_LOW_LOGPRN(COS_LOG_INFO, fmt, ##__VA_ARGS__)
#define SDK_LOG_WARN(fmt, ...) COS_LOW_LOGPRN(COS_LOG_WARN, fmt, ##__VA_ARGS__)
#define SDK_LOG_ERR(fmt, ...) COS_LOW_LOGPRN(COS_LOG_ERR, fmt, ##__VA_ARGS__)
#define SDK_LOG_COS(level, fmt, ...) COS_LOW_LOGPRN(level, fmt, ##__VA_ARGS__)

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
Expand Down
2 changes: 1 addition & 1 deletion include/cos_sys_config.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#ifndef COS_SYS_CONF_H
#define COS_SYS_CONF_H
#include <pthread.h>
#include <stdint.h>

#include "cos_defines.h"
Expand Down Expand Up @@ -139,6 +138,7 @@ class CosSysConfig {
static bool m_is_check_md5;

static std::string m_dest_domain;

};

} // namespace qcloud_cos
Expand Down
2 changes: 0 additions & 2 deletions include/op/file_copy_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#define FILE_COPY_TASK_H
#pragma once

#include <pthread.h>

#include <string>

#include "cos_config.h"
Expand Down
5 changes: 2 additions & 3 deletions include/op/file_download_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#define FILE_DOWN_TASK_H
#pragma once

#include <pthread.h>

#include <string>

#include "cos_config.h"
Expand Down Expand Up @@ -42,7 +40,7 @@ class FileDownTask {

void DownTask();

void SetDownParams(unsigned char* pdatabuf, size_t datalen, uint64_t offset);
void SetDownParams(unsigned char* pdatabuf, size_t datalen, uint64_t offset, uint64_t target_size);

std::string GetTaskResp();

Expand Down Expand Up @@ -71,6 +69,7 @@ class FileDownTask {
int m_http_status;
std::map<std::string, std::string> m_resp_headers;
std::string m_err_msg;
uint64_t m_target_size;
};

} // namespace qcloud_cos
Expand Down
27 changes: 25 additions & 2 deletions include/op/file_upload_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@
#define FILE_UPLOAD_TASK_H
#pragma once

#include <pthread.h>

#include <string>

#include "cos_config.h"
#include "cos_defines.h"
#include "cos_params.h"
#include "cos_sys_config.h"
#include "op/base_op.h"
#include "request/object_req.h"
#include "util/codec_util.h"
#include "util/file_util.h"
#include "util/http_sender.h"
#include "util/string_util.h"

#include "Poco/SharedPtr.h"
#include "trsf/transfer_handler.h"

namespace qcloud_cos{

class FileUploadTask {
Expand Down Expand Up @@ -46,6 +48,8 @@ class FileUploadTask {

bool IsTaskSuccess() const;

void SetTaskSuccess() { m_is_task_success = true; }

int GetHttpStatus() const ;

std::map<std::string, std::string> GetRespHeaders() const;
Expand All @@ -56,6 +60,22 @@ class FileUploadTask {

std::string GetErrMsg() const { return m_err_msg; }

void SetResume(const bool is_resume) { m_is_resume = is_resume; }

bool IsResume() const { return m_is_resume; }

void SetHandler(const bool is_handler) { m_is_handler = is_handler; }

bool IsHandler() const { return m_is_handler; }

void SetResumeEtag(const std::string& etag) { m_resume_etag = etag; }

std::string GetResumeEtag() const { return m_resume_etag; }

public:
Poco::SharedPtr<TransferHandler> m_handler;
MultiUploadObjectReq *m_req;

private:
std::string m_full_url;
std::map<std::string, std::string> m_headers;
Expand All @@ -69,6 +89,9 @@ class FileUploadTask {
int m_http_status;
std::map<std::string, std::string> m_resp_headers;
std::string m_err_msg;
bool m_is_resume;
std::string m_resume_etag;
bool m_is_handler;
};

}
Expand Down
Loading