| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- #include "tencent_cos.h"
- #include "dll_interface.h"
- #include "cos_api.h"
- #include "cos_http_io.h"
- #include "cos_log.h"
- static int inited = 0;
- extern "C" {
- #ifdef _WIN32
- DLL_EXPORT
- #endif
- int fastweb_module_regist(void* sol2, void* lua)
- {
- sol::state* state = static_cast<sol::state*>(sol2);
- module::tencent_cos::regist(state);
- return 0;
- }
- }
- module::tencent_cos::tencent_cos()
- {
- if(inited == 0){
- if (cos_http_io_initialize(NULL, 0) != COSE_OK) {
- printf("COS HTTP INIT FAILED");
- return;
- }
- inited = 1;
- }
- }
- module::tencent_cos::~tencent_cos()
- {
- // cos_http_io_deinitialize();
- }
- std::string module::tencent_cos::upfile(const std::string& appid,const std::string& endpoint,const std::string& access_key_id,const std::string& access_key_secret,const std::string& bucket_name,const std::string& object_name,const std::string& filepath)
- {
- int enable_checkpoint = COS_FALSE; // 是否开启断点续传
- cos_pool_t* p = NULL;
- cos_string_t bucket;
- cos_string_t object;
- cos_string_t filename;
- cos_status_t* s = NULL;
- cos_table_t* headers = NULL;
- cos_table_t* resp_headers = NULL;
- cos_request_options_t* options = NULL;
- cos_resumable_clt_params_t* clt_params;
- cos_pool_create(&p, NULL);
-
- options = cos_request_options_create(p);
- options->config = cos_config_create(options->pool);
- cos_str_set(&options->config->endpoint, endpoint.c_str());
- cos_str_set(&options->config->access_key_id, access_key_id.c_str());
- cos_str_set(&options->config->access_key_secret, access_key_secret.c_str());
- cos_str_set(&options->config->appid, appid.c_str());
- // cos_str_set(&config->sts_token, token); // 使用临时密钥时的 token
- options->config->is_cname = COS_FALSE; // 是否使用自定义域名
- options->ctl = cos_http_controller_create(options->pool, 0);
- headers = cos_table_make(p, 0);
- cos_str_set(&bucket, bucket_name.c_str());
- cos_str_set(&object, object_name.c_str());
- cos_str_set(&filename, filepath.c_str());
- // upload
- clt_params = cos_create_resumable_clt_params_content(p, 1024 * 1024 * 5, 8, enable_checkpoint, NULL);
- s = cos_resumable_upload_file(options, &bucket, &object, &filename, headers, NULL,
- clt_params, NULL, &resp_headers, NULL);
-
- std::string result = "";
- if (cos_status_is_ok(s)) {
- result = "";
- }
- else
- {
- result = "code:" + std::to_string(s->code)+", error_code:"+s->error_code+", error_msg:"+s->error_msg;
- }
- cos_pool_destroy(p);
- return result;
- }
- std::string module::tencent_cos::del(const std::string& appid,const std::string& endpoint,const std::string& access_key_id,const std::string& access_key_secret,const std::string& bucket_name,const std::string& object_name)
- {
- int enable_checkpoint = COS_FALSE; // 是否开启断点续传
- cos_pool_t* p = NULL;
- cos_string_t bucket;
- cos_string_t object;
- cos_string_t filename;
- cos_status_t* s = NULL;
- cos_table_t* headers = NULL;
- cos_table_t* resp_headers = NULL;
- cos_request_options_t* options = NULL;
- cos_resumable_clt_params_t* clt_params;
- cos_pool_create(&p, NULL);
-
- options = cos_request_options_create(p);
- options->config = cos_config_create(options->pool);
- cos_str_set(&options->config->endpoint, endpoint.c_str());
- cos_str_set(&options->config->access_key_id, access_key_id.c_str());
- cos_str_set(&options->config->access_key_secret, access_key_secret.c_str());
- cos_str_set(&options->config->appid, appid.c_str());
- // cos_str_set(&config->sts_token, token); // 使用临时密钥时的 token
- options->config->is_cname = COS_FALSE; // 是否使用自定义域名
- options->ctl = cos_http_controller_create(options->pool, 0);
- headers = cos_table_make(p, 0);
- cos_str_set(&bucket, bucket_name.c_str());
- cos_str_set(&object, object_name.c_str());
- s = cos_delete_object(options, &bucket, &object, &resp_headers);
-
- std::string result = "";
- if (cos_status_is_ok(s)) {
- result = "";
- }
- else
- {
- result = "code:" + std::to_string(s->code)+", error_code:"+s->error_code+", error_msg:"+s->error_msg;
- }
- cos_pool_destroy(p);
- return result;
- }
- void module::tencent_cos::regist(sol::state* lua)
- {
- lua->new_usertype<module::tencent_cos>("fw_tencent_cos",
- "new", sol::constructors<module::tencent_cos()>(),
- "upfile", &module::tencent_cos::upfile,
- "del", &module::tencent_cos::del
- );
- }
- void module::tencent_cos::regist_global(const char* name, sol::state* lua)
- {
- lua->registry()[name] = this;
- (*lua)[name] = this;
- }
|