|
6 | 6 | import zipfile |
7 | 7 | from datetime import datetime |
8 | 8 | from pathlib import Path |
| 9 | +import shutil |
9 | 10 |
|
10 | 11 | import boto3 |
11 | 12 | import requests |
@@ -209,6 +210,15 @@ def __upload_files_to_aws_thread( |
209 | 210 | already_uploaded[i] = True |
210 | 211 |
|
211 | 212 |
|
| 213 | +def _download_file(url, local_filename): |
| 214 | + with requests.get(url, stream=True) as r: |
| 215 | + r.raise_for_status() |
| 216 | + with open(local_filename, 'wb') as f: |
| 217 | + for chunk in r.iter_content(chunk_size=8192): |
| 218 | + f.write(chunk) |
| 219 | + return local_filename |
| 220 | + |
| 221 | + |
212 | 222 | def download_export( |
213 | 223 | project, export, folder_path, extract_zip_contents=True, to_s3_bucket=None |
214 | 224 | ): |
@@ -243,25 +253,26 @@ def download_export( |
243 | 253 | break |
244 | 254 |
|
245 | 255 | filename = Path(res['path']).name |
246 | | - r = requests.get(res['download'], allow_redirects=True) |
247 | | - if to_s3_bucket is None: |
248 | | - filepath = Path(folder_path) / filename |
249 | | - open(filepath, 'wb').write(r.content) |
250 | | - if extract_zip_contents: |
251 | | - with zipfile.ZipFile(filepath, 'r') as f: |
252 | | - f.extractall(folder_path) |
253 | | - Path.unlink(filepath) |
254 | | - logger.info("Extracted %s to folder %s", filepath, folder_path) |
255 | | - else: |
256 | | - logger.info("Downloaded export ID %s to %s", res['id'], filepath) |
257 | | - else: |
258 | | - with tempfile.TemporaryDirectory() as tmpdirname: |
259 | | - filepath = Path(tmpdirname) / filename |
260 | | - open(filepath, 'wb').write(r.content) |
| 256 | + with tempfile.TemporaryDirectory() as tmpdirname: |
| 257 | + temp_filepath = Path(tmpdirname) / filename |
| 258 | + _download_file(res['download'], temp_filepath) |
| 259 | + if to_s3_bucket is None: |
| 260 | + filepath = Path(folder_path) / filename |
| 261 | + shutil.copyfile(temp_filepath, filepath) |
261 | 262 | if extract_zip_contents: |
262 | 263 | with zipfile.ZipFile(filepath, 'r') as f: |
263 | | - f.extractall(tmpdirname) |
| 264 | + f.extractall(folder_path) |
264 | 265 | Path.unlink(filepath) |
| 266 | + logger.info("Extracted %s to folder %s", filepath, folder_path) |
| 267 | + else: |
| 268 | + logger.info( |
| 269 | + "Downloaded export ID %s to %s", res['id'], filepath |
| 270 | + ) |
| 271 | + else: |
| 272 | + if extract_zip_contents: |
| 273 | + with zipfile.ZipFile(temp_filepath, 'r') as f: |
| 274 | + f.extractall(tmpdirname) |
| 275 | + Path.unlink(temp_filepath) |
265 | 276 | files_to_upload = [] |
266 | 277 | for file in Path(tmpdirname).rglob("*.*"): |
267 | 278 | if not file.is_file(): |
@@ -296,4 +307,4 @@ def download_export( |
296 | 307 | t.join() |
297 | 308 | finish_event.set() |
298 | 309 | tqdm_thread.join() |
299 | | - logger.info("Exported to AWS %s/%s", to_s3_bucket, folder_path) |
| 310 | + logger.info("Exported to AWS %s/%s", to_s3_bucket, folder_path) |
0 commit comments