-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paths3-demo.cpp
69 lines (58 loc) · 1.78 KB
/
s3-demo.cpp
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
// Based on: https://docs.aws.amazon.com/cloud9/latest/user-guide/sample-cplusplus.html
#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/Bucket.h>
bool ListBuckets(const Aws::S3::S3Client &s3Client)
{
Aws::S3::Model::ListBucketsOutcome outcome = s3Client.ListBuckets();
Aws::String owner = outcome.GetResult().GetOwner().GetDisplayName();
if (outcome.IsSuccess() && owner != "")
{
Aws::Vector<Aws::S3::Model::Bucket> bucket_list =
outcome.GetResult().GetBuckets();
for (Aws::S3::Model::Bucket const &bucket : bucket_list)
{
std::cout << bucket.GetName() << std::endl;
}
return true;
}
else
{
Aws::String s3_error = outcome.GetError().GetMessage();
if (s3_error != "")
{
std::cout << "ListBuckets error: "
<< outcome.GetError().GetMessage() << std::endl;
}
else
{
std::cout << "Unknown error: Might be related to authentication" << std::endl;
}
}
return false;
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
std::cout << "Usage: s3-demo <AWS Region>" << std::endl
<< "Example: s3-demo us-east-1" << std::endl;
return false;
}
Aws::SDKOptions options;
// options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Info;
Aws::InitAPI(options);
{
Aws::String region = argv[1];
Aws::Client::ClientConfiguration config;
config.region = region;
Aws::S3::S3Client s3_client(config);
if (!ListBuckets(s3_client))
{
std::cout << "Failed to list buckets" << std::endl;
return 1;
}
}
Aws::ShutdownAPI(options);
return 0;
}