文件存储方案FastDFS (存储图片)

FastDFS c语言 轻量级分布式系统
功能:文件存储、文件访问(文件上传下载)、文件同步,解决大容量存储和负载均衡的问题

架构: client、Tracker server 和 Storage server

文件索引:

一、Docker 安装运行FastDFS
1. 获取FastDFS镜像:

远端: sudo docker image pull delron/fastdfs
本地: sudo load -i fastdfs_docker.tar

2. 开启tracker容器: sudo docker run -dit --name=tracker --network=host -v /var/fdfs/tracker:/var/fdfs delron/fastdfs tracker

3. 开启storage容器: sudo docker run -dit --name=storage --network=host -e TRACKER_SERVER=192.168.121.200:22122 -v /var/fdfs/storage:/var/fdfs delron/fastdfs storage

4.若无法重启storage,可以删除/var/fdfs/storage/data目录下的 fdfs_storaged.pid,然后重新运行storage
二、 FastDFS的python客户端
1. 进入fdfs_client-py-master.zip所在目录下:

运行安装:
pip fdfs_client-py-master.zip      # 文件下载地址待补充 
pip install mutagen -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
pip install requests -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

三、FastDFS客户端的使用,需要有配置文件

1. 在utils文件下 新建fastdfs文件 并在其中添加 client.conf配置文件
2. 修改conf文件中:

base_path = FastDFS客户端存放日志文件的目录: base_path=/Users/meihao/Desktop
tracker_server = 运行tracker服务的机器ip:22122:tracker_server=tracker_server所在电脑ip:22122

3. shell测试fastdfs上传图片:

python manage.py shell 
from fdfs_client.client import Fdfs_client
#加载配置
client = Fdfs_client('meiduo_mall/utils/fastdfs/client.conf')
#上传文件
ret = client.upload_by_filename('/home/ubuntu/Desktop/meinv.jpg')

"Remote file_id" : group1/M00/00/00/wKh5iF7z8miABdiGAACiSTpJlaQ999.jpg

4.django项目中添加配置

# FDFS需要的配置文件路径(即: client.conf文件绝对路径).
FDFS_CLIENT_CONF = os.path.join(BASE_DIR, 'utils/fastdfs/client.conf')
# FDFS中storage和tracker位置.端口规定死是8888, ip换成自己的ip
FDFS_URL = 'http://192.168.121.136:8888/'
# 指定django系统使用的文件存储类:
DEFAULT_FILE_STORAGE = 'meiduo_mall.utils.fastdfs.storage.FastDFSStorage'

fastdfs跨ip访问 重启storage

django 模型类中的文件字段
在django中定义模型类时, 文件字段可以使用FileField(其他)和ImageField(图片)
django默认文件保存的过程
客户端 ===请求上传文件===> Django服务器(默认将文件保存到服务器本地)

推荐使用 MinIO文件服务,设置方便,易使用(https://docs.min.io/cn/minio-quickstart-guide.html) 

原文地址:https://www.cnblogs.com/yqyn-study/p/13444502.html