def store_file(self, bucket_id, file_path, options=None, progress_callback=None, finished_callback=None): results = {} def handle_progress(progress, bytes, total_bytes): if progress_callback is not None: progress_callback(progress, bytes, total_bytes) def handle_finished(error, file_): # TODO: error handling based on `status` results['data'] = file_ if error is not None: error = Exception(error) results['error'] = error if finished_callback is not None: finished_callback(error, file_) upload_options = UploadOptions(bucket_id, file_path, options) pystorj.store_file(self.env, upload_options, handle_progress, handle_finished) pystorj.run(self.env.loop) return self._error_check(results)
def delete_bucket(self, bucket_id, callback=None): results = [] def handle(error): if error is not None: error = Exception(error) results.append(error) if callback is not None: callback(error) pystorj.delete_bucket(self.env, bucket_id, handle) pystorj.run(self.env.loop) return self._error_check(results)
def create_bucket(self, name, callback=None): results = [] def handle(error, bucket): if bucket is not None: results.append(bucket) if error is not None: error = Exception(error) results.append(error) if callback is not None: callback(error, bucket) pystorj.create_bucket(self.env, name, handle) pystorj.run(self.env.loop) return self._error_check(results)
def get_bucket_id(self, bucket_name, callback=None): results = {} def handle(error, bucket): if bucket is not None: results['data'] = bucket if error is not None: error = Exception(error) results['error'] = error if callback is not None: callback(error, bucket) pystorj.get_bucket_id(self.env, bucket_name, handle) pystorj.run(self.env.loop) return self._error_check(results)
def get_info(self, callback=None): results = [] def handle(error, result): # NB: method executes in a separate C thread! info = None try: info = json.loads(result) results.append(info) finally: if error is not None: error = Exception(error) results.append(error) if callback is not None: callback(error, info) pystorj.get_info(self.env, handle) pystorj.run(self.env.loop) return self._error_check(results)
def list_files(self, bucket_id, callback=None): results = {} def handle(error, files): if files is not None: for i, file_ in enumerate(files): iso8601_format = '%Y-%m-%dT%H:%M:%S.%fZ' created_date = datetime.strptime(file_['created'], iso8601_format) files[i]['created'] = created_date results['data'] = files if error is not None: error = Exception(error) results['error'] = error if callback is not None: callback(error, files) pystorj.list_files(self.env, bucket_id, handle) pystorj.run(self.env.loop) return self._error_check(results)