init
This commit is contained in:
10
example/CMakeLists.txt
Normal file
10
example/CMakeLists.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
set(CMAKE_BUILD_TYPE Debug)
|
||||
project(singleThread)
|
||||
|
||||
link_directories(/usr/local/lib64/)
|
||||
#set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR})
|
||||
add_executable(singleThread single_thread.cpp)
|
||||
target_link_libraries(singleThread hos_client_cpp)
|
||||
|
||||
#install(TARGETS singleThread DESTINATION ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
25
example/single_thread.cpp
Normal file
25
example/single_thread.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
/*************************************************************************
|
||||
> File Name: single_thread.cpp
|
||||
> Author: pxz
|
||||
> Created Time: Fri 11 Sep 2020 09:52:05 AM CST
|
||||
************************************************************************/
|
||||
#include<stdio.h>
|
||||
#include"../src/hos_client.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
hos_client_handle handle = hos_client_init("192.168.44.12:9098/hos", "default", "defalut");
|
||||
if (handle == NULL)
|
||||
{
|
||||
printf("error:hos_client_handle\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
hos_create_bucket(handle, "my-first-bucket");
|
||||
|
||||
hos_upload_async(handle, "my-first-bucket", "my-file.txt");
|
||||
|
||||
hos_client_close(handle);
|
||||
|
||||
return 0;
|
||||
}
|
||||
15
src/CMakeLists.txt
Normal file
15
src/CMakeLists.txt
Normal file
@@ -0,0 +1,15 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
SET(lib_name hos_client_cpp)
|
||||
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -shared -fPIC -std=c++11")
|
||||
SET(CMAKE_BUILD_TYPE Debug)
|
||||
|
||||
link_directories(/usr/local/lib64)
|
||||
set(CMKAE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -shared -fPIC")
|
||||
add_library(${lib_name}_shared SHARED hos_client.cpp)
|
||||
target_link_libraries(${lib_name}_shared libaws-cpp-sdk-s3.so libaws-cpp-sdk-core.so)
|
||||
set_target_properties(${lib_name}_shared PROPERTIES OUTPUT_NAME ${lib_name})
|
||||
|
||||
install(TARGETS ${lib_name}_shared LIBRARY DESTINATION /opt/MESA/lib)
|
||||
|
||||
#install(TARGETS ${lib_name}_shared LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib COMPONENT LIBRARIES)
|
||||
102
src/hos_client.cpp
Normal file
102
src/hos_client.cpp
Normal file
@@ -0,0 +1,102 @@
|
||||
/*************************************************************************
|
||||
> File Name: hos_client_api.cpp
|
||||
> Author: pxz
|
||||
> Created Time: Thu 10 Sep 2020 03:00:23 PM CST
|
||||
************************************************************************/
|
||||
#include <aws/core/Aws.h>
|
||||
#include <aws/s3/S3Client.h>
|
||||
#include <aws/s3/model/PutObjectRequest.h>
|
||||
#include <aws/s3/model/CreateBucketRequest.h>
|
||||
#include <aws/core/auth/AWSCredentials.h>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <sys/stat.h>
|
||||
#include "hos_client.h"
|
||||
|
||||
std::mutex upload_mutex;
|
||||
|
||||
static void PutObjectAsyncFinished(const Aws::S3::S3Client* s3Client,
|
||||
const Aws::S3::Model::PutObjectRequest& request,
|
||||
const Aws::S3::Model::PutObjectOutcome& outcome,
|
||||
const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context)
|
||||
{
|
||||
if (outcome.IsSuccess()) {
|
||||
std::cout << "Success: PutObjectAsyncFinished: Finished uploading '"
|
||||
<< context->GetUUID() << "'." << std::endl;
|
||||
}
|
||||
else {
|
||||
std::cout << "Error: PutObjectAsyncFinished: " <<
|
||||
outcome.GetError().GetMessage() << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
hos_client_handle hos_client_init(const char *endpoint, const char *accesskeyid, const char *secretkey)
|
||||
{
|
||||
Aws::SDKOptions options;
|
||||
Aws::InitAPI(options);
|
||||
|
||||
hos_client_handle handle = NULL;
|
||||
Aws::Client::ClientConfiguration config;
|
||||
Aws::Auth::AWSCredentials credentials(accesskeyid, secretkey);
|
||||
std::cout << "accesskeyid: " << credentials.GetAWSAccessKeyId() << "\n" << std::endl;
|
||||
std::cout << "secretkey: " << credentials.GetAWSSecretKey() << "\n" << std::endl;
|
||||
|
||||
config.endpointOverride = endpoint;
|
||||
config.verifySSL = false;
|
||||
config.enableEndpointDiscovery = true;
|
||||
|
||||
handle = new Aws::S3::S3Client(credentials, config);
|
||||
return handle;
|
||||
}
|
||||
|
||||
bool hos_create_bucket(hos_client_handle handle, const char *bucket)
|
||||
{
|
||||
Aws::S3::S3Client& s3Client = *(Aws::S3::S3Client *) handle;
|
||||
Aws::S3::Model::CreateBucketRequest createBucketRequest;
|
||||
createBucketRequest.SetBucket(bucket);
|
||||
std::cout << "bucket name: " << createBucketRequest.GetBucket() << "\n" << std::endl;
|
||||
|
||||
Aws::S3::Model::CreateBucketOutcome createBucketOutcome = s3Client.CreateBucket(createBucketRequest);
|
||||
|
||||
if (!createBucketOutcome.IsSuccess())
|
||||
{
|
||||
std::cout << "Failed to create bucket: " << bucket << "\n" << createBucketOutcome.GetError() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool hos_upload_async(hos_client_handle handle, const char *bucket, const char *object)
|
||||
{
|
||||
const Aws::S3::S3Client &s3Client = (Aws::S3::S3Client&) handle;
|
||||
struct stat buffer;
|
||||
|
||||
std::unique_lock<std::mutex> lock(upload_mutex);
|
||||
if (stat(object, &buffer) == -1)
|
||||
{
|
||||
//error: file does not exist.
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create and configure the asynchronous put object request.
|
||||
Aws::S3::Model::PutObjectRequest request;
|
||||
request.SetBucket(bucket);
|
||||
request.SetKey(object);
|
||||
|
||||
s3Client.PutObjectAsync(request, PutObjectAsyncFinished);
|
||||
return true;
|
||||
}
|
||||
|
||||
void hos_client_close(hos_client_handle handle)
|
||||
{
|
||||
if (handle == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
delete (Aws::S3::S3Client*)&handle;
|
||||
|
||||
return ;
|
||||
}
|
||||
15
src/hos_client.h
Normal file
15
src/hos_client.h
Normal file
@@ -0,0 +1,15 @@
|
||||
/*************************************************************************
|
||||
> File Name: hos_client_api.h
|
||||
> Author: pxz
|
||||
> Created Time: Thu 10 Sep 2020 03:13:59 PM CST
|
||||
************************************************************************/
|
||||
#ifndef __HOS_CLIENT_INIT__
|
||||
#define __HOS_CLIENT_INIT__
|
||||
|
||||
typedef void* hos_client_handle;
|
||||
|
||||
hos_client_handle hos_client_init(const char *endpoint, const char *accesskeyid, const char *secretkey);
|
||||
bool hos_create_bucket(hos_client_handle handle, const char *bucket);
|
||||
bool hos_upload_async(hos_client_handle handle, const char *bucket, const char *object);
|
||||
void hos_client_close(hos_client_handle handle);
|
||||
#endif
|
||||
BIN
support/aws-sdk-cpp-master.zip
Normal file
BIN
support/aws-sdk-cpp-master.zip
Normal file
Binary file not shown.
Reference in New Issue
Block a user