def acquire_new_assign_key(self, count=1):
        """
        get a new avalable new volume-file-location from from weed-master by getting a new-assign-key
        Arguments:
        - `self`:

        assign_key is in json format like below:
        -----------
        {"count":1,"fid":"3,01637037d6","url":"127.0.0.1:8080","publicUrl":"localhost:8080"}
        -----------

        return a tuple(dst_file_fid, dst_volume_url) like below:
        ----------
        (dst_file_fid, http://{volume-url})
        (3,20392030920, http://127.0.0.1:8080)
        ----------

        """
        assign_key_url = self.url_assign + '?count=' + str(count)
        dst_volume_url = None
        wak = WeedAssignKeyExtended()
        try:
            LOGGER.debug('Getting new dst_volume_url with master-assign-key-url: %s' % assign_key_url)
            r = requests.get(assign_key_url)
            key_dict = json.loads(r.content)

            wak.update(key_dict)
            wak.update_full_urls()

            LOGGER.info('Successfuly got dst_volume_url: %s' % dst_volume_url)
        except Exception as e:
            LOGGER.error('Could not get new assign key from the assign url: %s. Exception is: %s'
                         % (assign_key_url, e))
        return wak
示例#2
0
def put_file(fp, fid_full_url, fname='', 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 fname and http_headers can help weed-server to decide content-type and other infos.

    eg:
       @fname = '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 = fname 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)

    # LOGGER.debug(rsp.request.headers)
    rsp_json = rsp.json()
    wor = WeedOperationResponse()
    wor.status = 'success'
    wor.url = fid_full_url
    wor.fname = fname
    wor.storage_size = rsp.json().get('size', 0)
    LOGGER.info('wor is: %s' % wor)

    if 'error' in rsp_json:
        LOGGER.error('Put file fails. Error returns from weedfs: "%s"' %
                     rsp_json['error'])
        wor.status = 'fail'
        wor.message = rsp_json['error']
    elif not rsp_json.has_key(
            'size') 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)
        LOGGER.error(err_msg)
        wor.status = 'fail'
        wor.message = err_msg
    else:
        pass

    return wor
示例#3
0
def put_file(fp, fid_full_url, fname='', 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 fname and http_headers can help weed-server to decide content-type and other infos.

    eg:
       @fname = '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 = fname 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)

    # LOGGER.debug(rsp.request.headers)
    rsp_json = rsp.json()
    wor = WeedOperationResponse()
    wor.status == 'success'
    wor.url = fid_full_url
    wor.fname = fname
    wor.storage_size = rsp.json().get('size', 0)
    LOGGER.info('wor is: %s' % wor)

    if 'error' in rsp_json:
        LOGGER.error('Put file fails. Error returns from weedfs: "%s"' % rsp_json['error'])
        wor.status = 'fail'
        wor.message = rsp_json['error']
    elif not rsp_json.has_key('size') 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)
        LOGGER.error(err_msg)
        wor.status = 'fail'
        wor.message = err_msg
    else:
        pass

    return wor
示例#4
0
    def acquire_new_assign_key(self, count=1):
        """
        get a new avalable new volume-file-location from from weed-master by getting a new-assign-key
        Arguments:
        - `self`:

        assign_key is in json format like below:
        -----------
        {"count":1,"fid":"3,01637037d6","url":"127.0.0.1:8080","publicUrl":"localhost:8080"}
        -----------

        return a tuple(dst_file_fid, dst_volume_url) like below:
        ----------
        (dst_file_fid, http://{volume-url})
        (3,20392030920, http://127.0.0.1:8080)
        ----------

        """
        assign_key_url = self.url_assign + '?count=' + str(count)
        dst_volume_url = None
        wak = WeedAssignKeyExtended()
        try:
            LOGGER.debug(
                'Getting new dst_volume_url with master-assign-key-url: %s' %
                assign_key_url)
            r = requests.get(assign_key_url)
            key_dict = json.loads(r.content)

            wak.update(key_dict)
            wak.update_full_urls()

            LOGGER.info('Successfuly got dst_volume_url: %s' % dst_volume_url)
        except Exception as e:
            LOGGER.error(
                'Could not get new assign key from the assign url: %s. Exception is: %s'
                % (assign_key_url, e))
        return wak