OmniSciDB  91042dcc5b
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
S3Archive.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ARCHIVE_S3ARCHIVE_H_
18 #define ARCHIVE_S3ARCHIVE_H_
19 
20 #include <cstdio>
21 #include <exception>
22 #include <map>
23 #include <optional>
24 #include <string>
25 #include <thread>
26 #include <vector>
27 #include "Archive.h"
28 
29 #include <openssl/evp.h>
30 
31 #ifdef HAVE_AWS_S3
32 #include <aws/core/Aws.h>
33 #include <aws/s3/S3Client.h>
34 #endif // HAVE_AWS_S3
35 
36 // this is the based archive class for files hosted on AWS S3.
37 // known variants:
38 // . parquet files
39 // . compressed files
40 // no mixed of above is supported yet
41 class S3Archive : public Archive {
42  public:
43  S3Archive(const std::string& url, const bool plain_text) : Archive(url, plain_text) {
44  // these envs are on server side so are global settings
45  // which make few senses in case of private s3 resources
46  char* env;
47  if (0 != (env = getenv("AWS_REGION"))) {
48  s3_region = env;
49  }
50  if (0 != (env = getenv("AWS_ACCESS_KEY_ID"))) {
51  s3_access_key = env;
52  }
53  if (0 != (env = getenv("AWS_SECRET_ACCESS_KEY"))) {
54  s3_secret_key = env;
55  }
56  if (0 != (env = getenv("AWS_SESSION_TOKEN"))) {
57  s3_session_token = env;
58  }
59 
60  if (0 != (env = getenv("AWS_ENDPOINT"))) {
61  s3_endpoint = env;
62  }
63  }
64 
65  S3Archive(const std::string& url,
66  const std::string& s3_access_key,
67  const std::string& s3_secret_key,
68  const std::string& s3_session_token,
69  const std::string& s3_region,
70  const std::string& s3_endpoint,
71  const bool plain_text,
72  const std::optional<std::string>& regex_path_filter,
73  const std::optional<std::string>& file_sort_order_by,
74  const std::optional<std::string>& file_sort_regex)
75  : S3Archive(url, plain_text) {
76  this->s3_access_key = s3_access_key;
77  this->s3_secret_key = s3_secret_key;
78  this->s3_session_token = s3_session_token;
79  this->s3_region = s3_region;
80  this->s3_endpoint = s3_endpoint;
81  this->regex_path_filter = regex_path_filter;
82  this->file_sort_order_by = file_sort_order_by;
83  this->file_sort_regex = file_sort_regex;
84 
85  // this must be local to omnisci_server not client
86  // or posix dir path accessible to omnisci_server
87  auto env_s3_temp_dir = getenv("TMPDIR");
88  s3_temp_dir = env_s3_temp_dir ? env_s3_temp_dir : "/tmp";
89  }
90 
91  ~S3Archive() override {
92 #ifdef HAVE_AWS_S3
93  for (auto& thread : threads) {
94  if (thread.joinable()) {
95  thread.join();
96  }
97  }
98 #endif // HAVE_AWS_S3
99  }
100 
101 #ifdef HAVE_AWS_S3
102  void init_for_read() override;
103 #else
104  void init_for_read() override {
105  throw std::runtime_error("AWS S3 support not available");
106  }
107 #endif
108  const std::vector<std::string>& get_objkeys() { return objkeys; }
109 #ifdef HAVE_AWS_S3
110  const std::string land(const std::string& objkey,
111  std::exception_ptr& teptr,
112  const bool for_detection);
113  void vacuum(const std::string& objkey);
114 #else
115  const std::string land(const std::string& objkey,
116  std::exception_ptr& teptr,
117  const bool for_detection) {
118  throw std::runtime_error("AWS S3 support not available");
119  }
120  void vacuum(const std::string& objkey) {
121  throw std::runtime_error("AWS S3 support not available");
122  }
123 #endif // HAVE_AWS_S3
124  size_t get_total_file_size() const { return total_file_size; }
125 
126  private:
127 #ifdef HAVE_AWS_S3
128  static int awsapi_count;
129  static std::mutex awsapi_mtx;
130  static Aws::SDKOptions awsapi_options;
131 
132  std::unique_ptr<Aws::S3::S3Client> s3_client;
133  std::vector<std::thread> threads;
134 #endif // HAVE_AWS_S3
135  std::string s3_access_key; // per-query credentials to override the
136  std::string s3_secret_key; // settings in ~/.aws/credentials or environment
137  std::string s3_session_token;
138  std::string s3_region;
139  std::string s3_endpoint;
140  std::string s3_temp_dir;
141 
142  std::string bucket_name;
143  std::string prefix_name;
144  std::optional<std::string> regex_path_filter;
145  std::optional<std::string> file_sort_order_by;
146  std::optional<std::string> file_sort_regex;
147  std::vector<std::string> objkeys;
148  std::map<const std::string, const std::string> file_paths;
149  size_t total_file_size{0};
150 };
151 
152 class S3ParquetArchive : public S3Archive {
153  public:
154  S3ParquetArchive(const std::string& url,
155  const std::string& s3_access_key,
156  const std::string& s3_secret_key,
157  const std::string& s3_session_token,
158  const std::string& s3_region,
159  const std::string& s3_endpoint,
160  const bool plain_text,
161  const std::optional<std::string>& regex_path_filter,
162  const std::optional<std::string>& file_sort_order_by,
163  const std::optional<std::string>& file_sort_regex)
164  : S3Archive(url,
165  s3_access_key,
166  s3_secret_key,
167  s3_session_token,
168  s3_region,
169  s3_endpoint,
170  plain_text,
171  regex_path_filter,
172  file_sort_order_by,
173  file_sort_regex) {}
174 };
175 
176 #endif /* ARCHIVE_S3ARCHIVE_H_ */
std::string s3_endpoint
Definition: S3Archive.h:139
std::string s3_region
Definition: S3Archive.h:138
S3Archive(const std::string &url, const std::string &s3_access_key, const std::string &s3_secret_key, const std::string &s3_session_token, const std::string &s3_region, const std::string &s3_endpoint, const bool plain_text, const std::optional< std::string > &regex_path_filter, const std::optional< std::string > &file_sort_order_by, const std::optional< std::string > &file_sort_regex)
Definition: S3Archive.h:65
size_t total_file_size
Definition: S3Archive.h:149
const std::string land(const std::string &objkey, std::exception_ptr &teptr, const bool for_detection)
Definition: S3Archive.h:115
std::string prefix_name
Definition: S3Archive.h:143
std::string bucket_name
Definition: S3Archive.h:142
S3ParquetArchive(const std::string &url, const std::string &s3_access_key, const std::string &s3_secret_key, const std::string &s3_session_token, const std::string &s3_region, const std::string &s3_endpoint, const bool plain_text, const std::optional< std::string > &regex_path_filter, const std::optional< std::string > &file_sort_order_by, const std::optional< std::string > &file_sort_regex)
Definition: S3Archive.h:154
std::string s3_access_key
Definition: S3Archive.h:135
std::map< const std::string, const std::string > file_paths
Definition: S3Archive.h:148
~S3Archive() override
Definition: S3Archive.h:91
const std::vector< std::string > & get_objkeys()
Definition: S3Archive.h:108
std::optional< std::string > file_sort_regex
Definition: S3Archive.h:146
std::optional< std::string > regex_path_filter
Definition: S3Archive.h:144
void init_for_read() override
Definition: S3Archive.h:104
std::optional< std::string > file_sort_order_by
Definition: S3Archive.h:145
std::string s3_session_token
Definition: S3Archive.h:137
std::string s3_temp_dir
Definition: S3Archive.h:140
std::string url
Definition: Archive.h:202
std::string s3_secret_key
Definition: S3Archive.h:136
S3Archive(const std::string &url, const bool plain_text)
Definition: S3Archive.h:43
bool plain_text
Definition: Archive.h:206
std::vector< std::string > objkeys
Definition: S3Archive.h:147
void vacuum(const std::string &objkey)
Definition: S3Archive.h:120
size_t get_total_file_size() const
Definition: S3Archive.h:124