OmniSciDB  cde582ebc3
 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 2022 HEAVY.AI, 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  const std::string& s3_temp_dir_path = {})
76  : S3Archive(url, plain_text) {
77  this->s3_access_key = s3_access_key;
78  this->s3_secret_key = s3_secret_key;
79  this->s3_session_token = s3_session_token;
80  this->s3_region = s3_region;
81  this->s3_endpoint = s3_endpoint;
82  this->regex_path_filter = regex_path_filter;
83  this->file_sort_order_by = file_sort_order_by;
84  this->file_sort_regex = file_sort_regex;
85 
86  if (s3_temp_dir_path.empty()) {
87  // this must be local to heavydb not client
88  // or posix dir path accessible to heavydb
89  auto env_s3_temp_dir = getenv("TMPDIR");
90  s3_temp_dir = env_s3_temp_dir ? env_s3_temp_dir : "/tmp";
91  } else {
92  s3_temp_dir = s3_temp_dir_path;
93  }
94  }
95 
96  ~S3Archive() override {
97 #ifdef HAVE_AWS_S3
98  for (auto& thread : threads) {
99  if (thread.joinable()) {
100  thread.join();
101  }
102  }
103 #endif // HAVE_AWS_S3
104  }
105 
106 #ifdef HAVE_AWS_S3
107  void init_for_read() override;
108 #else
109  void init_for_read() override {
110  throw std::runtime_error("AWS S3 support not available");
111  }
112 #endif
113  const std::vector<std::string>& get_objkeys() { return objkeys; }
114 #ifdef HAVE_AWS_S3
115  const std::string land(const std::string& objkey,
116  std::exception_ptr& teptr,
117  const bool for_detection,
118  const bool allow_named_pipe_use = true,
119  const bool track_file_paths = true);
120  void vacuum(const std::string& objkey);
121 #else
122  const std::string land(const std::string& objkey,
123  std::exception_ptr& teptr,
124  const bool for_detection) {
125  throw std::runtime_error("AWS S3 support not available");
126  }
127  void vacuum(const std::string& objkey) {
128  throw std::runtime_error("AWS S3 support not available");
129  }
130 #endif // HAVE_AWS_S3
131  size_t get_total_file_size() const { return total_file_size; }
132 
133  private:
134 #ifdef HAVE_AWS_S3
135  static int awsapi_count;
136  static std::mutex awsapi_mtx;
137  static Aws::SDKOptions awsapi_options;
138 
139  std::unique_ptr<Aws::S3::S3Client> s3_client;
140  std::vector<std::thread> threads;
141 #endif // HAVE_AWS_S3
142  std::string s3_access_key; // per-query credentials to override the
143  std::string s3_secret_key; // settings in ~/.aws/credentials or environment
144  std::string s3_session_token;
145  std::string s3_region;
146  std::string s3_endpoint;
147  std::string s3_temp_dir;
148 
149  std::string bucket_name;
150  std::string prefix_name;
151  std::optional<std::string> regex_path_filter;
152  std::optional<std::string> file_sort_order_by;
153  std::optional<std::string> file_sort_regex;
154  std::vector<std::string> objkeys;
155  std::map<const std::string, const std::string> file_paths;
156  size_t total_file_size{0};
157 };
158 
159 class S3ParquetArchive : public S3Archive {
160  public:
161  S3ParquetArchive(const std::string& url,
162  const std::string& s3_access_key,
163  const std::string& s3_secret_key,
164  const std::string& s3_session_token,
165  const std::string& s3_region,
166  const std::string& s3_endpoint,
167  const bool plain_text,
168  const std::optional<std::string>& regex_path_filter,
169  const std::optional<std::string>& file_sort_order_by,
170  const std::optional<std::string>& file_sort_regex)
171  : S3Archive(url,
172  s3_access_key,
173  s3_secret_key,
174  s3_session_token,
175  s3_region,
176  s3_endpoint,
177  plain_text,
178  regex_path_filter,
179  file_sort_order_by,
180  file_sort_regex) {}
181 };
182 
183 #endif /* ARCHIVE_S3ARCHIVE_H_ */
std::string s3_endpoint
Definition: S3Archive.h:146
std::string s3_region
Definition: S3Archive.h:145
size_t total_file_size
Definition: S3Archive.h:156
const std::string land(const std::string &objkey, std::exception_ptr &teptr, const bool for_detection)
Definition: S3Archive.h:122
std::string prefix_name
Definition: S3Archive.h:150
std::string bucket_name
Definition: S3Archive.h:149
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:161
std::string s3_access_key
Definition: S3Archive.h:142
std::map< const std::string, const std::string > file_paths
Definition: S3Archive.h:155
~S3Archive() override
Definition: S3Archive.h:96
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, const std::string &s3_temp_dir_path={})
Definition: S3Archive.h:65
const std::vector< std::string > & get_objkeys()
Definition: S3Archive.h:113
std::optional< std::string > file_sort_regex
Definition: S3Archive.h:153
std::optional< std::string > regex_path_filter
Definition: S3Archive.h:151
void init_for_read() override
Definition: S3Archive.h:109
std::optional< std::string > file_sort_order_by
Definition: S3Archive.h:152
std::string s3_session_token
Definition: S3Archive.h:144
std::string s3_temp_dir
Definition: S3Archive.h:147
std::string url
Definition: Archive.h:190
std::string s3_secret_key
Definition: S3Archive.h:143
S3Archive(const std::string &url, const bool plain_text)
Definition: S3Archive.h:43
bool plain_text
Definition: Archive.h:194
std::vector< std::string > objkeys
Definition: S3Archive.h:154
void vacuum(const std::string &objkey)
Definition: S3Archive.h:127
size_t get_total_file_size() const
Definition: S3Archive.h:131