Files
microser/cfg_parse/oss_aliyun.cpp
2025-01-16 16:17:01 +08:00

511 lines
13 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @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>
char* OSS_ENDPOINT;
char* ACCESS_KEY_ID;
char* ACCESS_KEY_SECRET;
char* BUCKET_NAME;
//const char OSS_ENDPOINT[] = "oss-cn-nanjing.aliyuncs.com";
//const char ACCESS_KEY_ID[] = "LTAI5tER4bgJxT6Ptie7t2X7";
//const char ACCESS_KEY_SECRET[] = "dSYIC5hD3flhTNoLMAxCoKjSPdWFSz";
//const char BUCKET_NAME[] = "cn-pq-test";
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;
// initialize http io system, call it olny once
/* 在程序入口调用aos_http_io_initialize方法来初始化网络、内存等全局资源。*/
printf(">>>TestOSS ini Start");
if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
return;
}
printf(">>>TestOSS put Start");
// run samples
//put_object_from_buffer();
//put_object_from_file();
put_object_from_buffer();
get_object_to_file();
//get_object_to_buffer();
//get_object_to_file();
//delete_object();
printf(">>>TestOSS put End");
// deinitialize http io system, call it olny once
aos_http_io_deinitialize();
return ;
}
void PutOSS(char* File_Name,char* data) //zw修改 2023-9-7 上送oss文件
{
apr_file_t* output = NULL;
aos_pool_t* pool = NULL;
apr_status_t ret;
// initialize http io system, call it olny once
/* 在程序入口调用aos_http_io_initialize方法来初始化网络、内存等全局资源。*/
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);//使用buffer推送文件
printf(">>>PutOSS put End");
// deinitialize http io system, call it olny once
aos_http_io_deinitialize();
return;
}
void GetOSS(char* File_Name,char* savepath) //zw修改 2023-9-7 获取oss文件
{
apr_file_t* output = NULL;
aos_pool_t* pool = NULL;
apr_status_t ret;
// initialize http io system, call it olny once
/* 在程序入口调用aos_http_io_initialize方法来初始化网络、内存等全局资源。*/
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);//使用buffer推送文件
printf(">>>GetOSS put End");
// deinitialize http io system, call it olny once
aos_http_io_deinitialize();
return;
}
void DelOSS(char* File_Name)
{
apr_file_t* output = NULL;
aos_pool_t* pool = NULL;
apr_status_t ret;
// initialize http io system, call it olny once
/* 在程序入口调用aos_http_io_initialize方法来初始化网络、内存等全局资源。*/
printf(">>>DelOSS ini Start");
if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
return;
}
printf(">>>DelOSS put Start");
delete_object_new(File_Name);//使用buffer推送文件
printf(">>>DelOSS put End");
// deinitialize http io system, call it olny once
aos_http_io_deinitialize();
return;
}
void coutTest() {
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修改 2023-9-7 oss推送
{
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;
/* 是否使用了CNAME。0表示不使用。*/
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;
/* 用于内存管理的内存池pool等价于apr_pool_t。其实现代码在apr库中。*/
aos_pool_create(&p, NULL);
/* 创建并初始化options该参数包括endpoint、access_key_id、acces_key_secret、is_cname、curl等全局配置信息。*/
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;
/* 是否使用了CNAME。0表示不使用。*/
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;
/* 用于内存管理的内存池pool等价于apr_pool_t。其实现代码在apr库中。*/
aos_pool_create(&p, NULL);
/* 创建并初始化options该参数包括endpoint、access_key_id、acces_key_secret、is_cname、curl等全局配置信息。*/
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 {
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 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);
}