class SelectelApi(object): __metaclass__ = Singleton def __init__(self, user=None, password=None, container=None): self.user = user self.password = password self.container = container self.connection = Storage(self.user, self.password) def add(self, path, file_obj): self._make_call('put', self.container, path, file_obj) def get(self, path): return self._make_call('get_stream', self.container, path) def delete(self, path): self._make_call('remove', self.container, path) @retry((ReadTimeout, HTTPError), delay=1.5) def _make_call(self, method, *args): try: return getattr(self.connection, method)(*args) except HTTPError as e: if e.response.status_code == 401: self.connection.authenticate() raise @retry((ReadTimeout, HTTPError), delay=1.5) def info(self): url = "%s/%s" % (self.connection.auth.storage, self.container) try: r = self.connection.session.head(url, verify=True) r.raise_for_status() except HTTPError as e: if e.response.status_code == 401: self.connection.authenticate() raise result = { 'used': int(r.headers['X-Container-Bytes-Used']), 'quota': int(r.headers['X-Container-Meta-Quota-Bytes']), 'count': int(r.headers['X-Container-Object-Count']) } return result
def __init__(self, user=None, password=None, container=None): self.user = user self.password = password self.container = container self.connection = Storage(self.user, self.password)