Arbiter  0.0.0
s3.hpp
1 #pragma once
2 
3 #include <memory>
4 #include <string>
5 #include <vector>
6 
7 #ifndef ARBITER_IS_AMALGAMATION
8 #include <arbiter/drivers/http.hpp>
9 #endif
10 
11 #ifdef ARBITER_CUSTOM_NAMESPACE
12 namespace ARBITER_CUSTOM_NAMESPACE
13 {
14 #endif
15 
16 namespace arbiter
17 {
18 
19 namespace drivers
20 {
21 
23 class S3 : public Http
24 {
25 public:
26  class Auth;
27 
28  S3(
29  http::Pool& pool,
30  const Auth& auth,
31  std::string region = "us-east-1",
32  bool sse = false);
33 
42  static std::unique_ptr<S3> create(
43  http::Pool& pool,
44  const Json::Value& json);
45 
46  static std::string extractProfile(const Json::Value& json);
47 
48  virtual std::string type() const override { return "s3"; }
49 
50  virtual std::unique_ptr<std::size_t> tryGetSize(
51  std::string path) const override;
52 
54  virtual void put(
55  std::string path,
56  const std::vector<char>& data,
57  http::Headers headers,
58  http::Query query) const override;
59 
61  class Auth
62  {
63  public:
64  Auth(std::string access, std::string hidden);
65 
75  static std::unique_ptr<Auth> find(std::string profile = "");
76 
77  std::string access() const;
78  std::string hidden() const;
79 
80  private:
81  std::string m_access;
82  std::string m_hidden;
83  };
84 
85 private:
87  virtual bool get(
88  std::string path,
89  std::vector<char>& data,
90  http::Headers headers,
91  http::Query query) const override;
92 
93  virtual std::vector<std::string> glob(
94  std::string path,
95  bool verbose) const override;
96 
97  struct Resource
98  {
99  Resource(std::string baseUrl, std::string fullPath);
100 
101  std::string url() const;
102  std::string host() const;
103 
104  std::string baseUrl;
105  std::string bucket;
106  std::string object;
107  };
108 
109  class FormattedTime
110  {
111  public:
112  FormattedTime();
113 
114  const std::string& date() const { return m_date; }
115  const std::string& time() const { return m_time; }
116 
117  std::string amazonDate() const
118  {
119  return date() + 'T' + time() + 'Z';
120  }
121 
122  private:
123  std::string formatTime(const std::string& format) const;
124 
125  const std::string m_date;
126  const std::string m_time;
127  };
128 
129  class ApiV4
130  {
131  public:
132  ApiV4(
133  std::string verb,
134  const std::string& region,
135  const Resource& resource,
136  const S3::Auth& auth,
137  const http::Query& query,
138  const http::Headers& headers,
139  const std::vector<char>& data);
140 
141  const http::Headers& headers() const { return m_headers; }
142  const http::Query& query() const { return m_query; }
143 
144  const std::string& signedHeadersString() const
145  {
146  return m_signedHeadersString;
147  }
148 
149  private:
150  std::string buildCanonicalRequest(
151  std::string verb,
152  const Resource& resource,
153  const http::Query& query,
154  const std::vector<char>& data) const;
155 
156  std::string buildStringToSign(
157  const std::string& canonicalRequest) const;
158 
159  std::string calculateSignature(
160  const std::string& stringToSign) const;
161 
162  std::string getAuthHeader(
163  const std::string& signedHeadersString,
164  const std::string& signature) const;
165 
166  const S3::Auth& m_auth;
167  const std::string m_region;
168  const FormattedTime m_formattedTime;
169 
170  http::Headers m_headers;
171  http::Query m_query;
172  std::string m_canonicalHeadersString;
173  std::string m_signedHeadersString;
174  };
175 
176  Auth m_auth;
177 
178  std::string m_region;
179  std::string m_baseUrl;
180  http::Headers m_baseHeaders;
181 };
182 
183 } // namespace drivers
184 } // namespace arbiter
185 
186 #ifdef ARBITER_CUSTOM_NAMESPACE
187 }
188 #endif
189 
AWS authentication information.
Definition: s3.hpp:61
HTTP driver. Intended as both a standalone driver as well as a base for derived drivers build atop HT...
Definition: http.hpp:29
virtual std::string type() const override
Definition: s3.hpp:48
Definition: arbiter.cpp:16
Amazon S3 driver.
Definition: s3.hpp:23