| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- #include "aliyunoss.h"
- #include "dll_interface.h"
- #include "aos_log.h"
- #include "aos_util.h"
- #include "aos_string.h"
- #include "aos_status.h"
- #include "oss_auth.h"
- #include "oss_util.h"
- #include "oss_api.h"
- #include <oss_c_sdk/aos_status.h>
- static int inited = 0;
- module::aliyun_oss::aliyun_oss()
- {
- if(inited == 0){
- if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
- printf("COS HTTP aos_http_io_initialize FAILED");
- return;
- }
- inited = 1;
- }
- }
- module::aliyun_oss::~aliyun_oss()
- {
- //aos_http_io_deinitialize();
- }
- std::string module::aliyun_oss::updata(
- 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_view& data
- )
- {
- aos_pool_t* p = NULL;
- aos_string_t bucket;
- aos_string_t object;
- int is_cname = 0;
- aos_table_t* headers = NULL;
- aos_table_t* resp_headers = NULL;
- oss_request_options_t* options = NULL;
- aos_list_t buffer;
- aos_buf_t* content = NULL;
- aos_status_t* s = NULL;
- aos_pool_create(&p, NULL);
- options = oss_request_options_create(p);
- options->config = oss_config_create(options->pool);
- aos_str_set(&options->config->endpoint, endpoint.c_str());
- aos_str_set(&options->config->access_key_id, access_key_id.c_str());
- aos_str_set(&options->config->access_key_secret, access_key_secret.c_str());
- options->config->is_cname = is_cname;
- options->ctl = aos_http_controller_create(options->pool, 0);
- headers = aos_table_make(p, 1);
- apr_table_set(headers, "x-oss-meta-author", "oss");
- aos_str_set(&bucket, bucket_name.c_str());
- aos_str_set(&object, object_name.c_str());
- aos_list_init(&buffer);
- content = aos_buf_pack(options->pool, data.data(), data.length());
- aos_list_add_tail(&content->node, &buffer);
- s = oss_put_object_from_buffer(options, &bucket, &object,
- &buffer, headers, &resp_headers);
- std::string return_msg;
- if (aos_status_is_ok(s)) {
- }
- else {
- return_msg = "exec failed";
- if(s->code != NULL){
- return_msg += ",code:"+ std::to_string(s->code);
- }
- if(s->error_code != NULL){
- return_msg += ",error_code:"+ std::to_string(s->error_code);
- }
- if(s->error_msg != NULL){
- return_msg += ",error_msg:"+ std::to_string(s->error_code);
- }
- }
- aos_pool_destroy(p);
- return return_msg;
- }
- 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)
- {
- aos_pool_t* p = NULL;
- aos_string_t bucket;
- aos_string_t object;
- int is_cname = 0;
- aos_table_t* headers = NULL;
- aos_table_t* resp_headers = NULL;
- oss_request_options_t* options = NULL;
- aos_status_t* s = NULL;
- aos_string_t file;
- aos_pool_create(&p, NULL);
- options = oss_request_options_create(p);
- options->config = oss_config_create(options->pool);
- aos_str_set(&options->config->endpoint, endpoint.c_str());
- aos_str_set(&options->config->access_key_id, access_key_id.c_str());
- aos_str_set(&options->config->access_key_secret, access_key_secret.c_str());
- options->config->is_cname = is_cname;
- options->ctl = aos_http_controller_create(options->pool, 0);
- headers = aos_table_make(options->pool, 1);
- //apr_table_set(headers, OSS_CONTENT_TYPE, content_type.c_str());
- aos_str_set(&bucket, bucket_name.c_str());
- aos_str_set(&object, object_name.c_str());
- aos_str_set(&file, filename.c_str());
- s = oss_put_object_from_file(options, &bucket, &object, &file,
- headers, &resp_headers);
- std::string return_msg;
- if (aos_status_is_ok(s)) {
- }
- else {
- return_msg = "exec failed";
- if(s->code != NULL){
- return_msg += ",code:"+ std::to_string(s->code);
- }
- if(s->error_code != NULL){
- return_msg += ",error_code:"+ std::to_string(s->error_code);
- }
- if(s->error_msg != NULL){
- return_msg += ",error_msg:"+ std::to_string(s->error_code);
- }
- }
- aos_pool_destroy(p);
- return return_msg;
- }
- 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)
- {
- aos_pool_t* p = NULL;
- aos_string_t bucket;
- aos_string_t object;
- int is_cname = 0;
- aos_table_t* headers = NULL;
- aos_table_t* resp_headers = NULL;
- oss_request_options_t* options = NULL;
- aos_status_t* s = NULL;
- aos_pool_create(&p, NULL);
- options = oss_request_options_create(p);
- options->config = oss_config_create(options->pool);
- aos_str_set(&options->config->endpoint, endpoint.c_str());
- aos_str_set(&options->config->access_key_id, access_key_id.c_str());
- aos_str_set(&options->config->access_key_secret, access_key_secret.c_str());
- options->config->is_cname = is_cname;
- options->ctl = aos_http_controller_create(options->pool, 0);
- headers = aos_table_make(options->pool, 1);
- //apr_table_set(headers, OSS_CONTENT_TYPE, content_type.c_str());
- aos_str_set(&bucket, bucket_name.c_str());
- aos_str_set(&object, object_name.c_str());
- s = oss_delete_object(options, &bucket, &object, &resp_headers);
- std::string return_msg;
- if (aos_status_is_ok(s)) {
- }
- else {
- return_msg = "exec failed";
- if(s->code != NULL){
- return_msg += ",code:"+ std::to_string(s->code);
- }
- if(s->error_code != NULL){
- return_msg += ",error_code:"+ std::to_string(s->error_code);
- }
- if(s->error_msg != NULL){
- return_msg += ",error_msg:"+ std::to_string(s->error_code);
- }
- }
- aos_pool_destroy(p);
- return return_msg;
- }
- void module::aliyun_oss::regist(sol::state* lua)
- {
- lua->new_usertype<module::aliyun_oss>("fw_aliyunoss",
- "new", sol::constructors<module::aliyun_oss()>(),
- "upfile", &module::aliyun_oss::upfile,
- "updata", &module::aliyun_oss::updata,
- "del", &module::aliyun_oss::del
- );
- }
- void module::aliyun_oss::regist_global(const char* name, sol::state* lua)
- {
- lua->registry()[name] = this;
- (*lua)[name] = this;
- }
|