OmniSciDB  fe05a0c208
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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  : S3Archive(url, plain_text) {
70  this->s3_access_key = s3_access_key;
71  this->s3_secret_key = s3_secret_key;
72  this->s3_session_token = s3_session_token;
73  this->s3_region = s3_region;
74  this->s3_endpoint = s3_endpoint;
75 
76  // this must be local to omnisci_server not client
77  // or posix dir path accessible to omnisci_server
78  auto env_s3_temp_dir = getenv("TMPDIR");
79  s3_temp_dir = env_s3_temp_dir ? env_s3_temp_dir : "/tmp";
80  }
81 
82  ~S3Archive() override {
83 #ifdef HAVE_AWS_S3
84  for (auto& thread : threads) {
85  if (thread.joinable()) {
86  thread.join();
87  }
88  }
89 #endif // HAVE_AWS_S3
90  }
91 
92 #ifdef HAVE_AWS_S3
93  void init_for_read() override;
94 #else
95  void init_for_read() override {
96  throw std::runtime_error("AWS S3 support not available");
97  }
98 #endif
99  const std::vector<std::string>& get_objkeys() { return objkeys; }
100 #ifdef HAVE_AWS_S3
101  const std::string land(const std::string& objkey,
102  std::exception_ptr& teptr,
103  const bool for_detection);
104  void vacuum(const std::string& objkey);
105 #else
106  const std::string land(const std::string& objkey,
107  std::exception_ptr& teptr,
108  const bool for_detection) {
109  throw std::runtime_error("AWS S3 support not available");
110  }
111  void vacuum(const std::string& objkey) {
112  throw std::runtime_error("AWS S3 support not available");
113  }
114 #endif // HAVE_AWS_S3
115  size_t get_total_file_size() const { return total_file_size; }
116 
117  private:
118 #ifdef HAVE_AWS_S3
119  static int awsapi_count;
120  static std::mutex awsapi_mtx;
121  static Aws::SDKOptions awsapi_options;
122 
123  std::unique_ptr<Aws::S3::S3Client> s3_client;
124  std::vector<std::thread> threads;
125 #endif // HAVE_AWS_S3
126  std::string s3_access_key; // per-query credentials to override the
127  std::string s3_secret_key; // settings in ~/.aws/credentials or environment
128  std::string s3_session_token;
129  std::string s3_region;
130  std::string s3_endpoint;
131  std::string s3_temp_dir;
132 
133  std::string bucket_name;
134  std::string prefix_name;
135  std::vector<std::string> objkeys;
136  std::map<const std::string, const std::string> file_paths;
137  size_t total_file_size{0};
138 };
139 
140 class S3ParquetArchive : public S3Archive {
141  public:
142  S3ParquetArchive(const std::string& url,
143  const std::string& s3_access_key,
144  const std::string& s3_secret_key,
145  const std::string& s3_session_token,
146  const std::string& s3_region,
147  const std::string& s3_endpoint,
148  const bool plain_text)
149  : S3Archive(url,
150  s3_access_key,
151  s3_secret_key,
152  s3_session_token,
153  s3_region,
154  s3_endpoint,
155  plain_text) {}
156 };
157 
158 #endif /* ARCHIVE_S3ARCHIVE_H_ */
std::string s3_endpoint
Definition: S3Archive.h:130
std::string s3_region
Definition: S3Archive.h:129
size_t total_file_size
Definition: S3Archive.h:137
const std::string land(const std::string &objkey, std::exception_ptr &teptr, const bool for_detection)
Definition: S3Archive.h:106
std::string prefix_name
Definition: S3Archive.h:134
std::string bucket_name
Definition: S3Archive.h:133
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)
Definition: S3Archive.h:62
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)
Definition: S3Archive.h:142
std::string s3_access_key
Definition: S3Archive.h:126
std::map< const std::string, const std::string > file_paths
Definition: S3Archive.h:136
~S3Archive() override
Definition: S3Archive.h:82
const std::vector< std::string > & get_objkeys()
Definition: S3Archive.h:99
void init_for_read() override
Definition: S3Archive.h:95
std::string s3_session_token
Definition: S3Archive.h:128
std::string s3_temp_dir
Definition: S3Archive.h:131
std::string url
Definition: Archive.h:187
std::string s3_secret_key
Definition: S3Archive.h:127
S3Archive(const std::string &url, const bool plain_text)
Definition: S3Archive.h:40
bool plain_text
Definition: Archive.h:191
std::vector< std::string > objkeys
Definition: S3Archive.h:135
void vacuum(const std::string &objkey)
Definition: S3Archive.h:111
size_t get_total_file_size() const
Definition: S3Archive.h:115