RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
soa/service/s3_transfer_cmd.cc
00001 #include "soa/service/s3.h"
00002 #include "jml/utils/file_functions.h"
00003 #include <boost/program_options/cmdline.hpp>
00004 #include <boost/program_options/options_description.hpp>
00005 #include <boost/program_options/positional_options.hpp> 
00006 #include <boost/program_options/parsers.hpp>
00007 #include <boost/program_options/variables_map.hpp>
00008 #include <boost/filesystem.hpp>
00009 #include <fstream>
00010 #include <iostream>
00011 
00012 namespace po = boost::program_options;
00013 namespace fs = boost::filesystem;
00014 
00015 using namespace std;
00016 
00017 int main(int argc, char* argv[]){
00018     po::options_description desc("Allowed options");
00019 
00020     string localFile = "";
00021     string id = "";
00022     string key = "";
00023     string bucket = "";
00024     string s3File = "";
00025     string direction = "";
00026     int maxSizeKB = -1;
00027     
00028     desc.add_options()
00029         ("help,h", "Produce help message")
00030         ("localfile,l", po::value<string>(&localFile), "Local file")
00031         ("id,i", po::value<string>(&id), "S3 id")
00032         ("key,k", po::value<string>(&key), "S3 key")
00033         ("bucket,b", po::value<string>(&bucket), "S3 bucket")
00034         ("s3file,s", po::value<string>(&s3File), "S3 file key")
00035         ("direction,d", po::value<string>(&direction), 
00036             "Direction (u for upload, d for download)")
00037         ("maxsizekb,m", po::value<int>(&maxSizeKB), 
00038             "If specified, at most {maxsizebk} BK will be transferred. (Download only)");
00039 
00040 
00041     po::positional_options_description pos;
00042     pos.add("output", -1);
00043     po::variables_map vm;
00044     bool showHelp = false;
00045     try{
00046         po::parsed_options parsed = po::command_line_parser(argc, argv)
00047             .options(desc)
00048             .positional(pos)
00049             .run();
00050         po::store(parsed, vm);
00051         po::notify(vm);
00052 
00053         if(direction != "u" && direction != "d"){
00054             cout << "Invalid direction\n";
00055             showHelp = true;
00056         }
00057     }catch(...){
00058         //invalid command line param
00059         showHelp = true;
00060     }
00061 
00062     //If one of the options is set to 'help'...
00063     if (showHelp || vm.count("help")){
00064         //Display the options_description
00065         cout << desc << "\n";
00066         return showHelp ? 1 : 0;
00067     }else if(localFile.length() == 0 || 
00068             id.length() == 0 || 
00069             key.length() == 0 ||
00070             bucket.length() == 0 ||
00071             s3File.length() == 0
00072     ){
00073         cout << "You need to specify all parameters except help and maxsizekb. " 
00074                 << "Run with \"-h\" to list them.\n";
00075         return 1;
00076     }
00077     
00078     try{
00079         if(direction == "u"){
00080             //upload
00081             cout << "File: " << localFile 
00082                     << " - Size: " << fs::file_size(localFile) <<  "\n";
00083             ML::File_Read_Buffer frb(localFile);
00084             Datacratic::S3Api s3(id, key);
00085             string result = s3.upload(
00086                     frb.start(), 
00087                     fs::file_size(localFile), 
00088                     bucket, "/" + s3File); 
00089             cout << result << "\n";
00090         }else{
00091             //download
00092             Datacratic::S3Api s3(id, key);
00093             s3.downloadToFile(
00094                     "s3://" + bucket + "/" + s3File, 
00095                     localFile, 
00096                     maxSizeKB > 0 ? 1024 * maxSizeKB : -1);
00097         }
00098     }catch(const fs::filesystem_error& ex){
00099         cout << "File does not exist.\n";
00100         return 1;
00101     }
00102     return 0;
00103 }
00104 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator