Skip to content

Instantly share code, notes, and snippets.

@jrdi
Last active May 3, 2024 17:55
Show Gist options
  • Save jrdi/50a6096a005bba52b67ad666614c8511 to your computer and use it in GitHub Desktop.
Save jrdi/50a6096a005bba52b67ad666614c8511 to your computer and use it in GitHub Desktop.
cmake_minimum_required(VERSION 3.13)
# Set the AWS service components used by this project.
set(SERVICE_COMPONENTS s3)
# Set this project's name.
project("hello_s3")
# Set the C++ standard to use to build this target.
# At least C++ 11 is required for the AWS SDK for C++.
set(CMAKE_CXX_STANDARD 11)
# Find the AWS SDK for C++ package.
find_package(AWSSDK REQUIRED COMPONENTS ${SERVICE_COMPONENTS})
add_executable(${PROJECT_NAME}
hello_s3.cpp)
target_link_libraries(${PROJECT_NAME}
${AWSSDK_LINK_LIBRARIES})
#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <iostream>
#include <fstream>
#include <aws/core/auth/AWSCredentialsProviderChain.h>
#include <aws/s3/model/GetObjectRequest.h>
#include <aws/s3/model/GetObjectResult.h>
using namespace Aws;
using namespace Aws::Auth;
int main(int argc, char **argv) {
Aws::SDKOptions options;
// Optionally change the log level for debugging.
Aws::InitAPI(options); // Should only be called once.
{
Aws::Client::ClientConfiguration clientConfig;
Aws::S3::S3Client s3Client(clientConfig);
Aws::S3::Model::GetObjectRequest request;
request.SetBucket("jordi-cache");
request.SetKey("test.csv");
Aws::S3::Model::GetObjectOutcome outcome =
s3Client.GetObject(request);
if (!outcome.IsSuccess()) {
const Aws::S3::S3Error& err = outcome.GetError();
std::cerr << "Error: GetObject: " <<
err.GetExceptionName() << ": " << err.GetMessage() << std::endl;
}
else {
std::cout << "Successfully retrieved" << std::endl;
std::istream & istr = outcome.GetResult().GetBody();
istr.seekg(0, istr.end);
int length = istr.tellg();
istr.seekg(0, istr.beg);
char * buffer = new char[length];
std::cout << "Reading " << length << " characters... " << std::endl;
istr.read(buffer, length);
std::ifstream f("/proc/thread-self/io");
if (f.is_open())
std::cout << f.rdbuf();
}
}
Aws::ShutdownAPI(options); // Should only be called once.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment