Boost.AsioでS3からファイルを取得

表題の通り。
CentOSのBoostのバージョン、上がらないかな…。

#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

int main(int argc, char* argv[]) {
  try {
    std::string host = argv[1];
    std::string path = argv[2];
    std::string date = argv[3];
    std::string auth = argv[4];

    boost::asio::io_service io_service;

    tcp::resolver resolver(io_service);
    tcp::resolver::query query(host, "http");
    tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
    tcp::resolver::iterator end;

    tcp::socket socket(io_service);
    boost::system::error_code error = boost::asio::error::host_not_found;

    while (error && endpoint_iterator != end) {
      socket.close();
      socket.connect(*endpoint_iterator++, error);
    }

    if (error) {
      throw boost::system::system_error(error);
    }

    boost::asio::streambuf request;
    std::ostream request_stream(&request);

    request_stream << "GET " << path << " HTTP/1.0\r\n";
    request_stream << "Host: " << host << "\r\n";
    request_stream << "Date: " << date << "\r\n";
    request_stream << "Authorization: " << auth << "\r\n";
    request_stream << "Connection: close\r\n\r\n";

    boost::asio::write(socket, request);

    boost::asio::streambuf response;
    boost::asio::read_until(socket, response, "\r\n");
    std::istream response_stream(&response);

    std::string http_version;
    response_stream >> http_version;

    unsigned int status_code;
    response_stream >> status_code;

    std::string status_message;
    std::getline(response_stream, status_message);

    if (!response_stream || http_version.substr(0, 5) != "HTTP/") {
      std::cout << "Invalid response\n";
      return 1;
    }

/*
    if (status_code != 200) {
      std::cout << "Response returned with status code " << status_code << "\n";
      return 1;
    }
*/

    boost::asio::read_until(socket, response, "\r\n\r\n");

    /*
    std::string header;

    while (std::getline(response_stream, header) && header != "\r") {
      std::cout << header << "\n";
    }

    std::cout << "\n";
     */

    if (response.size() > 0) {
      std::cout << &response;
    }

   while (boost::asio::read(socket, response, boost::asio::transfer_at_least(1), error)) {
      std::cout << &response;
    }

    if (error != boost::asio::error::eof) {
      throw boost::system::system_error(error);
    }
  } catch (std::exception& e) {
    std::cout << "Exception: " << e.what() << "\n";
  }

  return 0;
}

シンガポールリージョンにて実行時間を計ってみる。


~$ time ./client 'my_bucket.s3.amazonaws.com' '/foo.jpg' 'Fri, 20 Aug 2010 01:52:11 +0900' 'AWS XXXXXXXXXX:XXXXXXXXXX=' > bar.jpg

real 0m0.346s
user 0m0.010s
sys 0m0.017s
~$ time ./client 'my_bucket.s3.amazonaws.com' '/foo.jpg' 'Fri, 20 Aug 2010 01:52:11 +0900' 'AWS XXXXXXXXXX:XXXXXXXXXX=' > bar.jpg

real 0m0.446s
user 0m0.000s
sys 0m0.000s
~$ time ./client 'my_bucket.s3.amazonaws.com' '/foo.jpg' 'Fri, 20 Aug 2010 01:52:11 +0900' 'AWS XXXXXXXXXX:XXXXXXXXXX=' > bar.jpg

real 0m0.327s
user 0m0.001s
sys 0m0.017s
~$ ll bar.jpg
-rw-r--r-- 1 root root 4365700 Aug 20 01:52 bar.jpg

4.3Mで0.3〜0.4sぐらい。


Rubyだと…


~$ ruby s3get.rb
user system total real
get: 0.730000 0.070000 0.800000 ( 1.136291)
~$ ruby s3get.rb
user system total real
get: 0.790000 0.110000 0.900000 ( 1.086686)
~$ ruby s3get.rb
user system total real
get: 0.690000 0.070000 0.760000 ( 1.136873)
1s超えるぐらいか。