400 errors when uploading (meta)data or updating metadata
Uploading metadata and data or just updating metadata for a file in Coscine, returns HTTPS 400 errors.
This is the code:
import requests
requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS += 'DEFAULT:!DH'
from pathlib import Path
from datetime import datetime
import coscine
TOKEN_COS = 'mytoken'
client_settings = coscine.Settings(verbose=True)
client = coscine.Client(TOKEN_COS, settings=client_settings)
# enter project name
PROJECT: str = "NikkisSandbox"
# enter resource name
RESOURCE: str = 'DSA Test'
project = client.project(PROJECT, toplevel=False)
resource = project.resource(RESOURCE)
metadataForm = resource.metadata_form()
# filled out the metadata, all looks good :)
filepath = Path('testfile.txt')
filename = filepath.name
resource.upload(filename, str(filepath.absolute()), metadataForm)
Which returns:
---------------------------------------------------------------------------
HTTPError Traceback (most recent call last)
File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/coscine/client.py:382, in Client._request(self, method, uri, **kwargs)
381 response = self.session.request(method, uri, **kwargs)
--> 382 response.raise_for_status()
383 logger.debug("response: %s", str(response.content))
File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/requests/models.py:1021, in Response.raise_for_status(self)
1020 if http_error_msg:
-> 1021 raise HTTPError(http_error_msg, response=self)
HTTPError: 400 Client Error: Bad Request for url: https://coscine.rwth-aachen.de/coscine/api/Coscine.Api.Tree/Tree/0526503b-68c2-4793-8cf4-50e8ecadb15b?path=testfile.txt
The above exception was the direct cause of the following exception:
RuntimeError Traceback (most recent call last)
Cell In[81], line 1
----> 1 resource.upload(filename, str(filepath.absolute()), metadataForm)
File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/coscine/resource.py:536, in Resource.upload(self, key, file, metadata, callback)
534 elif isinstance(file, str):
535 with open(file, "rb") as file_handle:
--> 536 self._upload_file_metadata(key, metadata)
537 self._upload_file_data(key, file_handle, callback)
538 else:
File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/coscine/resource.py:548, in Resource._upload_file_metadata(self, key, metadata)
546 uri = self.client.uri("Tree", "Tree", self.id)
547 params = {"path": key}
--> 548 self.client.put(uri, json=metadata, params=params)
File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/coscine/client.py:463, in Client.put(self, uri, **kwargs)
432 def put(self, uri: str, **kwargs) -> requests.Response:
433 """
434 Performs a PUT request to the Coscine API.
435
(...)
460 The response of the Coscine server as a requests.Response object.
461 """
--> 463 return self._request("PUT", uri, **kwargs)
File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/coscine/client.py:390, in Client._request(self, method, uri, **kwargs)
388 if exc.response.status_code == 401:
389 raise PermissionError("Invalid Coscine API token!") from exc
--> 390 raise RuntimeError(
391 "Unspecified error occurred when communicating "
392 "with the Coscine servers"
393 ) from exc
RuntimeError: Unspecified error occurred when communicating with the Coscine servers
Alternatively, I tried updating metadata for an existing file:
f = resource.object('testfile.txt')
f.update(metadataForm)
Both return the following:
---------------------------------------------------------------------------
HTTPError Traceback (most recent call last)
File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/coscine/client.py:382, in Client._request(self, method, uri, **kwargs)
381 response = self.session.request(method, uri, **kwargs)
--> 382 response.raise_for_status()
383 logger.debug("response: %s", str(response.content))
File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/requests/models.py:1021, in Response.raise_for_status(self)
1020 if http_error_msg:
-> 1021 raise HTTPError(http_error_msg, response=self)
HTTPError: 400 Client Error: Bad Request for url: https://coscine.rwth-aachen.de/coscine/api/Coscine.Api.Tree/Tree/0526503b-68c2-4793-8cf4-50e8ecadb15b?path=testfile.txt
The above exception was the direct cause of the following exception:
RuntimeError Traceback (most recent call last)
Cell In[70], line 1
----> 1 f.update(metadataForm)
File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/coscine/utils.py:93, in parallelizable..inner(*args, **kwargs)
91 return future
92 else:
---> 93 return func(*args, **kwargs)
File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/coscine/object.py:395, in FileObject.update(self, metadata)
393 logger.info("Updating metadata of FileObject '%s'...", self.path)
394 uri = self.client.uri("Tree", "Tree", self.resource.id)
--> 395 self.client.put(uri, params={"path": self.path}, json=metadata)
396 self._metadata_cache_is_invalid = True
File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/coscine/client.py:463, in Client.put(self, uri, **kwargs)
432 def put(self, uri: str, **kwargs) -> requests.Response:
433 """
434 Performs a PUT request to the Coscine API.
435
(...)
460 The response of the Coscine server as a requests.Response object.
461 """
--> 463 return self._request("PUT", uri, **kwargs)
File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/coscine/client.py:390, in Client._request(self, method, uri, **kwargs)
388 if exc.response.status_code == 401:
389 raise PermissionError("Invalid Coscine API token!") from exc
--> 390 raise RuntimeError(
391 "Unspecified error occurred when communicating "
392 "with the Coscine servers"
393 ) from exc
RuntimeError: Unspecified error occurred when communicating with the Coscine servers```
I tried this in web and RDS-S3 resources with the same result.