aliyunoss.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include "aliyunoss.h"
  2. #include "dll_interface.h"
  3. #include "aos_log.h"
  4. #include "aos_util.h"
  5. #include "aos_string.h"
  6. #include "aos_status.h"
  7. #include "oss_auth.h"
  8. #include "oss_util.h"
  9. #include "oss_api.h"
  10. #include <oss_c_sdk/aos_status.h>
  11. static int inited = 0;
  12. module::aliyun_oss::aliyun_oss()
  13. {
  14. if(inited == 0){
  15. if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
  16. printf("COS HTTP aos_http_io_initialize FAILED");
  17. return;
  18. }
  19. inited = 1;
  20. }
  21. }
  22. module::aliyun_oss::~aliyun_oss()
  23. {
  24. //aos_http_io_deinitialize();
  25. }
  26. std::string module::aliyun_oss::updata(
  27. const std::string& endpoint,
  28. const std::string& access_key_id,
  29. const std::string& access_key_secret,
  30. const std::string& bucket_name,
  31. const std::string& object_name,
  32. const std::string_view& data
  33. )
  34. {
  35. aos_pool_t* p = NULL;
  36. aos_string_t bucket;
  37. aos_string_t object;
  38. int is_cname = 0;
  39. aos_table_t* headers = NULL;
  40. aos_table_t* resp_headers = NULL;
  41. oss_request_options_t* options = NULL;
  42. aos_list_t buffer;
  43. aos_buf_t* content = NULL;
  44. aos_status_t* s = NULL;
  45. aos_pool_create(&p, NULL);
  46. options = oss_request_options_create(p);
  47. options->config = oss_config_create(options->pool);
  48. aos_str_set(&options->config->endpoint, endpoint.c_str());
  49. aos_str_set(&options->config->access_key_id, access_key_id.c_str());
  50. aos_str_set(&options->config->access_key_secret, access_key_secret.c_str());
  51. options->config->is_cname = is_cname;
  52. options->ctl = aos_http_controller_create(options->pool, 0);
  53. headers = aos_table_make(p, 1);
  54. apr_table_set(headers, "x-oss-meta-author", "oss");
  55. aos_str_set(&bucket, bucket_name.c_str());
  56. aos_str_set(&object, object_name.c_str());
  57. aos_list_init(&buffer);
  58. content = aos_buf_pack(options->pool, data.data(), data.length());
  59. aos_list_add_tail(&content->node, &buffer);
  60. s = oss_put_object_from_buffer(options, &bucket, &object,
  61. &buffer, headers, &resp_headers);
  62. std::string return_msg;
  63. if (aos_status_is_ok(s)) {
  64. }
  65. else {
  66. return_msg = "exec failed";
  67. if(s->code != NULL){
  68. return_msg += ",code:"+ std::to_string(s->code);
  69. }
  70. if(s->error_code != NULL){
  71. return_msg += ",error_code:"+ std::to_string(s->error_code);
  72. }
  73. if(s->error_msg != NULL){
  74. return_msg += ",error_msg:"+ std::to_string(s->error_code);
  75. }
  76. }
  77. aos_pool_destroy(p);
  78. return return_msg;
  79. }
  80. std::string module::aliyun_oss::upfile(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& filename, const std::string& content_type)
  81. {
  82. aos_pool_t* p = NULL;
  83. aos_string_t bucket;
  84. aos_string_t object;
  85. int is_cname = 0;
  86. aos_table_t* headers = NULL;
  87. aos_table_t* resp_headers = NULL;
  88. oss_request_options_t* options = NULL;
  89. aos_status_t* s = NULL;
  90. aos_string_t file;
  91. aos_pool_create(&p, NULL);
  92. options = oss_request_options_create(p);
  93. options->config = oss_config_create(options->pool);
  94. aos_str_set(&options->config->endpoint, endpoint.c_str());
  95. aos_str_set(&options->config->access_key_id, access_key_id.c_str());
  96. aos_str_set(&options->config->access_key_secret, access_key_secret.c_str());
  97. options->config->is_cname = is_cname;
  98. options->ctl = aos_http_controller_create(options->pool, 0);
  99. headers = aos_table_make(options->pool, 1);
  100. //apr_table_set(headers, OSS_CONTENT_TYPE, content_type.c_str());
  101. aos_str_set(&bucket, bucket_name.c_str());
  102. aos_str_set(&object, object_name.c_str());
  103. aos_str_set(&file, filename.c_str());
  104. s = oss_put_object_from_file(options, &bucket, &object, &file,
  105. headers, &resp_headers);
  106. std::string return_msg;
  107. if (aos_status_is_ok(s)) {
  108. }
  109. else {
  110. return_msg = "exec failed";
  111. if(s->code != NULL){
  112. return_msg += ",code:"+ std::to_string(s->code);
  113. }
  114. if(s->error_code != NULL){
  115. return_msg += ",error_code:"+ std::to_string(s->error_code);
  116. }
  117. if(s->error_msg != NULL){
  118. return_msg += ",error_msg:"+ std::to_string(s->error_code);
  119. }
  120. }
  121. aos_pool_destroy(p);
  122. return return_msg;
  123. }
  124. std::string module::aliyun_oss::del(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)
  125. {
  126. aos_pool_t* p = NULL;
  127. aos_string_t bucket;
  128. aos_string_t object;
  129. int is_cname = 0;
  130. aos_table_t* headers = NULL;
  131. aos_table_t* resp_headers = NULL;
  132. oss_request_options_t* options = NULL;
  133. aos_status_t* s = NULL;
  134. aos_pool_create(&p, NULL);
  135. options = oss_request_options_create(p);
  136. options->config = oss_config_create(options->pool);
  137. aos_str_set(&options->config->endpoint, endpoint.c_str());
  138. aos_str_set(&options->config->access_key_id, access_key_id.c_str());
  139. aos_str_set(&options->config->access_key_secret, access_key_secret.c_str());
  140. options->config->is_cname = is_cname;
  141. options->ctl = aos_http_controller_create(options->pool, 0);
  142. headers = aos_table_make(options->pool, 1);
  143. //apr_table_set(headers, OSS_CONTENT_TYPE, content_type.c_str());
  144. aos_str_set(&bucket, bucket_name.c_str());
  145. aos_str_set(&object, object_name.c_str());
  146. s = oss_delete_object(options, &bucket, &object, &resp_headers);
  147. std::string return_msg;
  148. if (aos_status_is_ok(s)) {
  149. }
  150. else {
  151. return_msg = "exec failed";
  152. if(s->code != NULL){
  153. return_msg += ",code:"+ std::to_string(s->code);
  154. }
  155. if(s->error_code != NULL){
  156. return_msg += ",error_code:"+ std::to_string(s->error_code);
  157. }
  158. if(s->error_msg != NULL){
  159. return_msg += ",error_msg:"+ std::to_string(s->error_code);
  160. }
  161. }
  162. aos_pool_destroy(p);
  163. return return_msg;
  164. }
  165. void module::aliyun_oss::regist(sol::state* lua)
  166. {
  167. lua->new_usertype<module::aliyun_oss>("fw_aliyunoss",
  168. "new", sol::constructors<module::aliyun_oss()>(),
  169. "upfile", &module::aliyun_oss::upfile,
  170. "updata", &module::aliyun_oss::updata,
  171. "del", &module::aliyun_oss::del
  172. );
  173. }
  174. void module::aliyun_oss::regist_global(const char* name, sol::state* lua)
  175. {
  176. lua->registry()[name] = this;
  177. (*lua)[name] = this;
  178. }