10
10
from fedml .api .fedml_response import FedMLResponse , ResponseCode
11
11
12
12
13
- # Todo (alaydshah): Add file size
14
13
class StorageMetadata (object ):
15
14
def __init__ (self , data : dict ):
16
15
self .dataName = data .get ("datasetName" , None )
17
16
self .description = data .get ("description" , None )
18
17
self .tags = data .get ("description" , None )
19
18
self .createdAt = data .get ("createTime" , None )
20
19
self .updatedAt = data .get ("updateTime" , None )
20
+ self .size = _get_size (data .get ("fileSize" ,None ))
21
21
22
22
23
- # Todo (alaydshah): Add file size while creating objects. Store service name in metadata
23
+ # Todo (alaydshah): Store service name in metadata
24
24
# Todo (alaydshah): If data already exists, don't upload again. Instead suggest to use update command
25
25
26
-
27
26
def upload (data_path , api_key , name , description , service , show_progress , out_progress_to_err , progress_desc ,
28
27
metadata ) -> FedMLResponse :
29
28
api_key = authenticate (api_key )
@@ -32,6 +31,9 @@ def upload(data_path, api_key, name, description, service, show_progress, out_pr
32
31
33
32
if user_id is None :
34
33
return FedMLResponse (code = ResponseCode .FAILURE , message = message )
34
+
35
+ if (not _check_data_path (data_path )):
36
+ return FedMLResponse (code = ResponseCode .FAILURE ,message = "Invalid data path" )
35
37
36
38
archive_path , message = _archive_data (data_path )
37
39
if not archive_path :
@@ -41,6 +43,7 @@ def upload(data_path, api_key, name, description, service, show_progress, out_pr
41
43
name = os .path .splitext (os .path .basename (archive_path ))[0 ] if name is None else name
42
44
file_name = name + ".zip"
43
45
dest_path = os .path .join (user_id , file_name )
46
+ file_size = os .path .getsize (archive_path )
44
47
45
48
file_uploaded_url = store .upload_file_with_progress (src_local_path = archive_path , dest_s3_path = dest_path ,
46
49
show_progress = show_progress ,
@@ -53,7 +56,7 @@ def upload(data_path, api_key, name, description, service, show_progress, out_pr
53
56
json_data = {
54
57
"datasetName" : name ,
55
58
"description" : description ,
56
- "fileSize" : "" ,
59
+ "fileSize" : file_size ,
57
60
"fileUrl" : file_uploaded_url ,
58
61
"tagNameList" : [],
59
62
}
@@ -228,6 +231,11 @@ def _get_storage_service(service):
228
231
else :
229
232
raise NotImplementedError (f"Service { service } not implemented" )
230
233
234
+ def _check_data_path (data_path ):
235
+ if not os .path .isdir (data_path ) or not os .path .isfile (data_path ):
236
+ return False
237
+ return True
238
+
231
239
232
240
def _archive_data (data_path : str ) -> (str , str ):
233
241
src_local_path = os .path .abspath (data_path )
@@ -350,3 +358,20 @@ def _get_data_from_response(message: str, response: requests.Response) -> (Respo
350
358
return ResponseCode .FAILURE , message , None
351
359
352
360
return ResponseCode .SUCCESS , "Successfully parsed data from response" , data
361
+
362
+ def _get_size (size_in_bytes :str )-> str :
363
+ size_str = ""
364
+ if (size_in_bytes ):
365
+ size = int (size_in_bytes )
366
+ size_in_gb = size / (1024 * 1024 * 1024 )
367
+ size_in_mb = size / (1024 * 1024 )
368
+ size_in_kb = size / 1024
369
+ if (size_in_gb >= 1 ):
370
+ size_str = f"{ size_in_gb :.2f} GB"
371
+ elif (size_in_mb >= 1 ):
372
+ size_str = f"{ size_in_mb :.2f} MB"
373
+ elif (size_in_kb >= 1 ):
374
+ size_str = f"{ size_in_kb :.2f} KB"
375
+ else :
376
+ size_str = f"{ size } B"
377
+ return size_str
0 commit comments