Created
September 18, 2019 08:47
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use following in
CMakeLists.txt
: