tencent_cos.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "tencent_cos.h"
  2. #include "dll_interface.h"
  3. #include "cos_api.h"
  4. #include "cos_http_io.h"
  5. #include "cos_log.h"
  6. static int inited = 0;
  7. extern "C" {
  8. #ifdef _WIN32
  9. DLL_EXPORT
  10. #endif
  11. int fastweb_module_regist(void* sol2, void* lua)
  12. {
  13. sol::state* state = static_cast<sol::state*>(sol2);
  14. module::tencent_cos::regist(state);
  15. return 0;
  16. }
  17. }
  18. module::tencent_cos::tencent_cos()
  19. {
  20. if(inited == 0){
  21. if (cos_http_io_initialize(NULL, 0) != COSE_OK) {
  22. printf("COS HTTP INIT FAILED");
  23. return;
  24. }
  25. inited = 1;
  26. }
  27. }
  28. module::tencent_cos::~tencent_cos()
  29. {
  30. // cos_http_io_deinitialize();
  31. }
  32. 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)
  33. {
  34. int enable_checkpoint = COS_FALSE; // 是否开启断点续传
  35. cos_pool_t* p = NULL;
  36. cos_string_t bucket;
  37. cos_string_t object;
  38. cos_string_t filename;
  39. cos_status_t* s = NULL;
  40. cos_table_t* headers = NULL;
  41. cos_table_t* resp_headers = NULL;
  42. cos_request_options_t* options = NULL;
  43. cos_resumable_clt_params_t* clt_params;
  44. cos_pool_create(&p, NULL);
  45. options = cos_request_options_create(p);
  46. options->config = cos_config_create(options->pool);
  47. cos_str_set(&options->config->endpoint, endpoint.c_str());
  48. cos_str_set(&options->config->access_key_id, access_key_id.c_str());
  49. cos_str_set(&options->config->access_key_secret, access_key_secret.c_str());
  50. cos_str_set(&options->config->appid, appid.c_str());
  51. // cos_str_set(&config->sts_token, token); // 使用临时密钥时的 token
  52. options->config->is_cname = COS_FALSE; // 是否使用自定义域名
  53. options->ctl = cos_http_controller_create(options->pool, 0);
  54. headers = cos_table_make(p, 0);
  55. cos_str_set(&bucket, bucket_name.c_str());
  56. cos_str_set(&object, object_name.c_str());
  57. cos_str_set(&filename, filepath.c_str());
  58. // upload
  59. clt_params = cos_create_resumable_clt_params_content(p, 1024 * 1024 * 5, 8, enable_checkpoint, NULL);
  60. s = cos_resumable_upload_file(options, &bucket, &object, &filename, headers, NULL,
  61. clt_params, NULL, &resp_headers, NULL);
  62. std::string result = "";
  63. if (cos_status_is_ok(s)) {
  64. result = "";
  65. }
  66. else
  67. {
  68. result = "code:" + std::to_string(s->code)+", error_code:"+s->error_code+", error_msg:"+s->error_msg;
  69. }
  70. cos_pool_destroy(p);
  71. return result;
  72. }
  73. 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)
  74. {
  75. int enable_checkpoint = COS_FALSE; // 是否开启断点续传
  76. cos_pool_t* p = NULL;
  77. cos_string_t bucket;
  78. cos_string_t object;
  79. cos_string_t filename;
  80. cos_status_t* s = NULL;
  81. cos_table_t* headers = NULL;
  82. cos_table_t* resp_headers = NULL;
  83. cos_request_options_t* options = NULL;
  84. cos_resumable_clt_params_t* clt_params;
  85. cos_pool_create(&p, NULL);
  86. options = cos_request_options_create(p);
  87. options->config = cos_config_create(options->pool);
  88. cos_str_set(&options->config->endpoint, endpoint.c_str());
  89. cos_str_set(&options->config->access_key_id, access_key_id.c_str());
  90. cos_str_set(&options->config->access_key_secret, access_key_secret.c_str());
  91. cos_str_set(&options->config->appid, appid.c_str());
  92. // cos_str_set(&config->sts_token, token); // 使用临时密钥时的 token
  93. options->config->is_cname = COS_FALSE; // 是否使用自定义域名
  94. options->ctl = cos_http_controller_create(options->pool, 0);
  95. headers = cos_table_make(p, 0);
  96. cos_str_set(&bucket, bucket_name.c_str());
  97. cos_str_set(&object, object_name.c_str());
  98. s = cos_delete_object(options, &bucket, &object, &resp_headers);
  99. std::string result = "";
  100. if (cos_status_is_ok(s)) {
  101. result = "";
  102. }
  103. else
  104. {
  105. result = "code:" + std::to_string(s->code)+", error_code:"+s->error_code+", error_msg:"+s->error_msg;
  106. }
  107. cos_pool_destroy(p);
  108. return result;
  109. }
  110. void module::tencent_cos::regist(sol::state* lua)
  111. {
  112. lua->new_usertype<module::tencent_cos>("fw_tencent_cos",
  113. "new", sol::constructors<module::tencent_cos()>(),
  114. "upfile", &module::tencent_cos::upfile,
  115. "del", &module::tencent_cos::del
  116. );
  117. }
  118. void module::tencent_cos::regist_global(const char* name, sol::state* lua)
  119. {
  120. lua->registry()[name] = this;
  121. (*lua)[name] = this;
  122. }