---
# System prepended metadata

title: Scratch Space

---

# Scratch Space

This is used for pasting things like code and messages that are good for reference but would bog down standard documentation

## Messages

Some error messages could go here

## Code Snippets

### Aws-sdk-s3 C++ operation minio for file upload and download
From [here](https://www.programmersought.com/article/2070542330/). Who knows, it could get taken down at any moment, the internet can be fleeting. 

```C++
#pragma once
#include "TDPreDefine.h"
#include <aws/s3/S3Client.h>
#include <aws/core/Aws.h>
#include <aws/core/auth/AWSCredentialsProvider.h>
using namespace Aws::S3;
using namespace Aws::S3::Model;
class TDANALYSIS_EXPORT TDAWSOSSTools{
public:
	TDAWSOSSTools()
	{
		Aws::InitAPI(m_options);
		Aws::Client::ClientConfiguration cfg;
		cfg.endpointOverride = "10.1.3.148:9000"; // S3 server address and port
		cfg.scheme = Aws::Http::Scheme::HTTP;
		cfg.verifySSL = false;
		 //Aws::Auth::AWSCredentials cred("RPW421T9GSIO4A45Y9ZR", "2owKYy9emSS90Q0pXuyqpX1OxBCyEDYodsiBemcq"); // Certified Key
	    m_client = new S3Client(Aws::Auth::AWSCredentials("RPW421T9GSIO4A45Y9ZR", "2owKYy9emSS90Q0pXuyqpX1OxBCyEDYodsiBemcq"), cfg, Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Always,false);
	}
	~TDAWSOSSTools()
	{
		if (m_client != nullptr)
		{
			delete m_client;
			m_client = NULL;
		}
		Aws::ShutdownAPI(m_options);
	}
public:
	bool uploadfile(std::string BucketName, std::string objectKey,std::string pathkey);
	bool downloadfile(std::string BucketName, std::string objectKey, std::string pathkey);
private:
	S3Client * m_client = { NULL };
	Aws::SDKOptions m_options;
};



#include "TDAWSOSSTools.h"
#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/PutObjectRequest.h>
#include <aws/s3/model/GetObjectRequest.h>
#include <iostream>
#include <fstream>
 
bool TDAWSOSSTools::uploadfile(std::string BucketName,std::string objectKey, std::string pathkey)
{
	PutObjectRequest putObjectRequest;
	putObjectRequest.WithBucket(BucketName.c_str()).WithKey(objectKey.c_str());
	auto input_data = Aws::MakeShared<Aws::FStream>("PutObjectInputStream",
		pathkey.c_str(), std::ios_base::in | std::ios_base::binary);
	putObjectRequest.SetBody(input_data);
	auto putObjectResult = m_client->PutObject(putObjectRequest);
	if (putObjectResult.IsSuccess())
	{
		std::cout << "Done!" << std::endl;
		return true;
	}
	else
	{
		/*std::cout << "PutObject error: " <<
			putObjectResult.GetError().GetExceptionName() << " " <<
			putObjectResult.GetError().GetMessage() << std::endl;*/
		return false;
	}
}
bool TDAWSOSSTools::downloadfile(std::string BucketName, std::string objectKey, std::string pathkey)
{
	Aws::S3::Model::GetObjectRequest object_request;
	object_request.WithBucket(BucketName.c_str()).WithKey(objectKey.c_str());
	auto get_object_outcome = m_client->GetObject(object_request);
	if (get_object_outcome.IsSuccess())
	{
	 
		 
		Aws::OFStream local_file;
		local_file.open(pathkey.c_str(), std::ios::out | std::ios::binary);
		local_file << get_object_outcome.GetResult().GetBody().rdbuf();
		std::cout << "Done!" << std::endl;
		return true;
	}
	else
	{
		std::cout << "GetObject error: " <<
			get_object_outcome.GetError().GetExceptionName() << " " <<
			get_object_outcome.GetError().GetMessage() << std::endl;
		return false;
	}
}
```

root app code

```c=
std::string text = "{ \"first\": 1; \"second\": 2}";

    if(!reader.parse(response_string, root)) {
        std::cout << reader.getFormattedErrorMessages() << std::endl;
    }

    Runs basic ROOT app with the arguments given for main
    TRint app("app", &argc, argv);

    Testing. Makes some drawings on a TCanvas
    TCanvas* c = new TCanvas("c", "Something", 0, 0, 800, 600);
    TF1 *f1 = new TF1("f1","sin(x)", -5, 5);
    f1->SetLineColor(kBlue+1);
    f1->SetTitle("My graph;x; sin(x)");
    f1->Draw();
    c->Modified(); c->Update();
    


    Run and finish with ROOT prompt
    app.Run();
```

Code for fetching data buckets from minio

```c++
std::string BucketName = "345974d4-d2ec-49bb-bef2-6683b7e461d5";


    Aws::SDKOptions m_options;
    Aws::S3::S3Client* m_client = { NULL };


    Aws::InitAPI(m_options);
    Aws::Client::ClientConfiguration cfg;
    cfg.endpointOverride = "cmsopendata-minio.servicex.ssl-hep.org";
    cfg.scheme = Aws::Http::Scheme::HTTP;
    cfg.verifySSL = false;
    m_client = new Aws::S3::S3Client(Aws::Auth::AWSCredentials("miniouser", "leftfoot1"), cfg, 
        Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Never, false);
    std::string objectKey = "root:::eospublic.cern.ch::eos:opendata:cms:MonteCarlo2011:Summer11LegDR:SMHiggsToZZTo4L_M-125_7TeV-powheg15-JHUgenV3-pythia6:AODSIM:PU_S13_START53_LV6-v1:20000:08CD3ECC-4C92-E411-B001-0025907B4F20.root";
    std::string pathkey = "temp";
    Aws::S3::Model::PutObjectRequest putObjectRequest;
    putObjectRequest.WithBucket(BucketName.c_str()).WithKey(objectKey.c_str());

    Aws::S3::Model::ListObjectsRequest objRequest;
    objRequest.WithBucket(BucketName);

    auto outcome = m_client->ListObjects(objRequest);

    if (outcome.IsSuccess()) {
        std::cout << "Objects in bucket '" << BucketName << "':" 
            << std::endl << std::endl;

        Aws::Vector<Aws::S3::Model::Object> objects =
            outcome.GetResult().GetContents();

        for (Aws::S3::Model::Object& object : objects)
        {
            std::cout << object.GetKey() << std::endl;
        }

    }
    else
    {
        std::cout << "Error: ListObjects: " <<
            outcome.GetError().GetMessage() << std::endl;

    }

    // Get object
    std::cout << "Getting object\n"; 

    Aws::S3::Model::GetObjectRequest object_request;
    object_request.SetBucket(BucketName);
    object_request.SetKey(objectKey);

    Aws::S3::Model::GetObjectOutcome get_object_outcome = 
        m_client->GetObject(object_request);

    if (get_object_outcome.IsSuccess()) {
        // auto& retrieved_file = get_object_outcome.GetResultWithOwnership().
        //     GetBody();
        // std::cout << typeid(retrieved_file).name() << '\n';

        // // Print a beginning portion of the text file.
        // std::cout << "Beginning of file contents:\n";
        // char file_data[255] = { 0 };
        // retrieved_file.getline(file_data, 254);
        // std::cout << file_data << std::endl;

        std::cout << "attempting save\n";

        // TFile *myFile = new TFile("myfile.root", "CREATE");

        Aws::OFStream local_file;
		local_file.open(objectKey.c_str(), std::ios::out | std::ios::binary);
		local_file << get_object_outcome.GetResult().GetBody().rdbuf();
		std::cout << "Done!" << std::endl;

        // myFile-

        // return true;
    }
    else {
        auto err = get_object_outcome.GetError();
        std::cout << "Error: GetObject: " <<
            err.GetExceptionName() << ": " << err.GetMessage() << std::endl;

        // return false;
    }
```