def put_file(fp, fid_full_url, file_name='', http_headers=None): """ save fp(file-pointer, file-description) to a remote weed volume. eg: PUT http://127.0.0.1:8080/3,20392030920 addtional file_name and http_headers can help weed-server to decide content-type and other infos. eg: @file_name = 'hello.txt' or 'abc.jpg' or 'youknow.png', @http_headers = {'content-type' : 'image/png'} or @http_headers = {'content-type' : 'image/jpeg'} or @http_headers = {'content-type' : 'text/xml'} or @http_headers = {'content-type' : 'application/json'} """ pos = fp.tell() tmp_uploading_file_name = file_name or 'a.unknown' # print('fid_full_url is: "%s"' % fid_full_url) # print('fp position: %d' % fp.tell()) # print('fp info: length: %d' % len(fp.read())) # fp.seek(0) if http_headers: rsp = requests.post(fid_full_url, files={tmp_uploading_file_name: fp}, headers=http_headers) else: rsp = requests.post(fid_full_url, files={tmp_uploading_file_name: fp}) # recove position of fp fp.seek(pos) # g_logger.debug(rsp.request.headers) # rsp_json sample: # {'name': 'test_opensource_logo.jpg', 'size': 5447, 'eTag': '071b90970a036c6492af19430bd96351'} rsp_json = rsp.json() wor = WeedOperationResponse() wor.status = Status.SUCCESS wor.url = fid_full_url wor.name = rsp_json.get('name', '') wor.storage_size = rsp_json.get('size', 0) wor.etag = rsp_json.get('eTag', '') g_logger.info('wor is: %s' % wor) if 'error' in rsp_json: g_logger.error('Put file fails. Error returns from weedfs: "%s"' % rsp_json['error']) wor.status = Status.FAILED wor.message = rsp_json['error'] elif 'size' not in rsp_json or rsp_json['size'] == 0: # post new file fails err_msg = 'Could not save file on weed-fs with fid_full_url: %s' % fid_full_url g_logger.error(err_msg) wor.status = Status.FAILED wor.message = err_msg else: pass return wor
def delete_file(self, fid) -> bytes or None: """ Delete a file by @fid Deprecated. Use WeedOperation().delete instead. """ url = urljoin(self.url_base, fid) try: r = requests.delete(url) except Exception as e: g_logger.error("Could not delete file. Exception is: %s" % e) return None return r.content
def get_status(self) -> None or {}: """ get status of this volume Arguments: - `self`: """ r = requests.get(self.url_status) try: result = json.loads(r.content) except Exception as e: g_logger.error( "Could not get status of this volume: %s. Exception is: %s" % (self.url_status, e)) result = None return result
def __init__(self, json_of_weed_response=None): self['fid'] = '' self['count'] = 0 self['url'] = '' self['publicUrl'] = '' if json_of_weed_response: try: d = json.loads(json_of_weed_response) self.update(d) except Exception as e: g_logger.error('Error for json.loads "%s".\nException: %s' % (json_of_weed_response, e)) for k, v in list(self.items()): setattr(self, k, v) super(WeedAssignKey, self).__init__()
def get_file(self, fid) -> bytes or None: """ Get a file's content by @fid Deprecated. Use WeedOperation().get instead. """ url = urljoin(self.url_base, fid) try: r = requests.get(url) except Exception as e: g_logger.error("Could not get file. Exception is: %s" % e) return None if r.status_code == 200: return r.content elif r.status_code == 404: g_logger.error("File with fid %s not found" % fid) return None else: return None
def put_file(self, absolute_file_path, fid, headers=None) -> None or {}: """ you can put exact http-headers in @headers to help weed to clarify putting file. eg: @headers = {'content-type' : 'image/png'} or @headers = {'content-type' : 'image/jpeg'} or @headers = {'content-type' : 'text/xml'} or @headers = {'content-type' : 'application/json'} Deprecated. Use util.put_file instead. """ url = urljoin(self.url_base, fid) if headers and isinstance(headers, dict): files = { 'file': (open(absolute_file_path, 'rb')), 'headers': headers } else: files = {'file': (open(absolute_file_path, 'rb'))} try: r = requests.post(url, files=files) except Exception as e: g_logger.error("Could not post file. Exception is: %s" % e) return None # weed-fs returns a 200 but the content may contain an error result = json.loads(r.content) if r.status_code == 200: if 'error' in result: g_logger.error(result['error']) else: g_logger.debug(result) return result