Skip to content

Instantly share code, notes, and snippets.

@genuinelucifer
Created September 18, 2019 08:47
Show Gist options
  • Save genuinelucifer/32e2e29aeafd8f7a06facc96a6062be1 to your computer and use it in GitHub Desktop.
Save genuinelucifer/32e2e29aeafd8f7a06facc96a6062be1 to your computer and use it in GitHub Desktop.
Downloads a .json.gz file from S3, uncompresses it and converts it to json using nlohmann::json
#pragma once
#include <aws/core/Aws.h>
#include <aws/core/client/ClientConfiguration.h>
#include <aws/core/auth/AWSCredentialsProviderChain.h>
#include <aws/core/utils/logging/LogLevel.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/GetObjectRequest.h>
#include <iostream>
#include <fstream>
#include <memory>
// decompress.hpp from https://github.com/mapbox/gzip-hpp/blob/master/include/gzip/
#include "decompress.hpp"
// nlohmann::json lib
#include "json.hpp"
using json = nlohmann::json;
using std::clog, std::endl;
using namespace Aws;
// Uncompressed gzipped string from stream
std::string uncompressed_string_from_stream(std::basic_iostream<char>& stream)
{
// Read data from the stream
stream.seekg (0, stream.end);
int length = stream.tellg();
stream.seekg (0, stream.beg);
char * buffer = new char [length];
stream.read (buffer,length);
// Uncompress the data
std::string result = gzip::decompress(buffer, length);
// Delete the buffer and return the result
delete buffer;
return result;
}
// Downloads a .json.gz file from S3, uncompresses it and converts it to json using nlohmann::json
json download_json(String profile, String region, String bucket, String key)//, String local_path)
{
json json_result = nullptr;
SDKOptions options;
InitAPI(options);
{
Client::ClientConfiguration config;
config.region = region;
config.scheme = Http::Scheme::HTTPS;
auto credentials = Auth::ProfileConfigFileAWSCredentialsProvider(profile.c_str()).GetAWSCredentials();
auto client = S3::S3Client(credentials, config);
S3::Model::GetObjectRequest request;
request.WithBucket(bucket).WithKey(key);
//request.SetResponseStreamFactory([local_path] { return new std::fstream(local_path.c_str(), std::ios_base::out); });
auto get_object_outcome = client.GetObject(request);
if (get_object_outcome.IsSuccess())
{
auto& result_stream = get_object_outcome.GetResult().GetBody();
auto result = uncompressed_string_from_stream(result_stream);
json_result = json::parse(result);
}
else
{
clog << "GetObject error: " <<
get_object_outcome.GetError().GetExceptionName() << " " <<
get_object_outcome.GetError().GetMessage() << std::endl;
}
}
ShutdownAPI(options);
return json_result;
}
@genuinelucifer
Copy link
Author

Use following in CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)
project(projname)
find_package(AWSSDK REQUIRED COMPONENTS s3)
find_package( ZLIB REQUIRED )
set(CMAKE_CXX_STANDARD 17)
add_executable(projname "projname.cpp")
# list all deps for static linking
target_link_libraries(projname ${AWSSDK_LINK_LIBRARIES} ${ZLIB_LIBRARIES})
target_compile_options(projname PRIVATE "-Wall" "-Werror")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment