aws-sdk-cpp part 1

Introduction

In this post, I want to present how to install aws-sdk-cpp on Linux Mint 18 and how to write basic code to work with buckets and objects.
 

Preparation

Software I used:
1. Virtual Box
2. Linux Mint 18 “Sahra” 64 bit
3. CloudBerry Explorer
 
Create new virtual machine on Virtual Box and install Linux Mint 18 on it. New Mint requires additional installations to work with aws, so create prepare.sh file and copy script below into it. Remember to set executable attribute for this file (chmod +x prepare.sh) and run this script with sudo ./prepare.sh or as root. This script will do whole job for you. But keep in mind that compiling aws-sdk-cpp takes (on my machine) 2 hours.
 
prepare.sh

 

Hello, world !

Now you're ready to code. Let's create "hello world". Create hello_aws.cpp file and copy code below into it. This code does nothing yet, but it's good starting point. If you can compile and run it, that means everything is installed properly.
 
hello_aws.cpp

 
In order to build hello_aws.cpp file we need building script. Create build_and_run.sh file and copy script below into it. Again, remeber about setting executable attribute (chmod +x build_and_run.sh).
 
build_and_run.sh

 

AWSCredentials

Next thing we have to add is instance of AWSCredentials class. As it’s name suggests, it keeps access key and secret key. The first one is like login, the second, like password. Both are created for you on aws.amazon.com website. Code after changes will look like this:
 

 

DefaultRetryStrategy

When we are dealing with network, sooner or later something goes wrong. Connection is lost, etc. In such case you should try couple times. You should retry.
 
To decide how many times application should try again and how long it should wait between retries, we need retry policy. AWS provides two classes to address this issue: RetryStrategy and DefaultRetryStrategy. The first one is abstract class, the later is default implementation that derives from RetryStrategy. I’m going to use the default retry policy because it’s enough for me. What you should know is that, if you need custom retry policy, you can create your own class that derives from RetryStrategy and then override methods: ShouldRetry and CalculateDelayBeforeNextRetry. You can find more details here.
 
Because ClientConfiguration takes std::shared_ptr, I created DefaultRetryStrategy using new operator and then used that pointer (defaultRetryStrategy) to create retryStrategy std::shared_ptr.
 

 

ClientConfiguration

Since we have std::shared_ptr to our retry strategy, we can create ClientConfiguration object. As it’s name suggests it contains settings for S3Client.
 

 
Timeouts are set to zero (it means infinity) to avoid timeout errors. It’s problematic when you are just starting your adventure with AWS. To use ClientConfiguration class we need to include S3Client.h file.
 

S3Client

S3Client object is essential for our application. It takes part in every operation like: creating bucket, uploading file, etc.
It’s worth to keep in mind that S3Client class is thread safe, so it’s instance can be shared by many threads and it has tcp connection pool. This pool is by default set to 25 connections but it can be set up in ClientConfiguration.maxConnections field.
 

 
Important!
It’s very important to delete client object before call to ShutdownAPI function. Otherwise you’ll get Segmentation fault error. For that reason you shouldn’t instantiate client as local object like this:

because local object will be destroyed after main function returns (so after ShutdownAPI is called).
 

Let’s create bucket

 
Almost all operations follow the same pattern.
1. Create request object.
2. Pass the request object to client’s request method.
3. Request method returns outcome object.
4. The outcome object tells you if the request was successful with “IsSuccess” method.
5. If successful, you can get the result object from outcome object with “GetResult” method.
6. If not successful, you can get error object from outcome object with “GetError” method.
 
To create bucket, I wrote CreateBucket function that takes, previously created, client from main function and bucket’s name.
 
Important!
Bucket name MUST BE GLOBALLY UNIQUE! It’s like domain. You cannot create “abc” bucket, because someone in the world already has bucket with such name.
 

 
This pice of code finally does something. You can use CloudBerry Explorer to see if bucket really exists. It's usefull to have other tool to test your code at this stage.
 

We have bucket. Now what?

This short tutorial shows you how to setup Linux Mint and how to write very simple application that uses aws-sdk-cpp. I’m not going to talk about all details. Below you have source code that shows you how to do other things, like: delete bucket, list buckets, upload object, download object, delete object, get size of object, etc.
 
Here you have some useful links, especially reference manual.
Amazon S3 website
How to create account by barakpinch
Reference Manual for AWS SDK for C++