|
| 1 | +import logging |
| 2 | +import os |
| 3 | +import zipfile |
| 4 | +from pathlib import Path |
| 5 | +from typing import Any, Optional |
| 6 | + |
| 7 | +import boto3 |
| 8 | +import botocore |
| 9 | + |
| 10 | +logger = logging.getLogger(__name__) |
| 11 | + |
| 12 | + |
| 13 | +ENABLE_UPLOAD = bool(int(os.environ.get("ENABLE_UPLOAD", 0))) |
| 14 | +BUCKET_REGION = os.environ["BUCKET_REGION"] |
| 15 | +BUCKET_NAME = os.environ["BUCKET_NAME"] |
| 16 | +BUCKET_ACCESS_KEY_ID = os.environ["BUCKET_ACCESS_KEY_ID"] |
| 17 | +BUCKET_ACCESS_KEY_SECRET = os.environ["BUCKET_ACCESS_KEY_SECRET"] |
| 18 | + |
| 19 | + |
| 20 | +def _get_client() -> Any: |
| 21 | + session = boto3.session.Session() |
| 22 | + return session.client( |
| 23 | + "s3", |
| 24 | + endpoint_url=f"https://{BUCKET_REGION}.digitaloceanspaces.com/", |
| 25 | + config=botocore.config.Config(s3={"addressing_style": "virtual"}), |
| 26 | + region_name=BUCKET_REGION, |
| 27 | + aws_access_key_id=BUCKET_ACCESS_KEY_ID, |
| 28 | + aws_secret_access_key=BUCKET_ACCESS_KEY_SECRET, |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +def upload_file(local_path: str, content_type: str) -> str: |
| 33 | + bucket_path = os.path.relpath(local_path, "data") |
| 34 | + logger.info(f"Uploading file {local_path}") |
| 35 | + client = _get_client() |
| 36 | + client.upload_file( |
| 37 | + local_path, |
| 38 | + BUCKET_NAME, |
| 39 | + bucket_path, |
| 40 | + ExtraArgs={"ACL": "public-read", "ContentType": content_type}, |
| 41 | + ) |
| 42 | + return ( |
| 43 | + f"https://{BUCKET_NAME}.{BUCKET_REGION}.digitaloceanspaces.com/" + bucket_path |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +def upload_json(json_path: str) -> str: |
| 48 | + return upload_file(json_path, content_type="application/json") |
| 49 | + |
| 50 | + |
| 51 | +def upload_csv(csv_path: str) -> str: |
| 52 | + directory = Path(csv_path) |
| 53 | + zip_file_path = f"{csv_path}_csv.zip" |
| 54 | + with zipfile.ZipFile(zip_file_path, mode="w") as archive: |
| 55 | + for file_path in directory.rglob("*"): |
| 56 | + archive.write(file_path, arcname=file_path.relative_to(directory)) |
| 57 | + return upload_file(zip_file_path, content_type="application/zip") |
| 58 | + |
| 59 | + |
| 60 | +def upload_xlsx(xlsx_path: str) -> str: |
| 61 | + return upload_file( |
| 62 | + xlsx_path, |
| 63 | + content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", # noqa: E501 |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +def upload_files( |
| 68 | + json_path: Optional[str] = None, |
| 69 | + csv_path: Optional[str] = None, |
| 70 | + xlsx_path: Optional[str] = None, |
| 71 | +) -> tuple[Optional[str], Optional[str], Optional[str]]: |
| 72 | + # TODO: Option to delete local files once uploaded? |
| 73 | + # TODO: Exception handling |
| 74 | + if not ENABLE_UPLOAD: |
| 75 | + logger.info("Upload is disabled, skipping") |
| 76 | + return None, None, None |
| 77 | + logger.info("Uploading files") |
| 78 | + if json_path: |
| 79 | + json_public_url = upload_json(json_path) |
| 80 | + logger.info(f"Uploaded JSON file to {json_public_url}") |
| 81 | + if csv_path: |
| 82 | + csv_public_url = upload_csv(csv_path) |
| 83 | + logger.info(f"Uploaded CSV zip file to {csv_public_url}") |
| 84 | + if xlsx_path: |
| 85 | + xlsx_public_url = upload_xlsx(xlsx_path) |
| 86 | + logger.info(f"Uploaded XLSX file to {xlsx_public_url}") |
| 87 | + return json_public_url, csv_public_url, xlsx_public_url |
0 commit comments