以fast_fdfs作为存储后端
环境搭建fdfs
sudo docker run -d --network=host --name tracker -v /var/fdfs/tracker:/var/fdfs delron/fastdfs tracker
sudo docker run -d --network=host --name storage -e TRACKER_SERVER=192.168.80.131:22122 -v /var/fdfs/storage:/var/fdfs delron/fastdfs storage
客户端安装
https://github.com/JaceHo/fdfs_client-py
-
克隆到本地修改
-
修改文件,安装时会报错
setup.py 注释两行
'ext_modules': [Extension('fdfs_client.sendfile',
sources=['fdfs_client/sendfilemodule.c'])],
fdfs_client-py/fdfs_client/storage_client.py 注释以下行
from fdfs_client.sendfile import *
如果在使用时报关于字符编码错可以在 fdfs_client-py/fdfs_client/utils.py
def read(self, filenames):
if isinstance(filenames, basestring):
filenames = [filenames]
read_ok = []
for filename in filenames:
try:
#文件打开编码为utf-8
with open(filename,encoding='utf-8') as fp:
self.readfp(fp)
except IOError as e:
continue
else:
read_ok.append(filename)
return read_ok
安装 fdfs_client
python setup.py install
客户端配置文件(客户端会调用配置文件得到client对象)
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=FastDFS客户端存放日志文件的目录
# tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
tracker_server = fdfs_server: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
测试:fdfs_client
test.py
from fdfs_client.client import Fdfs_client
with open('./0.jpg','rb') as content:
client = Fdfs_client('./client.conf')
ret = client.upload_by_buffer(content.read())
print(ret)
{
'Group name': 'group1',
'Local file name': ''',
'Storage IP': '192.168.80.131',
'Remote file_id': 'group1/M00/00/03/rBCzi10HvhOAbpotAAAi00HB9mU322.jpg',
'Uploaded size': '8.00KB',
'Status': 'Upload successed.'
}
存储后端
from django.conf import settings
from django.core.files.storage import Storage
from django.utils.deconstruct import deconstructible
from fdfs_client.client import Fdfs_client
@deconstructible
class FastDFSStorage(Storage):
def __init__(self,base_url=None,client_conf=None):
if base_url is None:
base_url = settings.FDFS_URL
self.base_url = base_url
if client_conf is None:
client_conf = settings.FDFS_CLIENT_CONF
self.client_conf = client_conf
def _save(self,name,content):
#截取文件后缀
suffix = name.split('.')[1]
client = Fdfs_client(self.client_conf)
ret = client.upload_by_buffer(content.read(),suffix)
# print(ret)
if ret.get("Status") != "Upload successed.":
raise Exception("upload file faild")
file_name = ret.get('Remote file_id')
return file_name
def exists(self,name):
return False
def url(self,name):
# print(self.base_url+name)
http_url = self.base_url + name
print(http_url)
return http_url
setting.py配置
FDFS_URL = "http://192.168.80.131:8888/"
DEFAULT_FILE_STORAGE = 'fastdfs.storage.FastDFSStorage'
FDFS_CLIENT_CONF = os.path.join(BASE_DIR,'index/client.conf')
model.py
class Avatar(models.Model):
name = models.CharField(max_length=32)
#upload_to 自定义了存储后端此处填什么没有影响
avatar = models.FileField(upload_to='icon',verbose_name='头像')
admin.py
admin.site.register(Avatar)
image.png
views.py
def show_image(request):
if request.method == 'GET':
icons = models.Icon.objects.all()
url = []
for icon in icons:
url.append(icon.icon.url)
print(url)
return HttpResponse("<image src='{}'>".format(url[0]))
# return render(request,'show_image.html',{'url':url})
网友评论