#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace aws::lambda_runtime; char const TAG[] = "LAMBDA_ALLOC"; enum class Status { Success, Failure }; std::string download_file(Aws::S3::S3Client const& client, Aws::String const& bucket, Aws::String const& key, Aws::String & output) { Aws::S3::Model::GetObjectRequest request; request.WithBucket(bucket).WithKey(key); auto outcome = client.GetObject(request); if (outcome.IsSuccess()) { AWS_LOGSTREAM_INFO(TAG, "Download completed!"); Aws::StringStream ss; ss << outcome.GetResult().GetBody().rdbuf(); output = ss.str(); return ""; }else { AWS_LOGSTREAM_ERROR(TAG, "Failed with error: " << outcome.GetError()); return outcome.GetError().GetMessage(); } } static invocation_response getResponse(Status status, const std::string& body) { Aws::Utils::Json::JsonValue response; response.WithString("body", body.c_str()).WithInteger("statusCode", 200); std::string payload = response.View().WriteCompact(); if (status == Status::Failure) return invocation_response::success(payload, ""); else return invocation_response::success(payload, "application/json"); } static invocation_response my_handler(invocation_request const& req, Aws::S3::S3Client const& client) { Aws::Utils::Json::JsonValue json(req.payload); if (!json.WasParseSuccessful()) { return getResponse(Status::Failure, "InvalidJSON: Failed to parse input JSON"); } return getResponse(Status::Failure, a); auto query = json.View().GetObject("queryStringParameters"); if (!query.ValueExists("s3bucket") || !query.GetObject("s3bucket").IsString() || !query.ValueExists("s3key") || !query.GetObject("s3key").IsString()) { return getResponse(Status::Failure, "InvalidJSON: Missing input value s3bucket or s3key"); } auto bucket = query.GetString("s3bucket"); auto key = query.GetString("s3key"); AWS_LOGSTREAM_INFO(TAG, "Attempting to download file from s3://" << bucket << "/" << key); Aws::String file; auto err = download_file(client, bucket, key, file); if (!err.empty()) { return getResponse(Status::Failure, std::string("DownloadFailure: ") + err); } return getResponse(Status::Success, file); } std::function()> GetConsoleLoggerFactory() { return [] { return Aws::MakeShared( "console_logger", Aws::Utils::Logging::LogLevel::Trace); }; } int main() { using namespace Aws; SDKOptions options; options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Info; 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"; auto credentialsProvider = Aws::MakeShared(TAG); S3::S3Client client(credentialsProvider, 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; }