增加OSS

This commit is contained in:
a158
2025-12-10 11:55:31 +08:00
parent 0e15d59dea
commit 2ef062e1f4
9 changed files with 219 additions and 34 deletions

BIN
3rdparty/aliyun-oss-c-sdk-3.11.2.zip vendored Normal file

Binary file not shown.

View File

@@ -4,6 +4,10 @@ set(MODULE_NAME "aliyunsdk")
# 设置项目名为当前目录名
project(${MODULE_NAME})
#set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build" FORCE)
#set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0")
#set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -O0")
# 搜索源文件和头文件
file(GLOB_RECURSE SOURCE_FILES "${PROJECT_SOURCE_DIR}/src/*.cpp")
file(GLOB_RECURSE HEADER_FILES
@@ -42,7 +46,10 @@ else()
/usr/local/include/ylib
/usr/local/include/fastweb
/opt/lua54/include
/usr/local/include)
/usr/local/include
/usr/include/apr-1.0
/usr/local/include/oss_c_sdk
)
add_definitions(-DfPIC)
endif()
@@ -59,9 +66,13 @@ else()
/opt/lua54/lib/liblua.a
pthread
/usr/lib/x86_64-linux-gnu/libalibabacloud-sdk-core.so
/usr/local/lib/liboss_c_sdk.so
/usr/lib/x86_64-linux-gnu/libmxml.so
/usr/lib/x86_64-linux-gnu/libapr-1.so
/usr/lib/x86_64-linux-gnu/libaprutil-1.so
)
endif()
install(TARGETS ${MODULE_NAME} DESTINATION $<IF:$<CONFIG:Debug>,${FASTWEB}/bin/debug/module/${MODULE_NAME},${FASTWEB}/bin/release/module/${MODULE_NAME}>)
install(TARGETS ${MODULE_NAME} DESTINATION $<IF:$<CONFIG:Debug>,${FASTWEB}/bin/debug/module/${MODULE_NAME},${FASTWEB}/bin/release/module/${MODULE_NAME}>)

View File

@@ -2,6 +2,30 @@
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# 安装阿里云核心
mkdir tmp
cd tmp
git clone https://gitee.com/yobonianhua/aliyun-openapi-cpp-sdk.git
cd aliyun-openapi-cpp-sdk
sudo sh easyinstall.sh core
cd ${SCRIPT_DIR}
# 安装阿里云OSS
sudo apt-get install libcurl4-openssl-dev libapr1-dev libaprutil1-dev libmxml-dev -y
cd 3rdparty
rm -rf aliyun-oss-c-sdk-3.11.2
unzip aliyun-oss-c-sdk-3.11.2.zip
cd aliyun-oss-c-sdk-3.11.2
mkdir build
cd build
cmake ..
make
make install
cd ${SCRIPT_DIR}
# 安装ALIYUN
rm -rf build
mkdir build
cd build
cmake ..

98
src/aliyunoss.cpp Normal file
View File

@@ -0,0 +1,98 @@
#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>
module::aliyun_oss::aliyun_oss()
{
}
module::aliyun_oss::~aliyun_oss()
{
}
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
)
{
if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
return "NOT OK INIT";
}
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 = "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;
}
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
);
}
void module::aliyun_oss::regist_global(const char* name, sol::state* lua)
{
lua->registry()[name] = this;
(*lua)[name] = this;
}

23
src/aliyunoss.h Normal file
View File

@@ -0,0 +1,23 @@
#pragma once
#include "basemodule.h"
#include "oss_c_sdk/oss_api.h"
namespace module
{
/// <summary>
/// AliyunOSS
/// </summary>
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);
static void regist(sol::state* lua);
private:
// 通过 imodule 继承
virtual void regist_global(const char* name, sol::state* lua);
virtual void delete_global() { delete this; }
};
}

View File

@@ -4,6 +4,7 @@
#include <alibabacloud/core/CommonRequest.h>
#include <alibabacloud/core/CommonClient.h>
#include <alibabacloud/core/CommonResponse.h>
#include "aliyunoss.h"
extern "C" {
#ifdef _WIN32
DLL_EXPORT
@@ -12,8 +13,9 @@ extern "C" {
{
sol::state* state = static_cast<sol::state*>(sol2);
module::aliyun_sdk::regist(state);
module::aliyun_oss::regist(state);
return 0;
}
}
}
module::aliyun_sdk::aliyun_sdk()

View File

@@ -1,5 +1,6 @@
#pragma once
#include "basemodule.h"
#include "oss_c_sdk/oss_api.h"
namespace module
{
/// <summary>
@@ -9,7 +10,7 @@ namespace module
public:
aliyun_sdk();
~aliyun_sdk() override;
void set(const std::string& access_key_id,const std::string& access_key_secret);

26
target/aliyunoss.lua Normal file
View File

@@ -0,0 +1,26 @@
local aliyun_oss = {}
aliyun_oss.__index = aliyun_oss
--[[
创建一个新的 aliyun_oss 对象
@return 返回一个新的 aliyun_oss 对象
]]
function aliyun_oss.new(db)
local instance = setmetatable({}, aliyun_oss)
if db == nil then
instance.module = fw_aliyunoss.new()
else
instance.module = db
end
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)
end
function aliyun_oss:self()
return self.module:self()
end
return aliyun_sdk

View File

@@ -1,30 +1,30 @@
local aliyun_sdk = {}
aliyun_sdk.__index = aliyun_sdk
--[[
创建一个新的 fw_aliyun_sdk 对象
@return 返回一个新的 fw_aliyun_sdk 对象
]]
function aliyun_sdk.new(db)
local instance = setmetatable({}, aliyun_sdk)
if db == nil then
instance.module = fw_aliyunsdk.new()
else
instance.module = db
end
return instance
end
function aliyun_sdk:set(access_key_id, access_key_secret)
return self.module:set(access_key_id, access_key_secret)
end
function aliyun_sdk:exec(domain, version, parameter)
return self.module:exec(domain, version, parameter)
end
function aliyun_sdk:self()
return self.module:self()
end
return aliyun_sdk
local aliyun_sdk = {}
aliyun_sdk.__index = aliyun_sdk
--[[
创建一个新的 fw_aliyun_sdk 对象
@return 返回一个新的 fw_aliyun_sdk 对象
]]
function aliyun_sdk.new(db)
local instance = setmetatable({}, aliyun_sdk)
if db == nil then
instance.module = fw_aliyunsdk.new()
else
instance.module = db
end
return instance
end
function aliyun_sdk:set(access_key_id, access_key_secret)
return self.module:set(access_key_id, access_key_secret)
end
function aliyun_sdk:exec(domain, version, parameter)
return self.module:exec(domain, version, parameter)
end
function aliyun_sdk:self()
return self.module:self()
end
return aliyun_sdk