commit cd0649bfbcadaeca8d7e37b31d3e9a191160ce04 Author: pengxuanzheng Date: Fri Sep 11 16:13:02 2020 +0800 init diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt new file mode 100644 index 00000000..ff606acf --- /dev/null +++ b/example/CMakeLists.txt @@ -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}) diff --git a/example/single_thread.cpp b/example/single_thread.cpp new file mode 100644 index 00000000..71ddc4d5 --- /dev/null +++ b/example/single_thread.cpp @@ -0,0 +1,25 @@ +/************************************************************************* + > File Name: single_thread.cpp + > Author: pxz + > Created Time: Fri 11 Sep 2020 09:52:05 AM CST + ************************************************************************/ +#include +#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; +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 00000000..188cd77b --- /dev/null +++ b/src/CMakeLists.txt @@ -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) diff --git a/src/hos_client.cpp b/src/hos_client.cpp new file mode 100644 index 00000000..ac8261c1 --- /dev/null +++ b/src/hos_client.cpp @@ -0,0 +1,102 @@ +/************************************************************************* + > File Name: hos_client_api.cpp + > Author: pxz + > Created Time: Thu 10 Sep 2020 03:00:23 PM CST + ************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#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& 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 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 ; +} diff --git a/src/hos_client.h b/src/hos_client.h new file mode 100644 index 00000000..e38e9794 --- /dev/null +++ b/src/hos_client.h @@ -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 diff --git a/support/aws-sdk-cpp-master.zip b/support/aws-sdk-cpp-master.zip new file mode 100644 index 00000000..dd0e38d3 Binary files /dev/null and b/support/aws-sdk-cpp-master.zip differ