2020-09-11 16:13:02 +08:00
|
|
|
/*************************************************************************
|
|
|
|
|
> 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>
|
2020-09-14 19:19:50 +08:00
|
|
|
#include <fstream>
|
2020-09-11 16:13:02 +08:00
|
|
|
#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: " <<
|
2020-09-14 19:19:50 +08:00
|
|
|
outcome.GetError() << std::endl;
|
2020-09-11 16:13:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hos_client_handle hos_client_init(const char *endpoint, const char *accesskeyid, const char *secretkey)
|
|
|
|
|
{
|
2020-09-14 19:19:50 +08:00
|
|
|
if (!endpoint || !accesskeyid || !secretkey)
|
|
|
|
|
{
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2020-09-11 16:13:02 +08:00
|
|
|
Aws::SDKOptions options;
|
|
|
|
|
Aws::InitAPI(options);
|
|
|
|
|
|
|
|
|
|
hos_client_handle handle = NULL;
|
|
|
|
|
Aws::Client::ClientConfiguration config;
|
|
|
|
|
Aws::Auth::AWSCredentials credentials(accesskeyid, secretkey);
|
2020-09-14 19:19:50 +08:00
|
|
|
//std::cout << "accesskeyid: " << credentials.GetAWSAccessKeyId() << "\n" << std::endl;
|
|
|
|
|
//std::cout << "secretkey: " << credentials.GetAWSSecretKey() << "\n" << std::endl;
|
2020-09-11 16:13:02 +08:00
|
|
|
|
|
|
|
|
config.endpointOverride = endpoint;
|
|
|
|
|
config.verifySSL = false;
|
|
|
|
|
config.enableEndpointDiscovery = true;
|
|
|
|
|
|
2020-09-14 19:19:50 +08:00
|
|
|
handle = new Aws::S3::S3Client(credentials, config, Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Never, false);
|
2020-09-11 16:13:02 +08:00
|
|
|
return handle;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 19:19:50 +08:00
|
|
|
int hos_create_bucket(hos_client_handle handle, const char *bucket)
|
2020-09-11 16:13:02 +08:00
|
|
|
{
|
2020-09-14 19:19:50 +08:00
|
|
|
if (!bucket)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2020-09-11 16:13:02 +08:00
|
|
|
Aws::S3::S3Client& s3Client = *(Aws::S3::S3Client *) handle;
|
|
|
|
|
Aws::S3::Model::CreateBucketRequest createBucketRequest;
|
|
|
|
|
createBucketRequest.SetBucket(bucket);
|
2020-09-14 19:19:50 +08:00
|
|
|
//std::cout << "bucket name: " << createBucketRequest.GetBucket() << "\n" << std::endl;
|
2020-09-11 16:13:02 +08:00
|
|
|
|
|
|
|
|
Aws::S3::Model::CreateBucketOutcome createBucketOutcome = s3Client.CreateBucket(createBucketRequest);
|
|
|
|
|
|
|
|
|
|
if (!createBucketOutcome.IsSuccess())
|
|
|
|
|
{
|
2020-09-14 19:19:50 +08:00
|
|
|
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;
|
|
|
|
|
}
|
2020-09-11 16:13:02 +08:00
|
|
|
}
|
|
|
|
|
|
2020-09-14 19:19:50 +08:00
|
|
|
return 0;
|
2020-09-11 16:13:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool hos_upload_async(hos_client_handle handle, const char *bucket, const char *object)
|
|
|
|
|
{
|
2020-09-14 19:19:50 +08:00
|
|
|
Aws::S3::S3Client& s3Client = *(Aws::S3::S3Client *) handle;
|
2020-09-11 16:13:02 +08:00
|
|
|
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);
|
|
|
|
|
|
2020-09-14 19:19:50 +08:00
|
|
|
const std::shared_ptr<Aws::IOStream> input_data =
|
|
|
|
|
Aws::MakeShared<Aws::FStream>("SampleAllocationTag", object, std::ios_base::in | std::ios_base::binary);
|
|
|
|
|
|
|
|
|
|
request.SetBody(input_data);
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<Aws::Client::AsyncCallerContext> context =
|
|
|
|
|
Aws::MakeShared<Aws::Client::AsyncCallerContext>("PutObjectAllocationTag");
|
|
|
|
|
context->SetUUID(object);
|
|
|
|
|
s3Client.PutObjectAsync(request, PutObjectAsyncFinished, context);
|
2020-09-11 16:13:02 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void hos_client_close(hos_client_handle handle)
|
|
|
|
|
{
|
|
|
|
|
if (handle == NULL)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 19:19:50 +08:00
|
|
|
delete (Aws::S3::S3Client *)handle;
|
2020-09-11 16:13:02 +08:00
|
|
|
|
|
|
|
|
return ;
|
|
|
|
|
}
|