@@ -78,8 +78,9 @@ def upload(
7878 presigned_url_response = presigned_url_response ,
7979 )
8080 else :
81- return self .transfer_blob (
81+ return self .upload_blob_local (
8282 file_path = file_path ,
83+ object_name = object_name ,
8384 presigned_url_response = presigned_url_response ,
8485 )
8586
@@ -105,7 +106,9 @@ def upload_blob_s3(
105106 fields = presigned_url_response .fields
106107 fields ["file" ] = (object_name , f , "application/x-tar" )
107108 e = MultipartEncoder (fields = fields )
108- m = MultipartEncoderMonitor (e , lambda monitor : t .update (min (t .total , monitor .bytes_read ) - t .n ))
109+ m = MultipartEncoderMonitor (
110+ e , lambda monitor : t .update (min (t .total , monitor .bytes_read ) - t .n )
111+ )
109112 headers = {"Content-Type" : m .content_type }
110113 res = requests .post (
111114 presigned_url_response .url ,
@@ -116,7 +119,9 @@ def upload_blob_s3(
116119 )
117120 return res
118121
119- def upload_blob_gcs (self , file_path : str , presigned_url_response : PresignedURLCreateResponse ):
122+ def upload_blob_gcs (
123+ self , file_path : str , presigned_url_response : PresignedURLCreateResponse
124+ ):
120125 """Generic method to upload data to Google Cloud Storage and create the
121126 appropriate resource in the backend.
122127 """
@@ -137,7 +142,9 @@ def upload_blob_gcs(self, file_path: str, presigned_url_response: PresignedURLCr
137142 )
138143 return res
139144
140- def upload_blob_azure (self , file_path : str , presigned_url_response : PresignedURLCreateResponse ):
145+ def upload_blob_azure (
146+ self , file_path : str , presigned_url_response : PresignedURLCreateResponse
147+ ):
141148 """Generic method to upload data to Azure Blob Storage and create the
142149 appropriate resource in the backend.
143150 """
@@ -161,19 +168,34 @@ def upload_blob_azure(self, file_path: str, presigned_url_response: PresignedURL
161168 )
162169 return res
163170
164- def transfer_blob (
171+ def upload_blob_local (
165172 self ,
166173 file_path : str ,
174+ object_name : str ,
167175 presigned_url_response : PresignedURLCreateResponse ,
168176 ):
169177 """Generic method to transfer data to the openlayer folder and create the
170178 appropriate resource in the backend when using a local deployment.
171179 """
172- blob_path = presigned_url_response .storage_uri .replace ("local://" , "" )
173- dir_path = os .path .dirname (blob_path )
174- try :
175- os .makedirs (dir_path , exist_ok = True )
176- except OSError as exc :
177- raise _exceptions .OpenlayerError (f"Directory { dir_path } cannot be created" ) from exc
178- shutil .copyfile (file_path , blob_path )
179- return None
180+ with tqdm (
181+ total = os .stat (file_path ).st_size ,
182+ unit = "B" ,
183+ unit_scale = True ,
184+ unit_divisor = 1024 ,
185+ colour = "BLUE" ,
186+ ) as t :
187+ with open (file_path , "rb" ) as f :
188+ fields = {"file" : (object_name , f , "application/x-tar" )}
189+ e = MultipartEncoder (fields = fields )
190+ m = MultipartEncoderMonitor (
191+ e , lambda monitor : t .update (min (t .total , monitor .bytes_read ) - t .n )
192+ )
193+ headers = {"Content-Type" : m .content_type }
194+ res = requests .post (
195+ presigned_url_response .url ,
196+ data = m ,
197+ headers = headers ,
198+ verify = VERIFY_REQUESTS ,
199+ timeout = REQUESTS_TIMEOUT ,
200+ )
201+ return res
0 commit comments