소스 검색

增加上传文件

a158 9 달 전
부모
커밋
4658ccebbc
3개의 변경된 파일87개의 추가작업 그리고 36개의 파일을 삭제
  1. 76 29
      src/aliyunoss.cpp
  2. 5 4
      src/aliyunoss.h
  3. 6 3
      target/aliyunoss.lua

+ 76 - 29
src/aliyunoss.cpp

@@ -11,40 +11,40 @@
 
 module::aliyun_oss::aliyun_oss()
 {
-    
+    if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
+        printf("aos_http_io_initialize failed");
+    }
 }
 
 module::aliyun_oss::~aliyun_oss()
 {
- 
+    aos_http_io_deinitialize();
 }
-std::string module::aliyun_oss::put(
-                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
-            )
+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
+)
 {
-    if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
-        return "NOT OK INIT";
-    }
 
-    aos_pool_t *p = NULL;
+
+    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_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_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());
@@ -64,30 +64,77 @@ std::string module::aliyun_oss::put(
     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);
- 
+    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 = "put object from buffer failed,code:"+std::to_string(s->code)+",error_code:"+s->error_code+",error_msg:"+s->error_msg;
-    }    
+
+    }
+    else {
+        return_msg = "put object from buffer failed,code:" + std::to_string(s->code) + ",error_code:" + s->error_code + ",error_msg:" + s->error_msg;
+    }
 
 
     aos_pool_destroy(p);
 
-    aos_http_io_deinitialize();
+
 
     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 = "put object from file failed,code:" + std::to_string(s->code) + ",error_code:" + s->error_code + ",error_msg:" + s->error_msg;
+    }
+
+    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()>(),
-        "put", &module::aliyun_oss::put
+        "upfile", &module::aliyun_oss::upfile,
+        "updata", &module::aliyun_oss::updata
     );
 }
 

+ 5 - 4
src/aliyunoss.h

@@ -6,13 +6,14 @@ namespace module
 	/// <summary>
 	/// AliyunOSS
 	/// </summary>
-	class aliyun_oss:public module::base {
+	class aliyun_oss :public module::base{
 	public:
 		aliyun_oss();
 		~aliyun_oss() override;
-		
-		 
-		std::string put(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);
+
+
+		std::string 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);
+		std::string 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);
 		static void regist(sol::state* lua);
 	private:
 		// 通过 imodule 继承

+ 6 - 3
target/aliyunoss.lua

@@ -16,11 +16,14 @@ function aliyun_oss.new(db)
     return instance
 end
 
-function aliyun_oss:put(endpoint,access_key_id, access_key_secret,bucket_name,object_name,data)
-    return self.module:put(endpoint,access_key_id, access_key_secret,bucket_name,object_name,data)
+function aliyun_oss:updata(endpoint,access_key_id, access_key_secret,bucket_name,object_name)
+    return self.module:updata(endpoint,access_key_id, access_key_secret,bucket_name,object_name)
+end
+function aliyun_oss:upfile(endpoint,access_key_id, access_key_secret,bucket_name,object_name,filename,content_type)
+    return self.module:upfile(endpoint,access_key_id, access_key_secret,bucket_name,object_name,filename,content_type)
 end
 function aliyun_oss:self()
     return self.module:self()
 end
 
-return aliyun_sdk
+return aliyun_oss