diff --git a/CHANGES.md b/CHANGES.md index f37e847..712ddcf 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -5,8 +5,8 @@ Issue tracker: https://github.com/HumanBrainProject/pyunicore Version 1.1.0 (mmm dd, 2024) ---------------------------- - - new feature: added upload() method on PathFile object - - API CHANGE: remove Storage.upload() method + - API CHANGE: Storage.upload() method now accepts str-like or +file-like data to upload and requires destination name Version 1.0.1 (Mar 22, 2024) ---------------------------- diff --git a/pyunicore/client.py b/pyunicore/client.py index 1e9f684..9254639 100755 --- a/pyunicore/client.py +++ b/pyunicore/client.py @@ -407,7 +407,8 @@ def new_job(self, job_description, inputs=[], autostart=True): if len(inputs) > 0: working_dir = job.working_dir for input_item in inputs: - working_dir.upload(input_item) + with open(input_item, "rb") as f: + working_dir.upload(f, input_item) if autostart and job_description.get("haveClientStageIn", None) == "true": job.start() return job @@ -725,6 +726,20 @@ def makedirs(self, name): """create directory""" return self.mkdir(name) + def upload(self, source, destination): + """upload data to the destination file on this storage + + Args: + source (str-like or file-like): this will be uploaded + destination: target path (parent directory will be created if needed) + + """ + _headers = {"Content-Type": "application/octet-stream"} + with self.transport.put( + url=self._to_file_url(destination), headers=_headers, stream=True, data=source + ) as r: + r.close() + def send_file( self, file_name, @@ -888,19 +903,6 @@ def download(self, file): for chunk in resp.iter_content(chunk_size): file.write(chunk) - def upload(self, source): - """upload data or file - - Args: - source (str-like or file-like): this will be uploaded - - """ - _headers = {"Content-Type": "application/octet-stream"} - with self.transport.put( - url=self.resource_url, headers=_headers, stream=True, data=source - ) as r: - r.close() - def raw(self, offset=0, size=-1): """access the raw http response for a streaming download. The optional 'offset' and 'size' parameters allow to download only diff --git a/tests/integration/test_storage.py b/tests/integration/test_storage.py index d0ef37d..54629b7 100644 --- a/tests/integration/test_storage.py +++ b/tests/integration/test_storage.py @@ -45,9 +45,9 @@ def test_upload_download(self): home = self.get_home_storage() _path = "tests/integration/files/script.sh" _length = os.stat(_path).st_size - remote_file = home.stat("script.sh") with open(_path, "rb") as f: - remote_file.upload(f) + home.upload(f, "script.sh") + remote_file = home.stat("script.sh") self.assertEqual(_length, int(remote_file.properties["size"])) _out = BytesIO() remote_file.download(_out) @@ -58,8 +58,8 @@ def test_upload_download_data(self): home = self.get_home_storage() _data = "this is some test data" _length = len(_data) + home.upload(_data, "test.txt") remote_file = home.stat("test.txt") - remote_file.upload(_data) self.assertEqual(_length, int(remote_file.properties["size"])) _out = BytesIO() remote_file.download(_out) @@ -71,7 +71,7 @@ def test_transfer(self): _path = "tests/integration/files/script.sh" _length = os.stat(_path).st_size with open(_path, "rb") as f: - storage1.stat("script.sh").upload(f) + storage1.upload(f, "script.sh") site_client = self.get_client() storage2 = site_client.new_job({}).working_dir transfer = storage2.receive_file(storage1.resource_url + "/files/script.sh", "script.sh")