1. 自定义文件存储系统类
查看代码
from django.core.files.storage import Storage
from fdfs_client.client import Fdfs_client
from django.conf import settings
class FastDFSStorage(Storage):
"""自定义文件存储系统"""
def __init__(self, client_path=None, base_url=None):
# fastDFS的客户端配置的文件路径
self.client_path = client_path or settings.FDFS_CLIENT_CONF
# storage服务器的ip:port
self.base_url = base_url or settings.FDFS_BASE_URL
# 注意:_open()和_save()必须重写!
def _open(self, name, mode="rb"):
"""
用来打开文件的,但是我们自定义文件存储系统的目的是为了实现存储到远程的FastDFS服务器,
不需要打开文件,所以此方法重写后什么也不做,直接pass
:param name: 要打开的文件名
:param mode: 打开文件的模式 read bytes
:return: None
"""
pass
def _save(self, name, content):
"""
文件存储时,会调用此方法,但是此方法是默认是向本地存储,
在此方法中实现文件存储到远程的FastDFS服务器
:param name: 要上传的文件名
:param content: 以rb模式打开的文件对象,将来通过content.read() 就可以读取到文件的二进制
:return: file_id
"""
# 1. 创建FastDFS 客户端
client = Fdfs_client(self.client_path)
# 2. 通过客户端调用上传文件的方法上传到文件到fastDFS服务器
# client.upload_by_filename('') # 要写上传文件的绝对路径(此方式上传的文件会有后缀)
ret = client.upload_by_buffer(content.read()) # 可以通过文件二进制数据进行上传(此方式上传的文件没有后缀)
# 3. 判断文件是否上传成功
if ret.get('Status') != 'Upload successed.':
raise Exception('Upload file failed')
# 4. 获取file_id
file_id = ret.get('Remote file_id')
# 5. 返回 file_id
return file_id
def exists(self, name):
"""
当要进行上传时都会调用此方法判断文件是否已上传,如果没有上传才会调用save方法进行上传
:param name: 要上传的文件名
:return: bool(True:表示文件已上传,不需要上传。 False:文件不存在,需要上传)
"""
return False
def url(self, name):
"""
当要访问图片时,就会调用此方法获取图片文件的绝对路径
:param name: 要访问的图片的file_id
:return: 完整的图片访问路径: storage_server IP:8888 + file_id
"""
return self.base_url + name
# def delete(self, name):
# """
# 删除name的文件
# :param name: 需要删除的文件名
# :return:
# """
# pass
# def listdir(self, path):
# """
# 列出指定路径的内容
# :param path: 传入文件路径
# :return: 返回两个文件列表
# """
# pass
# def size(self, name):
# """
# 返回name文件的总大小
# :param name: 文件名
# :return: int
# """
# pass
2. 修改默认的存储后端
# django⽂件存储(修改django默认的文件存储系统)
DEFAULT_FILE_STORAGE = 'xxxx.utlis.fastdfs.fdfs_storage.FastDFSStorage'
# FastDFS
FDFS_BASE_URL = 'http://xxx.xxx.xx.x:8888/'
FDFS_CLIENT_CONF = os.path.join(BASE_DIR, 'xxxxx/client.conf')
3.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=D:/fdfs_log
# tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
# linux那边的远程地址,端口不变
tracker_server=xxxxxx: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
标签:文件,return,name,self,fastDFS,django,client,上传 From: https://www.cnblogs.com/yeli-oneselfblogs/p/17235158.html