Skip to content

Utilisation du SDK Amazon (aws-sdk-cpp) avec Ceph

Tout d'abord, installez le SDK AWS C++, soit par paquet pour votre distribution, soit en le compilant https://github.com/aws/aws-sdk-cpp :

$ git clone https://github.com/aws/aws-sdk-cpp
$ mkdir aws-sdk-cpp-build
$ cd aws-sdk-cpp-build
$ cmake ../aws-sdk-cpp -DCMAKE_BUILD_TYPE=Release
$ make -j4
$ sudo make install

Créez un fichier s3aws.cpp contenant :

#include <aws/core/Aws.h>
#include <aws/core/auth/AWSCredentialsProvider.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/Bucket.h>

int main(int argc, char** argv)
{
  Aws::SDKOptions options;
  Aws::InitAPI(options);
  {
    Aws::Client::ClientConfiguration config;
    config.endpointOverride = "s3.mesocentre.uca.fr";
    Aws::Auth::AWSCredentials auth("MYACCESSKEY", "MYSECRETKEY");
    Aws::S3::S3Client s3_client(auth, config);
    auto outcome = s3_client.ListBuckets();
    if (outcome.IsSuccess())
    {
      std::cout << "Your buckets:" << std::endl;
      Aws::Vector<Aws::S3::Model::Bucket> bucket_list = outcome.GetResult().GetBuckets();
      for (auto const &bucket: bucket_list)
      {
        std::cout << "  * " << bucket.GetName() << std::endl;
      }
    }
    else
    {
      std::cout << "ListBuckets error: "
                << outcome.GetError().GetExceptionName() << " - "
                << outcome.GetError().GetMessage() << std::endl;
    }
  }
  Aws::ShutdownAPI(options);
}

//=============================================================================

Vous pouvez compiler ce fichier avec :

$ g++ -std=c++11 -o s3aws s3aws.cpp -laws-cpp-sdk-core -laws-cpp-sdk-s3

Si vous n'avez pas installé le AWS SDK dans /usr/ ou /usr/local (pour avoir les headers dans /usr/include et les bibliothèques dans /usr/lib), alors votre ligne de compilation sera plutôt :

$ g++ -std=c++11 -o s3aws s3aws.cpp -I /path/to/aws-sdk-cpp-install/include -L /path/to/aws-sdk-cpp-install/lib -laws-cpp-sdk-core -laws-cpp-sdk-s3

Et dans un monde où tout s'est bien passé, son exécution donne la liste des buckets de votre compte :

$ ./s3aws
Your buckets:
  * Barcelona_Data_Backup
  * monbucket42