Changeset View
Changeset View
Standalone View
Standalone View
swh/objstorage/backends/seaweed.py
Show All 29 Lines | class WeedFiler(object): | ||||
""" | """ | ||||
def __init__(self, url): | def __init__(self, url): | ||||
self.url = url | self.url = url | ||||
def get(self, remote_path): | def get(self, remote_path): | ||||
url = urljoin(self.url, remote_path) | url = urljoin(self.url, remote_path) | ||||
LOGGER.debug("Get file %s", url) | LOGGER.debug("Get file %s", url) | ||||
return requests.get(url).content | resp = requests.get(url) | ||||
resp.raise_for_status() | |||||
return resp.content | |||||
def exists(self, remote_path): | def exists(self, remote_path): | ||||
url = urljoin(self.url, remote_path) | url = urljoin(self.url, remote_path) | ||||
LOGGER.debug("Check file %s", url) | LOGGER.debug("Check file %s", url) | ||||
return requests.head(url).status_code == 200 | return requests.head(url).status_code == 200 | ||||
def put(self, fp, remote_path): | def put(self, fp, remote_path): | ||||
url = urljoin(self.url, remote_path) | url = urljoin(self.url, remote_path) | ||||
LOGGER.debug("Put file %s", url) | LOGGER.debug("Put file %s", url) | ||||
return requests.post(url, files={"file": fp}) | return requests.post(url, files={"file": fp}) | ||||
def delete(self, remote_path): | def delete(self, remote_path): | ||||
url = urljoin(self.url, remote_path) | url = urljoin(self.url, remote_path) | ||||
LOGGER.debug("Delete file %s", url) | LOGGER.debug("Delete file %s", url) | ||||
return requests.delete(url) | return requests.delete(url) | ||||
def list(self, dir, last_file_name=None, limit=DEFAULT_LIMIT): | def list(self, dir, last_file_name=None, limit=DEFAULT_LIMIT): | ||||
"""list sub folders and files of @dir. show a better look if you turn on | """list sub folders and files of @dir. show a better look if you turn on | ||||
returns a dict of "sub-folders and files" | returns a dict of "sub-folders and files" | ||||
""" | """ | ||||
d = dir if dir.endswith("/") else (dir + "/") | d = dir if dir.endswith("/") else (dir + "/") | ||||
url = urljoin(self.url, d) | url = urljoin(self.url, d) | ||||
headers = {"Accept": "application/json"} | headers = {"Accept": "application/json"} | ||||
params = {"limit": limit} | params = {"limit": limit} | ||||
if last_file_name: | if last_file_name: | ||||
params["lastFileName"] = last_file_name | params["lastFileName"] = last_file_name | ||||
LOGGER.debug("List directory %s", url) | LOGGER.debug("List directory %s", url) | ||||
rsp = requests.get(url, params=params, headers=headers) | rsp = requests.get(url, params=params, headers=headers) | ||||
if rsp.ok: | if rsp.ok: | ||||
return rsp.json() | return rsp.json() | ||||
else: | else: | ||||
LOGGER.error('Error listing "%s". [HTTP %d]' % (url, rsp.status_code)) | LOGGER.error('Error listing "%s". [HTTP %d]' % (url, rsp.status_code)) | ||||
vlorentz: would this work, as a minor simplification? | |||||
Done Inline Actionsneed to check against actual seaweed filer douardda: need to check against actual seaweed filer | |||||
class WeedObjStorage(ObjStorage): | class WeedObjStorage(ObjStorage): | ||||
"""ObjStorage with seaweedfs abilities, using the Filer API. | """ObjStorage with seaweedfs abilities, using the Filer API. | ||||
https://github.com/chrislusf/seaweedfs/wiki/Filer-Server-API | https://github.com/chrislusf/seaweedfs/wiki/Filer-Server-API | ||||
""" | """ | ||||
def __init__(self, url="http://127.0.0.1:8888/swh", compression=None, **kwargs): | def __init__(self, url="http://127.0.0.1:8888/swh", compression=None, **kwargs): | ||||
▲ Show 20 Lines • Show All 127 Lines • Show Last 20 Lines |
would this work, as a minor simplification?