1
1
import logging
2
2
from pathlib import Path
3
- from shutil import make_archive , unpack_archive , move
3
+ from shutil import make_archive , move , unpack_archive
4
4
from tempfile import TemporaryDirectory
5
+ from typing import Optional , Union
5
6
6
7
from simcore_sdk .node_ports import config , filemanager
7
8
8
9
log = logging .getLogger (__name__ )
9
10
10
- def _create_s3_object (file_path : Path ) -> str :
11
- return "{}/{}/{}" .format (config .PROJECT_ID , config .NODE_UUID , file_path .name )
11
+ def _create_s3_object (file_path : Union [Path , str ]) -> str :
12
+ file_name = file_path .name if isinstance (file_path , Path ) else file_path
13
+ return f"{ config .PROJECT_ID } /{ config .NODE_UUID } /{ file_name } "
12
14
13
15
14
- async def _push_file (file_path : Path ):
16
+ async def _push_file (file_path : Path , rename_to : Optional [ str ] ):
15
17
store_id = 0 # this is for simcore.s3
16
18
s3_object = _create_s3_object (file_path )
17
19
log .info ("uploading %s to S3 to %s..." , file_path .name , s3_object )
@@ -20,16 +22,16 @@ async def _push_file(file_path: Path):
20
22
local_file_path = file_path )
21
23
log .info ("%s successfuly uploaded" , file_path )
22
24
23
- async def push (file_or_folder : Path ):
25
+ async def push (file_or_folder : Path , rename_to : Optional [ str ] = None ):
24
26
if file_or_folder .is_file ():
25
- return await _push_file (file_or_folder )
27
+ return await _push_file (file_or_folder , rename_to )
26
28
# we have a folder, so we create a compressed file
27
29
with TemporaryDirectory () as tmp_dir_name :
28
30
log .info ("compressing %s into %s..." , file_or_folder .name , tmp_dir_name )
29
31
# compress the files
30
- compressed_file_wo_ext = Path (tmp_dir_name ) / file_or_folder .stem
32
+ compressed_file_wo_ext = Path (tmp_dir_name ) / ( rename_to if rename_to else file_or_folder .stem )
31
33
archive_file = Path (make_archive (str (compressed_file_wo_ext ), 'zip' , root_dir = file_or_folder )) #, base_dir=folder))
32
- return await _push_file (archive_file )
34
+ return await _push_file (archive_file , rename_to )
33
35
34
36
async def _pull_file (file_path : Path ):
35
37
s3_object = _create_s3_object (file_path )
0 commit comments