环境准备
- docker安装
在虚拟机安装docker后,执行下面的命令
# 下载fastDFS镜像
docker image pull delron/fastdfs
# 运行tracker
docker run -dti --network=host --name tracker -v /var/fdfs/tracker:/var/fdfs delron/fastdfs tracker
# 运行storage(TRACKER_SERVER这里使用ifconfig命令查看虚拟机的内网地址,不要使用本地地址127.0.0.1)
docker run -dti --network=host --name storage -e TRACKER_SERVER=10.211.55.5:22122 -v /var/fdfs/storage:/var/fdfs delron/fastdfs storage
# 查看镜像是否在运行
docker ps
# 查看镜像logs,无error即可
docker logs tracker
docker logs storage
- pip安装
安装相关pip第三方包
pip install py3fdfs
pip install mutagen
pip isntall requests
本地测试
对创建的fastDFS进行测试
- 新建client.conf
base_path=utils/fastdfs/logs
tracker_server=10.211.55.5:22122
connect_timeout=60
- 新建test.py
from fdfs_client.client import Fdfs_client
client = Fdfs_client('/utils/fastdfs/client.conf') # 注意要绝对路径
ret = client.upload_by_filename('/Users/delron/Desktop/1.png')
print(ret)
{'Group name': 'group1', 'Remote file_id': 'group1/M00/00/02/CtM3BVr-k6SACjAIAAJctR1ennA809.png', 'Status': 'Upload successed.', 'Local file name': '/Users/delron/Desktop/1.png', 'Uploaded size': '151.00KB', 'Storage IP': '10.211.55.5'}
Django配置
在apps下面新建utils文件夹,并在utils新建fastdfs文件夹(可根据个人项目需要建立)
- setting.py
# 自定义文件系统
# django文件存储
DEFAULT_FILE_STORAGE = 'apps.utils.fastdfs.fdfs_storage.FastDFSStorage'
# FastDFS
FDFS_URL = 'http://10.141.28.245:8888/'
FDFS_CLIENT_CONF = os.path.join(BASE_DIR, 'apps/utils/fastdfs/client.conf')
- 新建client.conf
# 在utils/fastdfs下新建文件client.conf
base_path = /web/logs
tracker_server = 10.141.28.245:22122
connect_timeout = 60
- 新建fdfs_storage.py
# 在utils/fastdfs下新建文件fdfs_storage.py
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, get_tracker_conf
import os
import logging
log = logging.getLogger(__name__)
@deconstructible
class FastDFSStorage(Storage):
def __init__(self, base_url=None, client_conf=None):
"""
初始化
:param base_url: 用于构造图片完整路径使用,图片服务器的域名
:param client_conf: FastDFS客户端配置文件的路径
"""
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 _open(self, name, mode='rb'):
"""
用不到打开文件,所以省略
"""
pass
def _save(self, name, content):
'''_save方法'''
conf_path = get_tracker_conf(self.client_conf)
client = Fdfs_client(conf_path)
imgdir = f'{os.path.join(settings.BASE_DIR, "apps/utils/fastdfs")}/tmp.{str(name).split(".")[1]}'
with open(imgdir, "wb+") as f:
f.write(content.read())
result = client.upload_by_filename(imgdir)
os.remove(imgdir)
if result.get('Status') != 'Upload successed.':
logging.error('上传文件到FastDFS失败')
raise Exception('上传文件到FastDFS失败')
filename = result.get('Remote file_id')
return filename.decode()
def url(self, name):
"""
返回文件的完整URL路径
:param name: 数据库中保存的文件名
:return: 完整的URL
"""
return self.base_url + name
def exists(self, name):
"""
判断文件是否存在,FastDFS可以自行解决文件的重名问题
所以此处返回False,告诉Django上传的都是新文件
:param name: 文件名
:return: False
"""
return False
相关错误
configparser.NoOptionError: No option 'connect_timeout' in section: '__config__'
该错误在于client.conf中没有配置connect_timeout超时时间或者配置文件的地址不正确。解决:在client.conf中添加connect_timeout=10后若仍报错,卸载原有的pip包,pip安装py3fdfs即可
TypeError: type object argument after ** must be a mapping, not str
卸载原有的pip包,pip安装py3fdfs即可,或者使用
from fdfs_client.client import get_tracker_conf
引入配置文件