@@ -28,61 +28,58 @@ def _get_client() -> Any:
28
28
)
29
29
30
30
31
- def _upload_file (local_path : str , bucket_path : str , content_type : str ) -> str :
32
- logger .info (f"Uploading file { local_path } " )
33
- client = _get_client ()
34
- client .upload_file (
35
- local_path ,
36
- BUCKET_NAME ,
37
- bucket_path ,
38
- ExtraArgs = {"ACL" : "public-read" , "ContentType" : content_type },
39
- )
40
- public_url = (
41
- f"https://{ BUCKET_NAME } .{ BUCKET_REGION } .digitaloceanspaces.com/" + bucket_path
42
- )
43
- logger .info (f"Uploaded to { public_url } " )
44
- return public_url
45
-
46
-
47
- def _upload_json (dataset_id : str , json_path : str ) -> Optional [str ]:
31
+ def _upload_file (local_path : str , bucket_path : str , content_type : str ) -> Optional [str ]:
48
32
try :
49
- return _upload_file (
50
- local_path = json_path ,
51
- bucket_path = f"{ dataset_id } /{ dataset_id } .json" ,
52
- content_type = "application/json" ,
33
+ logger .info (f"Uploading file { local_path } " )
34
+ client = _get_client ()
35
+ client .upload_file (
36
+ local_path ,
37
+ BUCKET_NAME ,
38
+ bucket_path ,
39
+ ExtraArgs = {"ACL" : "public-read" , "ContentType" : content_type },
53
40
)
41
+ public_url = (
42
+ f"https://{ BUCKET_NAME } .{ BUCKET_REGION } .digitaloceanspaces.com/"
43
+ + bucket_path
44
+ )
45
+ logger .info (f"Uploaded to { public_url } " )
46
+ return public_url
54
47
except Exception as e :
55
- logger .warning (f"Failed to upload { json_path } with error { e } " )
48
+ logger .warning (f"Failed to upload { local_path } with error { e } " )
56
49
return None
57
50
58
51
52
+ def _upload_json (dataset_id : str , json_path : str ) -> Optional [str ]:
53
+ return _upload_file (
54
+ local_path = json_path ,
55
+ bucket_path = f"{ dataset_id } /{ dataset_id } .json" ,
56
+ content_type = "application/json" ,
57
+ )
58
+
59
+
59
60
def _upload_csv (dataset_id : str , csv_path : str ) -> Optional [str ]:
60
61
try :
61
62
directory = Path (csv_path )
62
63
zip_file_path = f"{ csv_path } _csv.zip"
63
64
with zipfile .ZipFile (zip_file_path , mode = "w" ) as archive :
64
65
for file_path in directory .rglob ("*" ):
65
66
archive .write (file_path , arcname = file_path .relative_to (directory ))
66
- return _upload_file (
67
- local_path = zip_file_path ,
68
- bucket_path = f"{ dataset_id } /{ dataset_id } _csv.zip" ,
69
- content_type = "application/zip" ,
70
- )
71
67
except Exception as e :
72
- logger .warning (f"Failed to upload { csv_path } with error { e } " )
68
+ logger .warning (f"Failed to zip { csv_path } with error { e } " )
73
69
return None
70
+ return _upload_file (
71
+ local_path = zip_file_path ,
72
+ bucket_path = f"{ dataset_id } /{ dataset_id } _csv.zip" ,
73
+ content_type = "application/zip" ,
74
+ )
74
75
75
76
76
77
def _upload_xlsx (dataset_id : str , xlsx_path : str ) -> Optional [str ]:
77
- try :
78
- return _upload_file (
79
- local_path = xlsx_path ,
80
- bucket_path = f"{ dataset_id } /{ dataset_id } .xlsx" ,
81
- content_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" , # noqa: E501
82
- )
83
- except Exception as e :
84
- logger .warning (f"Failed to upload { xlsx_path } with error { e } " )
85
- return None
78
+ return _upload_file (
79
+ local_path = xlsx_path ,
80
+ bucket_path = f"{ dataset_id } /{ dataset_id } .xlsx" ,
81
+ content_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" , # noqa: E501
82
+ )
86
83
87
84
88
85
def upload_files (
0 commit comments