大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE稳定放心使用
使用这个库,遇到不少问题,搜索加查看源码,暂时能用了~
py3Fdfs 2.2.0
安装:pip install py3Fdfs
TypeError: type object argument after ** must be a mapping, not str
调整代码为:
#由配置文件中的信息得到字典trackers
trackers = get_tracker_conf('../config/fdfs_client.conf')
self.client = Fdfs_client(trackers)
download_to_file(local_filename, remote_file_id),提示must be bytes[], not str
看源代码发现,在utils.py 222行
index = remote_file_id.find(b'/')
但是注意,这里去掉’b’,后面还有很多错误。
仔细查询后发现,是struct格式化字符串的问题,在python3发生了变化。utils.py还原~
Python3 struct格式化
在python2中’s’是string类型,改为了bytes,进参前做encoding:
remote_file_id = remote_file_id.encode(encoding='utf-8')
下载30k文件出现socket超时
使用donwload_to_file出现;反复尝试,无奈换一个方法调用donwload_to_filebuffer
增加上传文件指定group
发现api中,无对应方法。
位置:D:\ProgramData\Anaconda3\Lib\site-packages\fdfs_client
阅读源代码后发现在tracker_client.py中有方法获取group,如下:
def tracker_query_storage_stor_with_group(self, group_name):
'''Query storage server for upload, based group name. arguments: @group_name: string @Return Storage_server object '''
conn = self.pool.get_connection()
th = Tracker_header()
th.cmd = TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP_ONE
th.pkg_len = FDFS_GROUP_NAME_MAX_LEN
th.send_header(conn)
group_fmt = '!%ds' % FDFS_GROUP_NAME_MAX_LEN
send_buffer = struct.pack(group_fmt, group_name)
try:
tcp_send_data(conn, send_buffer)
th.recv_header(conn)
if th.status != 0:
raise DataError('Error: %d, %s' % (th.status, os.strerror(th.status)))
recv_buffer, recv_size = tcp_recv_response(conn, th.pkg_len)
if recv_size != TRACKER_QUERY_STORAGE_STORE_BODY_LEN:
errmsg = '[-] Error: Tracker response length is invaild, '
errmsg += 'expect: %d, actual: %d' % (TRACKER_QUERY_STORAGE_STORE_BODY_LEN, recv_size)
raise ResponseError(errmsg)
except ConnectionError:
raise
finally:
self.pool.release(conn)
# recv_fmt: |-group_name(16)-ipaddr(16-1)-port(8)-store_path_index(1)-|
recv_fmt = '!%ds %ds Q B' % (FDFS_GROUP_NAME_MAX_LEN, IP_ADDRESS_SIZE - 1)
store_serv = Storage_server()
(group, ip_addr, store_serv.port, store_serv.store_path_index) = struct.unpack(recv_fmt, recv_buffer)
store_serv.group_name = group.strip(b'\x00')
store_serv.ip_addr = ip_addr.strip(b'\x00')
return store_serv
在client.py中增加一个新的方法,注意这里的参数group_name仍然需要转换为bytes[]
# 指定group上传文件
def upload_by_filename_with_gourp(self, filename, group_name, meta_dict=None):
isfile, errmsg = fdfs_check_file(filename)
if not isfile:
raise DataError(errmsg + '(uploading)')
tc = Tracker_client(self.tracker_pool)
store_serv = tc.tracker_query_storage_stor_with_group(group_name)
store = Storage_client(store_serv.ip_addr, store_serv.port, self.timeout)
return store.storage_upload_by_filename(tc, store_serv, filename, meta_dict)
# 指定group上传可追加文件
def upload_appender_by_filename_with_group(self, local_filename, group_name, meta_dict=None):
isfile, errmsg = fdfs_check_file(local_filename)
if not isfile:
raise DataError(errmsg + '(uploading appender)')
tc = Tracker_client(self.tracker_pool)
store_serv = tc.tracker_query_storage_stor_with_group(group_name)
store = Storage_client(store_serv.ip_addr, store_serv.port, self.timeout)
return store.storage_upload_appender_by_filename(tc, store_serv, local_filename, meta_dict)
部分代码
# -*- coding: utf-8 -*-
""" Created on Wed Jul 24 14:31:45 2019 @author: yk """
from config.loki_config import lcfg
import os
from fdfs_client.client import Fdfs_client, get_tracker_conf
class FDfsClient(object):
def __init__(self):
# 由配置文件中的信息,得到字典trackers
trackers = get_tracker_conf(lcfg.FDFS_PATH)
self.client = Fdfs_client(trackers)
def upload_filename(self, file_path):
res = self.client.upload_by_filename(file_path)
if res.get('Status') != 'Upload successed.':
raise Exception('upload file to fastdfs failed. path:{}'.format(file_path))
filename = res.get('Remote file_id')
return filename.decode()
def upload_filename_with_group(self, file_path, group_name=lcfg.FDFS_GROUP_NAME):
group_name = group_name.encode(encoding='utf-8')
res = self.client.upload_by_filename_with_gourp(file_path, group_name)
if res.get('Status') != 'Upload successed.':
raise Exception('upload file to fastdfs failed. path:{}'.format(file_path))
filename = res.get('Remote file_id')
return filename.decode()
# 上传文件使其后续可以追加,修改等
def upload_appender_by_filename(self, file_path):
res = self.client.upload_appender_by_filename(file_path)
if res.get('Status') != 'Upload successed.':
raise Exception('upload_appender file to fastdfs failed. path:{}'.format(file_path))
filename = res.get('Remote file_id')
return filename.decode()
# 追加文件内容
def append_by_filename(self, file_path, remote_file_id):
remote_file_id = remote_file_id.encode(encoding='utf-8')
res = self.client.append_by_filename(file_path, remote_file_id)
if res.get('Status') != 'Append file successed.':
raise Exception('append file to fastdfs failed. file_path: {} || remote_file_id:{}'.format(file_path, remote_file_id))
def download_file(self, local_path, remote_file_id):
filename = remote_file_id.split('/')[-1]
remote_file_id = remote_file_id.encode(encoding='utf-8')
filename = os.path.join(local_path, filename)
res = self.client.download_to_buffer(remote_file_id)
with open(filename, mode='ab+') as f:
f.write(res.get('Content'))
return filename
raise Exception('download file from fastdfs failed. remote_id:{}'.format(remote_file_id))
def list_all_groups(self):
return self.client.list_all_groups()
def main():
filename = r'D:\workspace\iot-loki\util\1.txt'
group_name = 'group1'
fdfs_client = FDfsClient()
# res = fdfs_client.upload_filename(filename)
res = fdfs_client.upload_appender_by_filename(filename)
print(res)
# local = r"D:\workspace\iot-loki\util\1.txt"
## remote_id = "/sampling/ts/20191223/AACHD52496857/1577080025-1577080035.ts"
# remote_id = "group1/M00/04/7A/Ct39y14DDtuEEEEGAAAAAPvT3Cw584.txt"
# res = fdfs_client.append_by_filename(local, remote_id)
# res = fdfs_client.download_file('./', 'group1/M00/04/7A/Ct39y14DDtuEEEEGAAAAAPvT3Cw584.txt')
# print(res)
if __name__ == '__main__':
main()
部署替换linux下文件
找到对应包安装路径,使用命令:pip show py3Fdfs
获得信息:
Name: py3Fdfs
Version: 2.2.0
Summary: Python3 and Python2 client for Fastdfs
Home-page: http://www.google.com
Author: wwb
Author-email: 416713448@qq.com
License: GPLV3
Location: /usr/local/python3.6.8/lib/python3.6/site-packages
Requires:
Required-by:
到对应路径找到fdfs_client文件夹,将本地client.py替换上就好了。
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/179416.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...