OmniSciDB  6686921089
 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 <thread>
24 #include "Archive.h"
25 
26 #include <openssl/evp.h>
27 
28 #ifdef HAVE_AWS_S3
29 #include <aws/core/Aws.h>
30 #include <aws/s3/S3Client.h>
31 #endif // HAVE_AWS_S3
32 
33 // this is the based archive class for files hosted on AWS S3.
34 // known variants:
35 // . parquet files
36 // . compressed files
37 // no mixed of above is supported yet
38 class S3Archive : public Archive {
39  public:
40  S3Archive(const std::string& url, const bool plain_text) : Archive(url, plain_text) {
41  // these envs are on server side so are global settings
42  // which make few senses in case of private s3 resources
43  char* env;
44  if (0 != (env = getenv("AWS_REGION"))) {
45  s3_region = env;
46  }
47  if (0 != (env = getenv("AWS_ACCESS_KEY_ID"))) {
48  s3_access_key = env;
49  }
50  if (0 != (env = getenv("AWS_SECRET_ACCESS_KEY"))) {
51  s3_secret_key = env;
52  }
53  if (0 != (env = getenv("AWS_SESSION_TOKEN"))) {
54  s3_session_token = env;
55  }
56 
57  if (0 != (env = getenv("AWS_ENDPOINT"))) {
58  s3_endpoint = env;
59  }
60  }
61 
62  S3Archive(const std::string& url,
63  const std::string& s3_access_key,
64  const std::string& s3_secret_key,
65  const std::string& s3_session_token,
66  const std::string& s3_region,
67  const std::string& s3_endpoint,
68  const bool plain_text,
69  const std::optional<std::string>& regex_path_filter,
70  const std::optional<std::string>& file_sort_order_by,
71  const std::optional<std::string>& file_sort_regex)
72  : S3Archive(url, plain_text) {
73  this->s3_access_key = s3_access_key;
74  this->s3_secret_key = s3_secret_key;
75  this->s3_session_token = s3_session_token;
76  this->s3_region = s3_region;
77  this->s3_endpoint = s3_endpoint;
78  this->regex_path_filter = regex_path_filter;
79  this->file_sort_order_by = file_sort_order_by;
80  this->file_sort_regex = file_sort_regex;
81 
82  // this must be local to omnisci_server not client
83  // or posix dir path accessible to omnisci_server
84  auto env_s3_temp_dir = getenv("TMPDIR");
85  s3_temp_dir = env_s3_temp_dir ? env_s3_temp_dir : "/tmp";
86  }
87 
88  ~S3Archive() override {
89 #ifdef HAVE_AWS_S3
90  for (auto& thread : threads) {
91  if (thread.joinable()) {
92  thread.join();
93  }
94  }
95 #endif // HAVE_AWS_S3
96  }
97 
98 #ifdef HAVE_AWS_S3
99  void init_for_read() override;
100 #else
101  void init_for_read() override {
102  throw std::runtime_error("AWS S3 support not available");
103  }
104 #endif
105  const std::vector<std::string>& get_objkeys() { return objkeys; }
106 #ifdef HAVE_AWS_S3
107  const std::string land(const std::string& objkey,
108  std::exception_ptr& teptr,
109  const bool for_detection);
110  void vacuum(const std::string& objkey);
111 #else
112  const std::string land(const std::string& objkey,
113  std::exception_ptr& teptr,
114  const bool for_detection) {
115  throw std::runtime_error("AWS S3 support not available");
116  }
117  void vacuum(const std::string& objkey) {
118  throw std::runtime_error("AWS S3 support not available");
119  }
120 #endif // HAVE_AWS_S3
121  size_t get_total_file_size() const { return total_file_size; }
122 
123  private:
124 #ifdef HAVE_AWS_S3
125  static int awsapi_count;
126  static std::mutex awsapi_mtx;
127  static Aws::SDKOptions awsapi_options;
128 
129  std::unique_ptr<Aws::S3::S3Client> s3_client;
130  std::vector<std::thread> threads;
131 #endif // HAVE_AWS_S3
132  std::string s3_access_key; // per-query credentials to override the
133  std::string s3_secret_key; // settings in ~/.aws/credentials or environment
134  std::string s3_session_token;
135  std::string s3_region;
136  std::string s3_endpoint;
137  std::string s3_temp_dir;
138 
139  std::string bucket_name;
140  std::string prefix_name;
141  std::optional<std::string> regex_path_filter;
142  std::optional<std::string> file_sort_order_by;
143  std::optional<std::string> file_sort_regex;
144  std::vector<std::string> objkeys;
145  std::map<const std::string, const std::string> file_paths;
146  size_t total_file_size{0};
147 };
148 
149 class S3ParquetArchive : public S3Archive {
150  public:
151  S3ParquetArchive(const std::string& url,
152  const std::string& s3_access_key,
153  const std::string& s3_secret_key,
154  const std::string& s3_session_token,
155  const std::string& s3_region,
156  const std::string& s3_endpoint,
157  const bool plain_text,
158  const std::optional<std::string>& regex_path_filter,
159  const std::optional<std::string>& file_sort_order_by,
160  const std::optional<std::string>& file_sort_regex)
161  : S3Archive(url,
162  s3_access_key,
163  s3_secret_key,
164  s3_session_token,
165  s3_region,
166  s3_endpoint,
167  plain_text,
168  regex_path_filter,
169  file_sort_order_by,
170  file_sort_regex) {}
171 };
172 
173 #endif /* ARCHIVE_S3ARCHIVE_H_ */
std::string s3_endpoint
Definition: S3Archive.h:136
std::string s3_region
Definition: S3Archive.h:135
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:62
size_t total_file_size
Definition: S3Archive.h:146
const std::string land(const std::string &objkey, std::exception_ptr &teptr, const bool for_detection)
Definition: S3Archive.h:112
std::string prefix_name
Definition: S3Archive.h:140
std::string bucket_name
Definition: S3Archive.h:139
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:151
std::string s3_access_key
Definition: S3Archive.h:132
std::map< const std::string, const std::string > file_paths
Definition: S3Archive.h:145
~S3Archive() override
Definition: S3Archive.h:88
const std::vector< std::string > & get_objkeys()
Definition: S3Archive.h:105
std::optional< std::string > file_sort_regex
Definition: S3Archive.h:143
std::optional< std::string > regex_path_filter
Definition: S3Archive.h:141
void init_for_read() override
Definition: S3Archive.h:101
std::optional< std::string > file_sort_order_by
Definition: S3Archive.h:142
std::string s3_session_token
Definition: S3Archive.h:134
std::string s3_temp_dir
Definition: S3Archive.h:137
std::string url
Definition: Archive.h:201
std::string s3_secret_key
Definition: S3Archive.h:133
S3Archive(const std::string &url, const bool plain_text)
Definition: S3Archive.h:40
bool plain_text
Definition: Archive.h:205
std::vector< std::string > objkeys
Definition: S3Archive.h:144
void vacuum(const std::string &objkey)
Definition: S3Archive.h:117
size_t get_total_file_size() const
Definition: S3Archive.h:121