summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorpengxuanzheng <[email protected]>2020-09-11 16:13:02 +0800
committerpengxuanzheng <[email protected]>2020-09-11 16:13:02 +0800
commitcd0649bfbcadaeca8d7e37b31d3e9a191160ce04 (patch)
tree90e25d35da4fc1087ad07fb7c6cd321b66a5811f /src
init
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt15
-rw-r--r--src/hos_client.cpp102
-rw-r--r--src/hos_client.h15
3 files changed, 132 insertions, 0 deletions
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 <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 ;
+}
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