@@ -406,3 +406,40 @@ def _upload_to_dataset_local(connector, client, datasetid, filepath):
406406 return uploadedfileid
407407 else :
408408 logger .error ("unable to upload local file %s (not found)" , filepath )
409+
410+
411+ def upload_multiple_files (connector , client , datasetid , filepaths , folder_id = None ):
412+ """Upload multiple files to existing Clowder dataset.
413+
414+ Keyword arguments:
415+ connector -- connector information, used to get missing parameters and send status updates
416+ client -- ClowderClient containing authentication credentials
417+ datasetid -- the dataset that the files should be associated with
418+ filepaths -- list of file paths to upload
419+ folder_id -- the folder that the files should be uploaded to
420+ """
421+ logger = logging .getLogger (__name__ )
422+
423+ files = []
424+ for filepath in filepaths :
425+ if os .path .exists (filepath ):
426+ files .append (os .path .basename (filepath ), open (filepath , 'rb' ))
427+ else :
428+ logger .error ("unable to upload file %s (not found)" , filepath )
429+ return None
430+
431+
432+ url = posixpath .join (client .host , 'api/v2/datasets/%s/filesMultiple' % datasetid )
433+ if folder_id is not None :
434+ url = '%s?folder_id=%s' % (url , folder_id )
435+
436+ headers = {"X-API-KEY" : client .key }
437+ response = connector .post (url , files = files , headers = headers ,
438+ verify = connector .ssl_verify if connector else True )
439+
440+ if response .status_code == 200 :
441+ return response .json ()
442+ else :
443+ logger .error ("Error uploading files to dataset %s" , datasetid )
444+ return None
445+
0 commit comments