aboutsummaryrefslogtreecommitdiff
path: root/s3_client.cpp
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2025-09-14 00:30:41 +0800
committerMistivia <i@mistivia.com>2025-09-14 00:30:41 +0800
commit35c8f8e94f0346856130b2a96a7c99790796e53f (patch)
tree8fbf1e5e8b99d8cdfa41765f4a9776d4d891acb6 /s3_client.cpp
parenta4daf467f871b0e77f07f1071b47b960da7bfba9 (diff)
add aws s3 client
Diffstat (limited to 's3_client.cpp')
-rw-r--r--s3_client.cpp83
1 files changed, 83 insertions, 0 deletions
diff --git a/s3_client.cpp b/s3_client.cpp
new file mode 100644
index 0000000..aeb3510
--- /dev/null
+++ b/s3_client.cpp
@@ -0,0 +1,83 @@
+#include "s3_client.h"
+
+#include <fstream>
+#include <iostream>
+
+#include <aws/core/Aws.h>
+#include <aws/s3/S3Client.h>
+#include <aws/s3/model/PutObjectRequest.h>
+#include <aws/s3/model/DeleteObjectRequest.h>
+#include <aws/core/auth/AWSCredentials.h>
+
+#include "config.h"
+
+#include <unistd.h>
+
+namespace {
+
+Aws::S3::S3Client *s3client;
+Aws::SDKOptions aws_options;
+Aws::S3::S3ClientConfiguration config;
+Aws::Auth::AWSCredentials credentials;
+
+}
+
+void S3Client_init() {
+ Aws::InitAPI(aws_options);
+ config.endpointOverride = ezlive_config->endpoint;
+ config.region = ezlive_config->region;
+ credentials = Aws::Auth::AWSCredentials(ezlive_config->access_key, ezlive_config->secret_key);
+ s3client = new Aws::S3::S3Client(credentials, nullptr, config);
+}
+
+void S3Client_put(const char *filename, const char *object_name) {
+ while (1) {
+ Aws::S3::Model::PutObjectRequest request;
+ request.SetBucket(ezlive_config->bucket);
+ //We are using the name of the file as the key for the object in the bucket.
+ //However, this is just a string and can be set according to your retrieval needs.
+ request.SetKey(object_name);
+
+ std::shared_ptr<Aws::IOStream> inputData =
+ std::make_shared<Aws::FStream>(filename, std::ios_base::in | std::ios_base::binary);
+
+ if (!*inputData) {
+ std::cerr << "Error unable to read file " << filename << std::endl;
+ return;
+ }
+
+ request.SetBody(inputData);
+
+ Aws::S3::Model::PutObjectOutcome outcome =
+ s3client->PutObject(request);
+
+ if (!outcome.IsSuccess()) {
+ std::cerr << "Error: putObject: " <<
+ outcome.GetError().GetMessage() << std::endl;
+ sleep(3);
+ continue;
+ } else {
+ std::cout << "Added object '" << object_name << "' to bucket '"
+ << ezlive_config->bucket << "'.";
+ break;
+ }
+ }
+}
+
+void S3Client_delete(const char *object_name) {
+ Aws::S3::Model::DeleteObjectRequest request;
+
+ request.WithKey(object_name)
+ .WithBucket(ezlive_config->bucket);
+
+ Aws::S3::Model::DeleteObjectOutcome outcome =
+ s3client->DeleteObject(request);
+
+ if (!outcome.IsSuccess()) {
+ auto err = outcome.GetError();
+ std::cerr << "Error: deleteObject: " <<
+ err.GetExceptionName() << ": " << err.GetMessage() << std::endl;
+ } else {
+ std::cout << "Successfully deleted the object: " << object_name << std::endl;
+ }
+} \ No newline at end of file