diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index ff606acf..b9a3f2a9 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -2,7 +2,10 @@ cmake_minimum_required(VERSION 3.5) set(CMAKE_BUILD_TYPE Debug) project(singleThread) +SET(CMAKE_BUILD_TYPE Debug) link_directories(/usr/local/lib64/) +link_directories(/opt/MESA/lib/) +link_libraries(hos_client_cpp) #set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}) add_executable(singleThread single_thread.cpp) target_link_libraries(singleThread hos_client_cpp) diff --git a/example/single_thread.cpp b/example/single_thread.cpp index 71ddc4d5..fe584e2f 100644 --- a/example/single_thread.cpp +++ b/example/single_thread.cpp @@ -4,21 +4,32 @@ > Created Time: Fri 11 Sep 2020 09:52:05 AM CST ************************************************************************/ #include +#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"); + char *bucket = argv[1]; + printf("hos_client_init start ...\n"); + hos_client_handle handle = hos_client_init("http://192.168.44.12:9098/hos", "default", "default"); if (handle == NULL) { printf("error:hos_client_handle\n"); return -1; } + printf("hos_client_init success ..."); - hos_create_bucket(handle, "my-first-bucket"); + printf("hos_create_bucket start ... \n"); + if(!hos_create_bucket(handle, bucket)) + { + printf("hos_create_bucket failed ... \n"); + } + printf("hos_create_bucket success ... \n"); - hos_upload_async(handle, "my-first-bucket", "my-file.txt"); + printf("hos_upload_async start ...\n"); + hos_upload_async(handle, bucket, "my-file.txt"); + //sleep(30); hos_client_close(handle); return 0; diff --git a/src/hos_client.cpp b/src/hos_client.cpp index ac8261c1..f2a092e2 100644 --- a/src/hos_client.cpp +++ b/src/hos_client.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -26,51 +27,63 @@ static void PutObjectAsyncFinished(const Aws::S3::S3Client* s3Client, } else { std::cout << "Error: PutObjectAsyncFinished: " << - outcome.GetError().GetMessage() << std::endl; + outcome.GetError() << std::endl; } } hos_client_handle hos_client_init(const char *endpoint, const char *accesskeyid, const char *secretkey) { + if (!endpoint || !accesskeyid || !secretkey) + { + return NULL; + } 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; + //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); + handle = new Aws::S3::S3Client(credentials, config, Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Never, false); return handle; } -bool hos_create_bucket(hos_client_handle handle, const char *bucket) +int hos_create_bucket(hos_client_handle handle, const char *bucket) { + if (!bucket) + { + return -1; + } 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; + //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; + Aws::S3::S3Errors errorcode = createBucketOutcome.GetError().GetErrorType(); + if (errorcode != Aws::S3::S3Errors::BUCKET_ALREADY_OWNED_BY_YOU) + { + //std::cout << "Failed to create bucket: " << bucket << "\n" << createBucketOutcome.GetError() << std::endl; + return errorcode; + } } - return true; + return 0; } bool hos_upload_async(hos_client_handle handle, const char *bucket, const char *object) { - const Aws::S3::S3Client &s3Client = (Aws::S3::S3Client&) handle; + Aws::S3::S3Client& s3Client = *(Aws::S3::S3Client *) handle; struct stat buffer; std::unique_lock lock(upload_mutex); @@ -85,7 +98,15 @@ bool hos_upload_async(hos_client_handle handle, const char *bucket, const char * request.SetBucket(bucket); request.SetKey(object); - s3Client.PutObjectAsync(request, PutObjectAsyncFinished); + const std::shared_ptr input_data = + Aws::MakeShared("SampleAllocationTag", object, std::ios_base::in | std::ios_base::binary); + + request.SetBody(input_data); + + std::shared_ptr context = + Aws::MakeShared("PutObjectAllocationTag"); + context->SetUUID(object); + s3Client.PutObjectAsync(request, PutObjectAsyncFinished, context); return true; } @@ -96,7 +117,7 @@ void hos_client_close(hos_client_handle handle) return; } - delete (Aws::S3::S3Client*)&handle; + delete (Aws::S3::S3Client *)handle; return ; } diff --git a/src/hos_client.h b/src/hos_client.h index e38e9794..3eb38457 100644 --- a/src/hos_client.h +++ b/src/hos_client.h @@ -6,10 +6,84 @@ #ifndef __HOS_CLIENT_INIT__ #define __HOS_CLIENT_INIT__ +/*hos client 句柄*/ typedef void* hos_client_handle; +#define HOS_CLINET_OK 0 +#define HOS_CLIENT_ERR -1 + +/* s3 的错误码 */ +enum s3errors +{ + //From Core// + ////////////////////////////////////////////////////////////////////////////////////////// + INCOMPLETE_SIGNATURE = 0, + INTERNAL_FAILURE = 1, + INVALID_ACTION = 2, + INVALID_CLIENT_TOKEN_ID = 3, + INVALID_PARAMETER_COMBINATION = 4, + INVALID_QUERY_PARAMETER = 5, + INVALID_PARAMETER_VALUE = 6, + MISSING_ACTION = 7, // SDK should never allow + MISSING_AUTHENTICATION_TOKEN = 8, // SDK should never allow + MISSING_PARAMETER = 9, // SDK should never allow + OPT_IN_REQUIRED = 10, + REQUEST_EXPIRED = 11, + SERVICE_UNAVAILABLE = 12, + THROTTLING = 13, + VALIDATION = 14, + ACCESS_DENIED = 15, + RESOURCE_NOT_FOUND = 16, // Shared with multiple services + UNRECOGNIZED_CLIENT = 17, // Most likely caused by an invalid access key or secret key + MALFORMED_QUERY_STRING = 18, + SLOW_DOWN = 19, + REQUEST_TIME_TOO_SKEWED = 20, + INVALID_SIGNATURE = 21, + SIGNATURE_DOES_NOT_MATCH = 22, + INVALID_ACCESS_KEY_ID = 23, + REQUEST_TIMEOUT = 24, + NETWORK_CONNECTION = 99, // General failure to send message to service + + // These are needed for logical reasons + UNKNOWN = 100, + CLIENT_SIGNING_FAILURE = 101, // Client failed to sign the request + USER_CANCELLED = 102, // User cancelled the request + SERVICE_EXTENSION_START_RANGE = 128 + BUCKET_ALREADY_EXISTS= SERVICE_EXTENSION_START_RANGE + 1, + BUCKET_ALREADY_OWNED_BY_YOU, + NO_SUCH_BUCKET, + NO_SUCH_KEY, + NO_SUCH_UPLOAD, + OBJECT_ALREADY_IN_ACTIVE_TIER, + OBJECT_NOT_IN_ACTIVE_TIER +}; + +/************************************************************************************* + * 函数名: hos_client_init + * 参数: const char *endpoint 目的地址,如”http://192.168.44.12:9098/hos“ + * const char *accesskeyid AWS access key ID,如”default“ + * const char *secretkey AWS secret key,如”secretkey“ + * 返回值: 成功返回一个非空句柄,失败返回NULL。(失败原因都是因为输入参数为空) +*************************************************************************************/ hos_client_handle hos_client_init(const char *endpoint, const char *accesskeyid, const char *secretkey); +/************************************************************************************* + * 函数名: hos_create_bucket + * 参数: hos_client_handle handle 非空句柄 + * const char * bucket 桶名称 + * 返回值: bool +*************************************************************************************/ bool hos_create_bucket(hos_client_handle handle, const char *bucket); +/************************************************************************************* + * 函数名: hos_upload_async + * 参数: hos_client_handle handle 非空句柄 + * const char * bucket 桶名称 + * const char * object 上传对象名称 + * 返回值 bool +*************************************************************************************/ bool hos_upload_async(hos_client_handle handle, const char *bucket, const char *object); +/************************************************************************************* + * 函数名: hos_client_close + * 参数: hos_client_handle handle 非空句柄 +*************************************************************************************/ void hos_client_close(hos_client_handle handle); #endif