-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathmain.cpp
127 lines (108 loc) · 4.07 KB
/
main.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
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
#include <aws/core/Aws.h>
#include <aws/core/utils/logging/LogLevel.h>
#include <aws/core/utils/logging/ConsoleLogSystem.h>
#include <aws/core/utils/logging/LogMacros.h>
#include <aws/core/utils/json/JsonSerializer.h>
#include <aws/core/utils/HashingUtils.h>
#include <aws/core/platform/Environment.h>
#include <aws/core/client/ClientConfiguration.h>
#include <aws/core/auth/AWSCredentialsProvider.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/GetObjectRequest.h>
#include <aws/lambda-runtime/runtime.h>
#include <iostream>
#include <memory>
using namespace aws::lambda_runtime;
std::string download_and_encode_file(
Aws::S3::S3Client const& client,
Aws::String const& bucket,
Aws::String const& key,
Aws::String& encoded_output);
std::string encode(Aws::String const& filename, Aws::String& output);
char const TAG[] = "LAMBDA_ALLOC";
static invocation_response my_handler(invocation_request const& req, Aws::S3::S3Client const& client)
{
using namespace Aws::Utils::Json;
JsonValue json(req.payload);
if (!json.WasParseSuccessful()) {
return invocation_response::failure("Failed to parse input JSON", "InvalidJSON");
}
auto v = json.View();
if (!v.ValueExists("s3bucket") || !v.ValueExists("s3key") || !v.GetObject("s3bucket").IsString() ||
!v.GetObject("s3key").IsString()) {
return invocation_response::failure("Missing input value s3bucket or s3key", "InvalidJSON");
}
auto bucket = v.GetString("s3bucket");
auto key = v.GetString("s3key");
AWS_LOGSTREAM_INFO(TAG, "Attempting to download file from s3://" << bucket << "/" << key);
Aws::String base64_encoded_file;
auto err = download_and_encode_file(client, bucket, key, base64_encoded_file);
if (!err.empty()) {
return invocation_response::failure(err, "DownloadFailure");
}
return invocation_response::success(std::move(base64_encoded_file), "application/base64");
}
std::function<std::shared_ptr<Aws::Utils::Logging::LogSystemInterface>()> GetConsoleLoggerFactory()
{
return [] {
return Aws::MakeShared<Aws::Utils::Logging::ConsoleLogSystem>(
"console_logger", Aws::Utils::Logging::LogLevel::Trace);
};
}
int main()
{
using namespace Aws;
SDKOptions options;
options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Trace;
options.loggingOptions.logger_create_fn = GetConsoleLoggerFactory();
InitAPI(options);
{
Client::ClientConfiguration config;
config.region = Aws::Environment::GetEnv("AWS_REGION");
config.caFile = "/etc/pki/tls/certs/ca-bundle.crt";
S3::S3Client client(config);
auto handler_fn = [&client](aws::lambda_runtime::invocation_request const& req) {
return my_handler(req, client);
};
run_handler(handler_fn);
}
ShutdownAPI(options);
return 0;
}
std::string encode(Aws::IOStream& stream, Aws::String& output)
{
Aws::Vector<unsigned char> bits;
bits.reserve(stream.tellp());
stream.seekg(0, stream.beg);
char streamBuffer[1024 * 4];
while (stream.good()) {
stream.read(streamBuffer, sizeof(streamBuffer));
auto bytesRead = stream.gcount();
if (bytesRead > 0) {
bits.insert(bits.end(), (unsigned char*)streamBuffer, (unsigned char*)streamBuffer + bytesRead);
}
}
Aws::Utils::ByteBuffer bb(bits.data(), bits.size());
output = Aws::Utils::HashingUtils::Base64Encode(bb);
return {};
}
std::string download_and_encode_file(
Aws::S3::S3Client const& client,
Aws::String const& bucket,
Aws::String const& key,
Aws::String& encoded_output)
{
using namespace Aws;
S3::Model::GetObjectRequest request;
request.WithBucket(bucket).WithKey(key);
auto outcome = client.GetObject(request);
if (outcome.IsSuccess()) {
AWS_LOGSTREAM_INFO(TAG, "Download completed!");
auto& s = outcome.GetResult().GetBody();
return encode(s, encoded_output);
}
else {
AWS_LOGSTREAM_ERROR(TAG, "Failed with error: " << outcome.GetError());
return outcome.GetError().GetMessage();
}
}