键入docker指令:sudo docker image pull delron/fastdfs
键入docker指令
docker run -dit --network=host --name storage -e TRACKER_SERVER=192.168.87.128:22122 -v /var/fdfs/storage:/var/fdfs delron/fastdfs storage
启动storage
22122为tracker默认端口号 前面的IP为本机的IP,可以使用ipconfig找到,注意由于DHCP每次分配ip的原因,可能在重启电脑时会改变IP,这样需要手动改一下
docker run -dit --network=host --name=tracker -v /var/fdfs/tracker:/var/fdfs delron/fastdfs tracker
启动tracker
然后查看一下正在运行的容器
启动完成
如果启动不了storage服务器,只要通过命令行找到/var/fdfs/storage/data,然后ls找到fdfs_storaged.pid删除这个文件
键入以下命令删除:sudo rm -f fdfs_storaged.pid
从fdfs官方文档中找到官方python包的git地址https://github.com/JaceHo/fdfs_client-py,进入git中下载一下.zip文件,然后在.zip文件路径下进入虚拟环境并键入
pip install fdfs_client-py-master.zip
pip install mutagen
pip install requests
文件位置任选,我放在我的同名文件夹中的utils文件夹中,因为比较好找。别忘了是创建python_package,当然你也可以创建文件夹写一个__init__.py文件。
创建client.conf文件,最好直接用text的,否则不停飘红
# the base path to store log files
base_path=/home/lvbu89757/shanghuishop/shanghuishop/logs
# tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
tracker_server=192.168.211.130:22122
这两条是必填项,为了设置日志和告诉程序tracker路径
# 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=/home/lvbu89757/shanghuishop/shanghuishop/logs
# tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
tracker_server=192.168.211.130: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
从网上下载的配置文件
调试一下conf文件是否成功
其中Fdfs_client后面是我当前的conf文件所在位置
后面client.upload_by_filename是图片的路径
现在我们要实现我们存储文件的逻辑
创建fastdfs_storage.py文件
添加以下内容
from django.core.files.storage import Storage
from django.conf import settings
from fdfs_client.client import Fdfs_client
from django.utils.deconstruct import deconstructible
这个装饰器提供了迁移时序列化的方法
@deconstructible
class FastdfsStorageClass(Storage):
初始化类,获得base_url和client_conf路径
def __init__(self,base_url=None,client_conf=None):
# base_url will be used to combine the complete file and photo's url
# client_conf fdfs's completed file url
if base_url is None:
base_url=settings.FDFS_URL
if client_conf is None:
client_conf=settings.FDFS_CLIENT_CONFIG
self.base_url=base_url
self.client_conf=client_conf
def _open(self,name,mode):
# will be used by Storage.open()
pass
def _save(self,name,content):
# will be used by Storage.save()
# name:file who will be saved's name
client_obj=Fdfs_client(self.client_conf)
ret=client_obj.upload_by_buffer(content.read())
if ret.get('Status')!='Upload successed.':
raise Exception('upload failure')
filename=ret.get('Remote file_id')
return filename
def url(self,name):
return self.base_url+name
def exists(self, name):
return False
def delete(self, name):
return print('okay')
def list_all_groups(self):
client = Fdfs_client(self.client_conf)
return client.list_all_groups()
最后在setting文件中添加字段
改变django文件储存方法
DEFAULT_FILE_STORAGE='webbacksoftware.utils.fdfs.fdfs_storage.FastdfsStorageClass'
FDFS_URL='http://127.0.0.1:8888/'
找到配置文件
FDFS_CLIENT_CONFIG=os.path.join(BASE_DIR,'utils/fdfs/client.conf')
由于shell测试出了点问题,我新注册了一个模型来进行测试
在模块的model.py文件夹内添加字段
class Image(models.Model):
img=models.ImageField()
然后在该模块的admin.py文件夹下注册这个模块
from django.contrib import admin
from . import models
# Register your models here.
admin.site.register(models.Image)
创建管理员
运行python manage.py createsuperuser
至于他要什么东西大家应该都懂得,我就不说了。
使用这个管理员账户进入后端管理站点(别忘了开后端),127.0.0.1:8000/admin站点
进入images,添加一张图片,如果success,那么就是成功了。
然后你可以在该界面获取到查询的字段。
然后就可以用这个部分通过127.0.0.1:8888/Currently字段查询到我们的图片了
添加了一个下载文件,表示成功