@@ -180,6 +180,53 @@ def request(
180
180
181
181
return response
182
182
183
+ def upload_to_presigned_url (
184
+ self ,
185
+ data : Union [Dict [str , Any ], str ],
186
+ url : str ,
187
+ ) -> None :
188
+ """
189
+ Method to upload data to a presigned URL of the Nextmv Cloud API.
190
+ Args:
191
+ data: data to upload.
192
+ url: URL to upload the data to.
193
+ """
194
+
195
+ upload_data = None
196
+ if isinstance (data , Dict ):
197
+ upload_data = json .dumps (data , separators = ("," , ":" ))
198
+ elif isinstance (data , str ):
199
+ upload_data = data
200
+ else :
201
+ raise ValueError ("data must be a dictionary or a string" )
202
+
203
+ session = requests .Session ()
204
+ retries = Retry (
205
+ total = self .max_retries ,
206
+ backoff_factor = self .backoff_factor ,
207
+ backoff_jitter = self .backoff_jitter ,
208
+ backoff_max = self .backoff_max ,
209
+ status_forcelist = self .status_forcelist ,
210
+ allowed_methods = self .allowed_methods ,
211
+ )
212
+ adapter = HTTPAdapter (max_retries = retries )
213
+ session .mount ("https://" , adapter )
214
+ kwargs = {
215
+ "url" : url ,
216
+ "timeout" : self .timeout ,
217
+ "data" : upload_data ,
218
+ }
219
+
220
+ response = session .put (** kwargs )
221
+
222
+ try :
223
+ response .raise_for_status ()
224
+ except requests .HTTPError as e :
225
+ raise requests .HTTPError (
226
+ f"upload to presigned URL { url } failed with "
227
+ + f"status code { response .status_code } and message: { response .text } "
228
+ ) from e
229
+
183
230
def _set_headers_api_key (self , api_key : str ) -> None :
184
231
"""Sets the API key to use for requests to the Nextmv Cloud API."""
185
232
@@ -202,5 +249,8 @@ def get_size(obj: Union[Dict[str, Any], IO[bytes]]) -> int:
202
249
obj .seek (0 ) # Reset the cursor to the beginning of the file
203
250
return size
204
251
252
+ elif isinstance (obj , str ):
253
+ return len (obj .encode ("utf-8" ))
254
+
205
255
else :
206
256
raise TypeError ("Unsupported type. Only dictionaries and file objects are supported." )
0 commit comments