tencent_cos.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. void module::tencent_cos::regist(sol::state* lua)
  74. {
  75. lua->new_usertype<module::tencent_cos>("fw_tencent_cos",
  76. "new", sol::constructors<module::tencent_cos()>(),
  77. "upfile", &module::tencent_cos::upfile
  78. );
  79. }
  80. void module::tencent_cos::regist_global(const char* name, sol::state* lua)
  81. {
  82. lua->registry()[name] = this;
  83. (*lua)[name] = this;
  84. }