|
2 | 2 |
|
3 | 3 | from wasabi import msg |
4 | 4 | import pandas as pd |
5 | | -from kern import authentication, api_calls, settings, exceptions |
| 5 | +from kern import authentication, api_calls, settings, exceptions, util |
6 | 6 | from typing import List, Optional, Dict |
7 | 7 | import json |
| 8 | +import os.path |
8 | 9 | from tqdm import tqdm |
9 | 10 | import spacy |
10 | 11 |
|
@@ -127,30 +128,47 @@ def get_record_export( |
127 | 128 | msg.good(f"Downloaded export to {download_to}") |
128 | 129 | return df |
129 | 130 |
|
130 | | - # TODO: issue #6 |
131 | | - # def post_file_import(self, upload_from: str): |
132 | | - # upload_from = f"{upload_from}_SCALE" |
133 | | - # file_type = "records" |
134 | | - # import_file_options = None |
135 | | - # config_url = settings.get_config_url() |
136 | | - # config_api_response = api_calls.get_request(config_url, self.session_token) |
137 | | - # endpoint = config_api_response["KERN_S3_ENDPOINT"] |
138 | | - |
139 | | - # import_url = settings.get_import_url(self.project_id) |
140 | | - # import_api_response = api_calls.post_request( |
141 | | - # import_url, |
142 | | - # { |
143 | | - # "file_name": upload_from, |
144 | | - # "file_type": file_type, |
145 | | - # "import_file_options": import_file_options, |
146 | | - # }, |
147 | | - # self.session_token, |
148 | | - # ) |
149 | | - |
150 | | - # credentials = import_api_response["Credentials"] |
151 | | - # access_key = credentials["AccessKeyId"] |
152 | | - # secret_key = credentials["SecretAccessKey"] |
153 | | - # session_token = credentials["SessionToken"] |
154 | | - |
155 | | - # upload_task_id = import_api_response["uploadTaskId"] |
156 | | - # return endpoint, access_key, secret_key, session_token, upload_task_id |
| 131 | + def post_file_import(self, path: str) -> bool: |
| 132 | + if not os.path.exists(path): |
| 133 | + raise Exception(f"Given filepath is not valid. Path: {path}") |
| 134 | + last_path_part = path.split("/")[-1] |
| 135 | + file_name = f"{last_path_part}_SCALE" |
| 136 | + file_type = "records" |
| 137 | + import_file_options = "" |
| 138 | + |
| 139 | + # config |
| 140 | + config_url = settings.get_base_config(self.project_id) |
| 141 | + config_api_response = api_calls.get_request( |
| 142 | + config_url, |
| 143 | + self.session_token, |
| 144 | + ) |
| 145 | + endpoint = config_api_response.get("KERN_S3_ENDPOINT") |
| 146 | + |
| 147 | + # credentials |
| 148 | + credentials_url = settings.get_import_url(self.project_id) |
| 149 | + credentials_api_response = api_calls.post_request( |
| 150 | + credentials_url, |
| 151 | + { |
| 152 | + "file_name": file_name, |
| 153 | + "file_type": file_type, |
| 154 | + "import_file_options": import_file_options, |
| 155 | + }, |
| 156 | + self.session_token, |
| 157 | + ) |
| 158 | + credentials = credentials_api_response["Credentials"] |
| 159 | + access_key = credentials["AccessKeyId"] |
| 160 | + secret_key = credentials["SecretAccessKey"] |
| 161 | + session_token = credentials["SessionToken"] |
| 162 | + upload_task_id = credentials_api_response["uploadTaskId"] |
| 163 | + bucket = credentials_api_response["bucket"] |
| 164 | + success = util.s3_upload( |
| 165 | + access_key, |
| 166 | + secret_key, |
| 167 | + session_token, |
| 168 | + bucket, |
| 169 | + endpoint, |
| 170 | + upload_task_id, |
| 171 | + path, |
| 172 | + file_name, |
| 173 | + ) |
| 174 | + return True if success else False |
0 commit comments