1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include <zencore/except.h>
#include <zencore/filesystem.h>
#include <zencore/logging.h>
#include <zencore/sha1.h>
#if ZEN_WITH_TESTS
# define ZEN_TEST_WITH_RUNNER 1
# include <zencore/testing.h>
#endif
#if ZEN_USE_MIMALLOC
# include <mimalloc-new-delete.h>
#endif
ZEN_THIRD_PARTY_INCLUDES_START
#include <spdlog/sinks/ansicolor_sink.h>
#include <spdlog/spdlog.h>
ZEN_THIRD_PARTY_INCLUDES_END
ZEN_THIRD_PARTY_INCLUDES_START
#include <cxxopts.hpp>
ZEN_THIRD_PARTY_INCLUDES_END
ZEN_THIRD_PARTY_INCLUDES_START
#include <aws/core/Aws.h>
#include <aws/core/utils/HashingUtils.h>
#include <aws/core/utils/UUID.h>
#include <aws/core/utils/logging/CRTLogSystem.h>
#include <aws/core/utils/memory/stl/AWSStringStream.h>
#include <aws/s3-crt/S3CrtClient.h>
#include <aws/s3-crt/model/BucketLocationConstraint.h>
#include <aws/s3-crt/model/CreateBucketRequest.h>
#include <aws/s3-crt/model/DeleteBucketRequest.h>
#include <aws/s3-crt/model/DeleteObjectRequest.h>
#include <aws/s3-crt/model/GetObjectRequest.h>
#include <aws/s3-crt/model/PutObjectRequest.h>
ZEN_THIRD_PARTY_INCLUDES_END
#include <cstdlib>
#include <fstream>
#include <iostream>
static const char ALLOCATION_TAG[] = "s3-crt-demo";
// List all Amazon Simple Storage Service (Amazon S3) buckets under the account.
bool
ListBuckets(const Aws::S3Crt::S3CrtClient& s3CrtClient)
{
Aws::S3Crt::Model::ListBucketsOutcome outcome = s3CrtClient.ListBuckets();
if (outcome.IsSuccess())
{
std::cout << "All buckets under my account:" << std::endl;
for (auto const& bucket : outcome.GetResult().GetBuckets())
{
std::cout << " * " << bucket.GetName() << std::endl;
}
std::cout << std::endl;
return true;
}
else
{
std::cout << "ListBuckets error:\n" << outcome.GetError() << std::endl << std::endl;
return false;
}
}
// Create an Amazon Simple Storage Service (Amazon S3) bucket.
bool
CreateBucket(const Aws::S3Crt::S3CrtClient& s3CrtClient,
const Aws::String& bucketName,
const Aws::S3Crt::Model::BucketLocationConstraint& locConstraint)
{
std::cout << "Creating bucket: \"" << bucketName << "\" ..." << std::endl;
Aws::S3Crt::Model::CreateBucketRequest request;
request.SetBucket(bucketName);
// If you don't specify an AWS Region, the bucket is created in the US East (N. Virginia) Region (us-east-1)
if (locConstraint != Aws::S3Crt::Model::BucketLocationConstraint::us_east_1)
{
Aws::S3Crt::Model::CreateBucketConfiguration bucket_config;
bucket_config.SetLocationConstraint(locConstraint);
request.SetCreateBucketConfiguration(bucket_config);
}
Aws::S3Crt::Model::CreateBucketOutcome outcome = s3CrtClient.CreateBucket(request);
if (outcome.IsSuccess())
{
std::cout << "Bucket created." << std::endl << std::endl;
return true;
}
else
{
std::cout << "CreateBucket error:\n" << outcome.GetError() << std::endl << std::endl;
return false;
}
}
// Delete an existing Amazon S3 bucket.
bool
DeleteBucket(const Aws::S3Crt::S3CrtClient& s3CrtClient, const Aws::String& bucketName)
{
std::cout << "Deleting bucket: \"" << bucketName << "\" ..." << std::endl;
Aws::S3Crt::Model::DeleteBucketRequest request;
request.SetBucket(bucketName);
Aws::S3Crt::Model::DeleteBucketOutcome outcome = s3CrtClient.DeleteBucket(request);
if (outcome.IsSuccess())
{
std::cout << "Bucket deleted." << std::endl << std::endl;
return true;
}
else
{
std::cout << "DeleteBucket error:\n" << outcome.GetError() << std::endl << std::endl;
return false;
}
}
// Put an Amazon S3 object to the bucket.
bool
PutObject(const Aws::S3Crt::S3CrtClient& s3CrtClient,
const Aws::String& bucketName,
const Aws::String& objectKey,
const Aws::String& fileName)
{
std::cout << "Putting object: \"" << objectKey << "\" to bucket: \"" << bucketName << "\" ..." << std::endl;
Aws::S3Crt::Model::PutObjectRequest request;
std::shared_ptr<Aws::IOStream> bodyStream =
Aws::MakeShared<Aws::FStream>(ALLOCATION_TAG, fileName.c_str(), std::ios_base::in | std::ios_base::binary);
if (!bodyStream->good())
{
std::cout << "Failed to open file: \"" << fileName << "\"." << std::endl << std::endl;
return false;
}
request.SetBucket(bucketName);
request.SetKey(objectKey);
request.SetBody(bodyStream);
// A PUT operation turns into a multipart upload using the s3-crt client.
// https://github.com/aws/aws-sdk-cpp/wiki/Improving-S3-Throughput-with-AWS-SDK-for-CPP-v1.9
Aws::S3Crt::Model::PutObjectOutcome outcome = s3CrtClient.PutObject(request);
if (outcome.IsSuccess())
{
std::cout << "Object added." << std::endl << std::endl;
return true;
}
else
{
std::cout << "PutObject error:\n" << outcome.GetError() << std::endl << std::endl;
return false;
}
}
// Get the Amazon S3 object from the bucket.
bool
GetObject(const Aws::S3Crt::S3CrtClient& s3CrtClient, const Aws::String& bucketName, const Aws::String& objectKey)
{
std::cout << "Getting object: \"" << objectKey << "\" from bucket: \"" << bucketName << "\" ..." << std::endl;
Aws::S3Crt::Model::GetObjectRequest request;
request.SetBucket(bucketName);
request.SetKey(objectKey);
Aws::S3Crt::Model::GetObjectOutcome outcome = s3CrtClient.GetObject(request);
if (outcome.IsSuccess())
{
// Uncomment this line if you wish to have the contents of the file displayed. Not recommended for large files
// because it takes a while.
// std::cout << "Object content: " << outcome.GetResult().GetBody().rdbuf() << std::endl << std::endl;
return true;
}
else
{
std::cout << "GetObject error:\n" << outcome.GetError() << std::endl << std::endl;
return false;
}
}
// Delete the Amazon S3 object from the bucket.
bool
DeleteObject(const Aws::S3Crt::S3CrtClient& s3CrtClient, const Aws::String& bucketName, const Aws::String& objectKey)
{
std::cout << "Deleting object: \"" << objectKey << "\" from bucket: \"" << bucketName << "\" ..." << std::endl;
Aws::S3Crt::Model::DeleteObjectRequest request;
request.SetBucket(bucketName);
request.SetKey(objectKey);
Aws::S3Crt::Model::DeleteObjectOutcome outcome = s3CrtClient.DeleteObject(request);
if (outcome.IsSuccess())
{
std::cout << "Object deleted." << std::endl << std::endl;
return true;
}
else
{
std::cout << "DeleteObject error:\n" << outcome.GetError() << std::endl << std::endl;
return false;
}
}
//////////////////////////////////////////////////////////////////////////
// TODO: should make this Unicode-aware so we can pass anything in on the
// command line.
struct ZenCloudOptions
{
bool IsDebug = false;
bool IsVerbose = false;
bool IsLocal = false;
bool IsTest = false;
std::string TestDataDirectory;
// Arguments after " -- " on command line are passed through and not parsed
std::string PassthroughCommandLine;
std::string PassthroughArgs;
std::vector<std::string> PassthroughArgV;
};
int
DoWork(ZenCloudOptions& GlobalOptions)
{
using namespace std::literals;
using namespace zen;
// AWS SDK setup
Aws::SDKOptions options;
options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Info;
Aws::InitAPI(options);
{
// TODO: Set to your account AWS Region.
Aws::String region = Aws::Region::EU_NORTH_1; // US_EAST_1;
// The object_key is the unique identifier for the object in the bucket.
Aws::String object_key = "my-object";
// Create a globally unique name for the new bucket.
// Format: "my-bucket-" + lowercase UUID.
Aws::String uuid = Aws::Utils::UUID::RandomUUID();
Aws::String bucket_name = "my-bucket-" + Aws::Utils::StringUtils::ToLower(uuid.c_str());
const double throughput_target_gbps = 10;
const uint64_t part_size = 32 * 1024 * 1024;
Aws::S3Crt::ClientConfiguration config;
config.region = region;
config.throughputTargetGbps = throughput_target_gbps;
config.partSize = part_size;
// config.httpLibOverride = Aws::Http::TransferLibType::CURL_CLIENT;
std::vector<std::string_view> Tokens;
const char* AwsAccessEnv = std::getenv("ZEN_AWS_ACCESS");
if (AwsAccessEnv && !GlobalOptions.IsLocal)
{
zen::ForEachStrTok(AwsAccessEnv, ':', [&](const std::string_view& Token) {
Tokens.push_back(Token);
return true;
});
}
else
{
Tokens.push_back("zencloud-test"sv);
Tokens.push_back("misterblobby"sv);
config.endpointOverride = "http://127.0.0.1:9000";
}
ZEN_ASSERT(Tokens.size() == 2);
Aws::Auth::AWSCredentials credentials;
credentials.SetAWSAccessKeyId(Aws::String(Tokens[0]));
credentials.SetAWSSecretKey(Aws::String(Tokens[1]));
ZEN_CONSOLE("using credentials: {}/{}", Tokens[0], Tokens[1]);
Aws::S3Crt::S3CrtClient s3_crt_client(credentials, config);
// Use BucketLocationConstraintMapper to get the BucketLocationConstraint enum from the region string.
// https://sdk.amazonaws.com/cpp/api/0.14.3/namespace_aws_1_1_s3_1_1_model_1_1_bucket_location_constraint_mapper.html#a50d4503d3f481022f969eff1085cfbb0
Aws::S3Crt::Model::BucketLocationConstraint locConstraint =
Aws::S3Crt::Model::BucketLocationConstraintMapper::GetBucketLocationConstraintForName(region);
ListBuckets(s3_crt_client);
CreateBucket(s3_crt_client, bucket_name, locConstraint);
Aws::String file_name = GlobalOptions.TestDataDirectory;
PutObject(s3_crt_client, bucket_name, object_key, file_name);
GetObject(s3_crt_client, bucket_name, object_key);
DeleteObject(s3_crt_client, bucket_name, object_key);
DeleteBucket(s3_crt_client, bucket_name);
}
Aws::ShutdownAPI(options);
return 0;
}
int
main(int argc, char** argv)
{
using namespace zen;
using namespace std::literals;
ZEN_UNUSED(argc, argv);
#if ZEN_USE_MIMALLOC
mi_version();
#endif
zen::logging::InitializeLogging();
// Set output mode to handle virtual terminal sequences
zen::logging::EnableVTMode();
std::set_terminate([]() { ZEN_CRITICAL("Program exited abnormally via std::terminate()"); });
LoggerRef DefaultLogger = zen::logging::Default();
auto& Sinks = DefaultLogger.SpdLogger->sinks();
Sinks.clear();
auto ConsoleSink = std::make_shared<spdlog::sinks::ansicolor_stdout_sink_mt>();
Sinks.push_back(ConsoleSink);
zen::MaximizeOpenFileCount();
// Split command line into options, commands and any pass-through arguments
std::string Passthrough;
std::string PassthroughArgs;
std::vector<std::string> PassthroughArgV;
for (int i = 1; i < argc; ++i)
{
if ("--"sv == argv[i])
{
bool IsFirst = true;
ExtendableStringBuilder<256> Line;
ExtendableStringBuilder<256> Arguments;
for (int j = i + 1; j < argc; ++j)
{
auto AppendAscii = [&](auto X) {
Line.Append(X);
if (!IsFirst)
{
Arguments.Append(X);
}
};
if (!IsFirst)
{
AppendAscii(" ");
}
std::string_view ThisArg(argv[j]);
PassthroughArgV.push_back(std::string(ThisArg));
const bool NeedsQuotes = (ThisArg.find(' ') != std::string_view::npos);
if (NeedsQuotes)
{
AppendAscii("\"");
}
AppendAscii(ThisArg);
if (NeedsQuotes)
{
AppendAscii("\"");
}
IsFirst = false;
}
Passthrough = Line.c_str();
PassthroughArgs = Arguments.c_str();
// This will "truncate" the arg vector and terminate the loop
argc = i;
}
}
ZenCloudOptions GlobalOptions;
cxxopts::Options Options("zencloud", "Zen cloud interface");
Options.add_options()("d, debug", "Enable debugging", cxxopts::value<bool>(GlobalOptions.IsDebug));
Options.add_options()("v, verbose", "Enable verbose logging", cxxopts::value<bool>(GlobalOptions.IsVerbose));
Options.add_options()("help", "Show command line help");
Options.add_options()("local", "Use local server (such as minio)", cxxopts::value<bool>(GlobalOptions.IsLocal));
Options.add_options()("t, test", "Run tests", cxxopts::value<bool>(GlobalOptions.IsTest));
Options.add_options()("data", "Test data path", cxxopts::value<std::string>(GlobalOptions.TestDataDirectory));
const bool IsNullInvoke = (argc == 1); // If no arguments are passed we want to print usage information
try
{
cxxopts::ParseResult ParseResult = Options.parse(argc, argv);
if (ParseResult.count("help") || IsNullInvoke == 1)
{
std::string Help = Options.help();
printf("%s\n", Help.c_str());
exit(0);
}
if (GlobalOptions.IsDebug)
{
logging::SetLogLevel(logging::level::Debug);
}
if (GlobalOptions.IsVerbose)
{
logging::SetLogLevel(logging::level::Trace);
}
if (GlobalOptions.IsTest)
{
DoWork(GlobalOptions);
}
}
catch (const OptionParseException& Ex)
{
std::string HelpMessage = Options.help();
printf("Error parsing program arguments: %s\n\n%s", Ex.what(), HelpMessage.c_str());
return 9;
}
catch (const std::system_error& Ex)
{
printf("System Error: %s\n", Ex.what());
return Ex.code() ? Ex.code().value() : 10;
}
catch (const std::exception& Ex)
{
printf("Error: %s\n", Ex.what());
return 11;
}
return 0;
}
|