Files
microser/cfg_parse/oss_aliyun.cpp

484 lines
12 KiB
C++
Raw Normal View History

2025-01-16 16:17:01 +08:00
/**
* @file: $RCSfile: oss_aliyun.cpp,v $
* @brief: $aliyun oss include
*
* @version: $Revision: 1.01 $
* @date: $Date: 2023/08/31 23:02:00 $
* @author: $Author: wangwei $
* @state: $State: Exp $
*
* @latest: $Id: oss_aliyun.cpp,v 1.01 2023/08/31 23:02:00 wangwei Exp $
*
*/
using namespace std;
#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 "../mms/db_interface.h"
#include <iostream>
2025-04-29 15:05:36 +08:00
char* OSS_ENDPOINT;
char* ACCESS_KEY_ID;
char* ACCESS_KEY_SECRET;
2025-01-16 16:17:01 +08:00
char* BUCKET_NAME;
const char OBJECT_NAME[] = "comtrade/temp.json";
void init_sample_request_options(oss_request_options_t *options, int is_cname);
void put_object_from_buffer();
void put_object_from_buffer_new(char* File_Name, char* data);
void put_object_from_file();
void put_object_from_file_new(char* File_Name, char* path);
void get_object_to_buffer();
void get_object_to_file();
void get_object_to_file_new(char* File_Name, char* savepath);
void delete_object();
void delete_object_new(char* File_Name);
2026-01-20 15:51:41 +08:00
#if 0
2025-01-16 16:17:01 +08:00
void TestOSS()
{
apr_file_t *output = NULL;
aos_pool_t *pool = NULL;
apr_status_t ret;
2025-05-09 16:53:07 +08:00
/* 在程序入口调用aos_http_io_initialize方法来初始化网络、内存等全局资源。*/
2025-01-16 16:17:01 +08:00
printf(">>>TestOSS ini Start");
if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
return;
}
printf(">>>TestOSS put Start");
2025-04-29 15:05:36 +08:00
2025-01-16 16:17:01 +08:00
put_object_from_buffer();
get_object_to_file();
2025-04-29 15:05:36 +08:00
printf(">>>TestOSS put End");
2025-01-16 16:17:01 +08:00
aos_http_io_deinitialize();
return ;
}
2025-05-09 16:53:07 +08:00
void PutOSS(char* File_Name,char* data) //zw修改 2023-9-7 上送oss文件
2025-01-16 16:17:01 +08:00
{
apr_file_t* output = NULL;
aos_pool_t* pool = NULL;
apr_status_t ret;
2025-05-09 16:53:07 +08:00
/* 在程序入口调用aos_http_io_initialize方法来初始化网络、内存等全局资源。*/
2025-01-16 16:17:01 +08:00
printf(">>>PutOSS ini Start");
printf(File_Name);
printf(data);
if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
return;
}
printf(">>>PutOSS put Start");
2025-05-09 16:53:07 +08:00
put_object_from_file_new(File_Name, data);//使用buffer推送文件
2025-01-16 16:17:01 +08:00
printf(">>>PutOSS put End");
aos_http_io_deinitialize();
return;
}
2025-05-09 16:53:07 +08:00
void GetOSS(char* File_Name,char* savepath) //zw修改 2023-9-7 获取oss文件
2025-01-16 16:17:01 +08:00
{
apr_file_t* output = NULL;
aos_pool_t* pool = NULL;
apr_status_t ret;
2025-05-09 16:53:07 +08:00
/* 在程序入口调用aos_http_io_initialize方法来初始化网络、内存等全局资源。*/
2025-01-16 16:17:01 +08:00
printf(">>>GetOSS ini Start");
if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
return;
}
printf(">>>GetOSS put Start");
2025-05-09 16:53:07 +08:00
get_object_to_file_new(File_Name,savepath);//使用buffer推送文件
2025-01-16 16:17:01 +08:00
printf(">>>GetOSS put End");
aos_http_io_deinitialize();
return;
}
void DelOSS(char* File_Name)
{
apr_file_t* output = NULL;
aos_pool_t* pool = NULL;
apr_status_t ret;
2025-05-09 16:53:07 +08:00
/* 在程序入口调用aos_http_io_initialize方法来初始化网络、内存等全局资源。*/
2025-01-16 16:17:01 +08:00
printf(">>>DelOSS ini Start");
if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
return;
}
printf(">>>DelOSS put Start");
2025-04-29 15:05:36 +08:00
2025-05-09 16:53:07 +08:00
delete_object_new(File_Name);//使用buffer推送文件
2025-01-16 16:17:01 +08:00
printf(">>>DelOSS put End");
aos_http_io_deinitialize();
return;
}
2026-01-20 15:51:41 +08:00
#endif
2025-04-29 15:05:36 +08:00
void coutTest()
{
2025-01-16 16:17:01 +08:00
std:: cout << "OSS_ENDPOINT:" << OSS_ENDPOINT << std::endl;
std::cout << "ACCESS_KEY_ID:" << ACCESS_KEY_ID << std::endl;
std::cout << "ACCESS_KEY_SECRET:" << ACCESS_KEY_SECRET << std::endl;
std::cout << "BUCKET_NAME:" << BUCKET_NAME << std::endl;
}
void init_sample_request_options(oss_request_options_t *options, int is_cname)
{
options->config = oss_config_create(options->pool);
aos_str_set(&options->config->endpoint, OSS_ENDPOINT);
aos_str_set(&options->config->access_key_id, ACCESS_KEY_ID);
aos_str_set(&options->config->access_key_secret, ACCESS_KEY_SECRET);
options->config->is_cname = is_cname;
options->ctl = aos_http_controller_create(options->pool, 0);
}
void put_object_from_buffer()
{
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;
char *str = "test oss c sdk";
aos_status_t *s = NULL;
aos_pool_create(&p, NULL);
options = oss_request_options_create(p);
init_sample_request_options(options, is_cname);
headers = aos_table_make(p, 1);
apr_table_set(headers, "x-oss-meta-author", "oss");
aos_str_set(&bucket, BUCKET_NAME);
aos_str_set(&object, OBJECT_NAME);
aos_list_init(&buffer);
content = aos_buf_pack(options->pool, str, strlen(str));
aos_list_add_tail(&content->node, &buffer);
s = oss_put_object_from_buffer(options, &bucket, &object,
&buffer, headers, &resp_headers);
if (aos_status_is_ok(s)) {
printf("put object from buffer succeeded\n");
}
else {
printf("put object from buffer failed\n");
}
aos_pool_destroy(p);
}
2025-05-09 16:53:07 +08:00
void put_object_from_buffer_new(char* File_Name,char* data)//zw修改 2023-9-7 oss推送
2025-01-16 16:17:01 +08:00
{
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;
char* str = data;
aos_status_t* s = NULL;
aos_pool_create(&p, NULL);
options = oss_request_options_create(p);
init_sample_request_options(options, is_cname);
headers = aos_table_make(p, 1);
apr_table_set(headers, "x-oss-meta-author", "oss");
aos_str_set(&bucket, BUCKET_NAME);
aos_str_set(&object, File_Name);
aos_list_init(&buffer);
content = aos_buf_pack(options->pool, str, strlen(str));
aos_list_add_tail(&content->node, &buffer);
s = oss_put_object_from_buffer(options, &bucket, &object,
&buffer, headers, &resp_headers);
if (aos_status_is_ok(s)) {
printf("put object from buffer succeeded\n");
}
else {
printf("put object from buffer failed\n");
}
aos_pool_destroy(p);
}
void put_object_from_file()
{
aos_pool_t *p = NULL;
aos_string_t bucket;
aos_string_t object;
2025-05-09 16:53:07 +08:00
/* 是否使用了CNAME。0表示不使用。*/
2025-01-16 16:17:01 +08:00
int is_cname = 0;
aos_table_t *headers = NULL;
aos_table_t *resp_headers = NULL;
oss_request_options_t *options = NULL;
char *filename = "/home/pq/FeProject/etc/Device_Config.xml";
aos_status_t *s = NULL;
aos_string_t file;
2025-05-09 16:53:07 +08:00
/* 用于内存管理的内存池pool等价于apr_pool_t。其实现代码在apr库中。*/
2025-01-16 16:17:01 +08:00
aos_pool_create(&p, NULL);
2025-05-09 16:53:07 +08:00
/* 创建并初始化options该参数包括endpoint、access_key_id、acces_key_secret、is_cname、curl等全局配置信息。*/
2025-01-16 16:17:01 +08:00
options = oss_request_options_create(p);
init_sample_request_options(options, is_cname);
aos_str_set(&bucket, BUCKET_NAME);
aos_str_set(&object, OBJECT_NAME);
aos_str_set(&file, filename);
s = oss_put_object_from_file(options, &bucket, &object, &file,
headers, &resp_headers);
if (aos_status_is_ok(s)) {
printf("put object from file succeeded\n");
}
else {
printf("put object from file failed, code:%d, error_code:%s, error_msg:%s, request_id:%s\n",
s->code, s->error_code, s->error_msg, s->req_id);
}
aos_pool_destroy(p);
}
void put_object_from_file_new(char* File_Name, char* path)
{
aos_pool_t* p = NULL;
aos_string_t bucket;
aos_string_t object;
2025-05-09 16:53:07 +08:00
/* 是否使用了CNAME。0表示不使用。*/
2025-01-16 16:17:01 +08:00
int is_cname = 0;
aos_table_t* headers = NULL;
aos_table_t* resp_headers = NULL;
oss_request_options_t* options = NULL;
char* filename = path;
aos_status_t* s = NULL;
aos_string_t file;
2025-05-09 16:53:07 +08:00
/* 用于内存管理的内存池pool等价于apr_pool_t。其实现代码在apr库中。*/
2025-01-16 16:17:01 +08:00
aos_pool_create(&p, NULL);
2025-05-09 16:53:07 +08:00
/* 创建并初始化options该参数包括endpoint、access_key_id、acces_key_secret、is_cname、curl等全局配置信息。*/
2025-01-16 16:17:01 +08:00
options = oss_request_options_create(p);
init_sample_request_options(options, is_cname);
aos_str_set(&bucket, BUCKET_NAME);
aos_str_set(&object, File_Name);
aos_str_set(&file, filename);
s = oss_put_object_from_file(options, &bucket, &object, &file,
headers, &resp_headers);
if (aos_status_is_ok(s)) {
printf("put object from file succeeded\n");
}
else {
2025-04-29 15:05:36 +08:00
printf("put object from file failed, code:%d, error_code:%s, error_msg:%s, request_id:%s\n",s->code, s->error_code, s->error_msg, s->req_id);
2025-01-16 16:17:01 +08:00
}
aos_pool_destroy(p);
}
void get_object_to_buffer()
{
aos_pool_t *p = NULL;
aos_string_t bucket;
aos_string_t object;
int is_cname = 0;
oss_request_options_t *options = NULL;
aos_table_t *headers = NULL;
aos_table_t *params = NULL;
aos_table_t *resp_headers = NULL;
aos_status_t *s = NULL;
aos_list_t buffer;
aos_buf_t *content = NULL;
char *buf = NULL;
int64_t len = 0;
int64_t size = 0;
int64_t pos = 0;
aos_pool_create(&p, NULL);
options = oss_request_options_create(p);
init_sample_request_options(options, is_cname);
aos_str_set(&bucket, BUCKET_NAME);
aos_str_set(&object, OBJECT_NAME);
aos_list_init(&buffer);
s = oss_get_object_to_buffer(options, &bucket, &object,
headers, params, &buffer, &resp_headers);
if (aos_status_is_ok(s)) {
printf("get object to buffer succeeded\n");
}
else {
printf("get object to buffer failed\n");
}
//get buffer len
aos_list_for_each_entry(aos_buf_t, content, &buffer, node) {
len += aos_buf_size(content);
}
buf = (char*)aos_pcalloc(p, (apr_size_t)(len + 1));
buf[len] = '\0';
//copy buffer content to memory
aos_list_for_each_entry(aos_buf_t, content, &buffer, node) {
size = aos_buf_size(content);
memcpy(buf + pos, content->pos, (size_t)size);
pos += size;
}
aos_pool_destroy(p);
}
void get_object_to_file()
{
aos_pool_t *p = NULL;
aos_string_t bucket;
char *download_filename = "Data/tempbuff.jpg";
aos_string_t object;
int is_cname = 0;
oss_request_options_t *options = NULL;
aos_table_t *headers = NULL;
aos_table_t *params = NULL;
aos_table_t *resp_headers = NULL;
aos_status_t *s = NULL;
aos_string_t file;
aos_pool_create(&p, NULL);
options = oss_request_options_create(p);
init_sample_request_options(options, is_cname);
aos_str_set(&bucket, BUCKET_NAME);
aos_str_set(&object, OBJECT_NAME);
headers = aos_table_make(p, 0);
aos_str_set(&file, download_filename);
s = oss_get_object_to_file(options, &bucket, &object, headers,
params, &file, &resp_headers);
if (aos_status_is_ok(s)) {
printf("get object to local file succeeded\n");
}
else {
printf("get object to local file failed\n");
}
aos_pool_destroy(p);
}
void get_object_to_file_new(char* File_Name,char* savepath)
{
aos_pool_t* p = NULL;
aos_string_t bucket;
char* download_filename = savepath;
aos_string_t object;
int is_cname = 0;
oss_request_options_t* options = NULL;
aos_table_t* headers = NULL;
aos_table_t* params = NULL;
aos_table_t* resp_headers = NULL;
aos_status_t* s = NULL;
aos_string_t file;
aos_pool_create(&p, NULL);
options = oss_request_options_create(p);
init_sample_request_options(options, is_cname);
aos_str_set(&bucket, BUCKET_NAME);
aos_str_set(&object, File_Name);
headers = aos_table_make(p, 0);
aos_str_set(&file, download_filename);
s = oss_get_object_to_file(options, &bucket, &object, headers,
params, &file, &resp_headers);
if (aos_status_is_ok(s)) {
printf("get object to local file succeeded\n");
}
else {
printf("get object to local file failed\n");
}
aos_pool_destroy(p);
}
void delete_object()
{
aos_pool_t *p = NULL;
aos_string_t bucket;
aos_string_t object;
int is_cname = 0;
oss_request_options_t *options = NULL;
aos_table_t *resp_headers = NULL;
aos_status_t *s = NULL;
aos_pool_create(&p, NULL);
options = oss_request_options_create(p);
init_sample_request_options(options, is_cname);
aos_str_set(&bucket, BUCKET_NAME);
aos_str_set(&object, OBJECT_NAME);
s = oss_delete_object(options, &bucket, &object, &resp_headers);
if (aos_status_is_ok(s)) {
printf("delete object succeed\n");
}
else {
printf("delete object failed\n");
}
aos_pool_destroy(p);
}
void delete_object_new(char* File_Name)
{
aos_pool_t* p = NULL;
aos_string_t bucket;
aos_string_t object;
int is_cname = 0;
oss_request_options_t* options = NULL;
aos_status_t* s = NULL;
aos_pool_create(&p, NULL);
options = oss_request_options_create(p);
init_sample_request_options(options, is_cname);
aos_str_set(&bucket, BUCKET_NAME);
aos_str_set(&object, File_Name);
s = oss_delete_objects_by_prefix(options, &bucket, &object);
if (aos_status_is_ok(s)) {
printf("delete object succeed\n");
}
else {
printf("delete object failed\n");
}
aos_pool_destroy(p);
}