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);
void TestOSS()
{
apr_file_t *output = NULL;
aos_pool_t *pool = NULL;
apr_status_t ret;
/* <20>ڳ<EFBFBD><DAB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>aos_http_io_initialize<7A><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4>*/
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 ;
}
void PutOSS(char* File_Name,char* data) //zw<7A>޸<EFBFBD> 2023-9-7 <20><><EFBFBD><EFBFBD>oss<73>ļ<EFBFBD>
{
apr_file_t* output = NULL;
aos_pool_t* pool = NULL;
apr_status_t ret;
/* <20>ڳ<EFBFBD><DAB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>aos_http_io_initialize<7A><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4>*/
printf(">>>PutOSS ini Start");
printf(File_Name);
printf(data);
if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
return;
}
printf(">>>PutOSS put Start");
put_object_from_file_new(File_Name, data);//ʹ<><CAB9>buffer<65><72><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
printf(">>>PutOSS put End");
aos_http_io_deinitialize();
return;
}
void GetOSS(char* File_Name,char* savepath) //zw<7A>޸<EFBFBD> 2023-9-7 <20><>ȡoss<73>ļ<EFBFBD>
{
apr_file_t* output = NULL;
aos_pool_t* pool = NULL;
apr_status_t ret;
/* <20>ڳ<EFBFBD><DAB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>aos_http_io_initialize<7A><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4>*/
printf(">>>GetOSS ini Start");
if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
return;
}
printf(">>>GetOSS put Start");
get_object_to_file_new(File_Name,savepath);//ʹ<><CAB9>buffer<65><72><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
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;
/* <20>ڳ<EFBFBD><DAB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>aos_http_io_initialize<7A><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4>*/
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-01-16 16:17:01 +08:00
delete_object_new(File_Name);//ʹ<><CAB9>buffer<65><72><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
printf(">>>DelOSS put End");
aos_http_io_deinitialize();
return;
}
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);
}
void put_object_from_buffer_new(char* File_Name,char* data)//zw<7A>޸<EFBFBD> 2023-9-7 oss<73><73><EFBFBD><EFBFBD>
{
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;
/* <20>Ƿ<EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD>CNAME<4D><45>0<EFBFBD><30>ʾ<EFBFBD><CABE>ʹ<EFBFBD>á<EFBFBD>*/
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;
/* <20><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4>أ<EFBFBD>pool<6F><6C><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD><C8BC><EFBFBD>apr_pool_t<5F><74><EFBFBD><EFBFBD>ʵ<EFBFBD>ִ<EFBFBD><D6B4><EFBFBD><EFBFBD><EFBFBD>apr<70><72><EFBFBD>С<EFBFBD>*/
aos_pool_create(&p, NULL);
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>options<6E><73><EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>endpoint<6E><74>access_key_id<69><64>acces_key_secret<65><74>is_cname<6D><65>curl<72><6C>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2>*/
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;
/* <20>Ƿ<EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD>CNAME<4D><45>0<EFBFBD><30>ʾ<EFBFBD><CABE>ʹ<EFBFBD>á<EFBFBD>*/
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;
/* <20><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4>أ<EFBFBD>pool<6F><6C><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD><C8BC><EFBFBD>apr_pool_t<5F><74><EFBFBD><EFBFBD>ʵ<EFBFBD>ִ<EFBFBD><D6B4><EFBFBD><EFBFBD><EFBFBD>apr<70><72><EFBFBD>С<EFBFBD>*/
aos_pool_create(&p, NULL);
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>options<6E><73><EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>endpoint<6E><74>access_key_id<69><64>acces_key_secret<65><74>is_cname<6D><65>curl<72><6C>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2>*/
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);
}