fastDFS安装配置-Python客户端

  • A+
所属分类:软件开发

一:安装Python客户端

注:在 windows 环境下使用 pip install fdfs_client 会报错,所以直接安装是不行的,这里我们采用手动安装的方式。

fastDFS安装配置-Python客户端

1、下载并安装fdfs_client-py

下载地址:https://codeload.github.com/hay86/fdfs_client-py/zip/master

下载完成之后解压master文件并进入、通过workon命令进入虚拟环境。在安装之前我们需要手动去修改2个配置文件、不然安装过程会报错(ImportError: No module named sendfile)、把红框中的配置项用#注释掉。

fastDFS安装配置-Python客户端

fastDFS安装配置-Python客户端

 

用python setup.py install命令安装fdfs_client-py

fastDFS安装配置-Python客户端

 

安装完成之后直接将fdfs_client文件夹copy到python解释器的 /lib 目录下

fastDFS安装配置-Python客户端

fastDFS安装配置-Python客户端

 

2、安装 mutagen 和 requests

pip install mutagen

pip isntall requests

安装完成之后用pip list命令查看fdfs_client-py、mutagen和requests是否已经安装完成。

fastDFS安装配置-Python客户端

 

3、配置client.conf

fastDFS安装配置文章中提到的client.conf文件拷贝到项目目录中、也可以手动创建并修改、示例如下:

# connect timeout in seconds

# default value is 30s

connect_timeout=30

 

# network timeout in seconds

# default value is 30s

network_timeout=60

 

# the base path to store log files

base_path=../../log

 

# tracker_server can ocur more than once, and tracker_server format is

#  "host:port", host can be hostname or ip address

tracker_server=172.16.200.11:22122

 

#standard log level as syslog, case insensitive, value list:

### emerg for emergency

### alert

### crit for critical

### error

### warn for warning

### notice

### info

### debug

log_level=info

 

# if use connection pool

# default value is false

# since V4.05

use_connection_pool = false

 

# connections whose the idle time exceeds this time will be closed

# unit: second

# default value is 3600

# since V4.05

connection_pool_max_idle_time = 3600

 

# if load FastDFS parameters from tracker server

# since V4.05

# default value is false

load_fdfs_parameters_from_tracker=false

 

# if use storage ID instead of IP address

# same as tracker.conf

# valid only when load_fdfs_parameters_from_tracker is false

# default value is false

# since V4.05

use_storage_id = false

 

# specify storage ids filename, can use relative or absolute path

# same as tracker.conf

# valid only when load_fdfs_parameters_from_tracker is false

# since V4.05

storage_ids_filename = storage_ids.conf

 

 

#HTTP settings

http.tracker_server_port=8888

 

#use "#include" directive to include HTTP other settiongs

##include http.conf

 

我这里直接在项目目录中创建了client.conf文件

fastDFS安装配置-Python客户端

 

4、文件上传测试

导入模块:

from fdfs_client.client import Fdfs_client

 

导入配置文件:

注意:windows环境下绝对路径会发生转义,需要加上 "r" 说明是原生字符串

client = Fdfs_client(r'配置文件绝对路径')

我的示例:client = Fdfs_client(r'D:\EnvsProject\DailyFresh\utils\fdfs\client.conf')

 

上传图片:注意:windows环境下绝对路径会发生转义,需要加上 "r" 说明是原生字符串

client.upload_by_filename(r'图片文件绝对路径')

我的示例:client.upload_by_filename(r'D:\EnvsProject\DailyFresh\static\images\adv01.jpg')

 

上传成功之后会会看到下面的信息:

getting connection

<fdfs_client.connection.Connection object at 0x000000000564B2B0>

<fdfs_client.fdfs_protol.Tracker_header object at 0x000000000564B550>

 

{'Status': 'Upload successed.', 'Storage IP': u'172.16.200.11', 'Remote file_id': u'group1\\M00/00/00/rBDIC1xRAp6ABfo-AAA2pPk7JKw154.jpg', 'Group name': u'group1', 'Local file name': 'D:\\EnvsProject\\DailyFresh\\static\\images\\adv01.jpg', 'Uploaded size': '13.00KB'}

就代表上传成功了。

fastDFS安装配置-Python客户端

 

然后我们用172.16.200.11:8888/group1\\M00/00/00/rBDIC1xRAp6ABfo-AAA2pPk7JKw154.jpg去访问刚才上传的图片。

fastDFS安装配置-Python客户端

 

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: