一、创建包和文件
二、storage.py
1 from django.core.files.storage import Storage 2 from django.conf import settings 3 from fdfs_client.client import Fdfs_client 4 5 6 class FDFSStorage(Storage): 7 """fast dfs文件存储类""" 8 def __init__(self, client_conf=None, base_url=None): 9 """初始化""" 10 if client_conf is None: 11 client_conf = settings.FDFS_CLIENT_CONF 12 if base_url is None: 13 base_url = settings.FDFS_URL 14 15 self.client_conf = client_conf 16 self.base_url = base_url 17 18 def _open(self, name, mode='rb'): 19 """打开文件时使用""" 20 pass 21 22 def _save(self, name, content): 23 """保存文件时使用""" 24 client = Fdfs_client(self.client_conf) 25 res = client.upload_by_buffer(content.read()) 26 27 if res.get('Status') != 'Upload successed.': 28 raise Exception('上传文件到fast dfs失败') 29 30 filename = res.get('Remote file_id') 31 32 return filename 33 34 def exists(self, name): 35 """Django判断文件名是否可用""" 36 return False 37 38 def url(self, name): 39 """返回访问文件的url路径""" 40 return self.base_url + name
三、client.conf
1. base_path:日志存储路径
2. tracker_server:文件服务器地址
1 # connect timeout in seconds 2 # default value is 30s 3 connect_timeout=30 4 5 # network timeout in seconds 6 # default value is 30s 7 network_timeout=60 8 9 # the base path to store log files 10 base_path=/Users/cix/Desktop 11 12 # tracker_server can ocur more than once, and tracker_server format is 13 # "host:port", host can be hostname or ip address 14 tracker_server=192.168.53.132:22122 15 16 #standard log level as syslog, case insensitive, value list: 17 ### emerg for emergency 18 ### alert 19 ### crit for critical 20 ### error 21 ### warn for warning 22 ### notice 23 ### info 24 ### debug 25 log_level=info 26 27 # if use connection pool 28 # default value is false 29 # since V4.05 30 use_connection_pool = false 31 32 # connections whose the idle time exceeds this time will be closed 33 # unit: second 34 # default value is 3600 35 # since V4.05 36 connection_pool_max_idle_time = 3600 37 38 # if load FastDFS parameters from tracker server 39 # since V4.05 40 # default value is false 41 load_fdfs_parameters_from_tracker=false 42 43 # if use storage ID instead of IP address 44 # same as tracker.conf 45 # valid only when load_fdfs_parameters_from_tracker is false 46 # default value is false 47 # since V4.05 48 use_storage_id = false 49 50 # specify storage ids filename, can use relative or absolute path 51 # same as tracker.conf 52 # valid only when load_fdfs_parameters_from_tracker is false 53 # since V4.05 54 storage_ids_filename = storage_ids.conf 55 56 57 #HTTP settings 58 http.tracker_server_port=80 59 60 #use "#include" directive to include HTTP other settiongs 61 ##include http.conf
三、settings.py
1 # 设置Django的文件存储类 2 DEFAULT_FILE_STORAGE = 'utils.fdfs.storage.FDFSStorage' 3 4 # 设置fdfs使用的client.conf文件路径 5 FDFS_CLIENT_CONF = './utils/fdfs/client.conf' 6 7 # 设置fdfs存储服务器上nginx的IP和端口号 8 FDFS_URL = 'http://192.168.53.132:8888/'
四、细节
1. 因为修改了 django 内置的文件存储系统所以 upload_to 参数可以随便写
2. 因为采用 fastdfs 作为文件存储服务器,且表中的字段写下的是图片的id,所以会自动调用 storage.py 中的url方法来拼接成完整的url