FastDFS上传
1.什么是FastDFS
FastDFS 是用C语言编写的一款开源的分布式文件系统。FastDFS 为互联网量身定制, 充分考虑了冗余备份、负载均衡、线性扩容等机制,并注重高可用、高性能等指标,使用 FastDFS 很容易搭建一套高性能的文件服务器集群提供文件上传、下载等服务。
具体代码https://github.com/JaceHo/fdfs_client-py:github参考代码
2.安装步骤
*step1:*安装准备好的fdfs_client-py-master.zip到虚拟环境中(如果虚拟环境在Ubuntu中拷贝到Ubuntu某个路径下)
pip install fdfs_client-py-master.zip
安装完之后还需要继续安装如下两个包,否者会报no module name错误
pip install mutagen
pip isntall requests
*step2.*准备FastDFS客户端扩展的配置文件client.conf。让平台知道在什么地方对接的,需要在util里面建立一个包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=
# tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
tracker_server=XX: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=80
#use "#include" directive to include HTTP other settiongs
##include http.conf
注:
base_path=(随便选择一个Ubuntu的某个路径,FastDFS客户端存放日志文件的目录)
tracker_server=172.16.133.132(运行Tracker服务的机器ip):22122(为了访问,端口不能变)
*step3:*FastDFS客户端实现文件存储
# 使用 shell 进入 Python交互环境
python manage.py shell
其中/kk.jpeg
# 1. 导入FastDFS客户端扩展
from fdfs_client.client import Fdfs_client
# 2. 创建FastDFS客户端对象
client = Fdfs_client('/utils/fastdfs/client.conf')
# 3. 调用FastDFS客户端上传文件方法/kk.jpeg是一个图片
ret = client.upload_by_filename('/Users/Desktop/kk.jpeg')
会出现现:No space left on device(并没有很多存储空间啦,只是磁盘空间不够,但是上传依然可以)
返回值是一个字典模式,Remote file_id上传之后最重要的 file_id是
ret = {
'Group name': 'group1',
'Remote file_id': 'group1/M00/00/00/wKhnnlxw_gmAcoWmAAEXU5wmjPs35.jpeg',
'Status': 'Upload successed.',
'Local file name': '/Users/zhangjie/Desktop/kk.jpeg',
'Uploaded size': '69.00KB',
'Storage IP': '172.16.133.132'
}
ret = {
'Group name': 'Storage组名',
'Remote file_id': '文件索引,可用于下载',
'Status': '文件上传结果反馈',
'Local file name': '上传文件全路径',
'Uploaded size': '文件大小',
'Storage IP': 'Storage地址'
}