以fast_fdfs作為存儲后端
環(huán)境搭建fdfs
sudo docker run -d --network=host --name tracker -v /var/fdfs/tracker:/var/fdfs delron/fastdfs tracker
sudo docker run -d --network=host --name storage -e TRACKER_SERVER=192.168.80.131:22122 -v /var/fdfs/storage:/var/fdfs delron/fastdfs storage
客戶端安裝
https://github.com/JaceHo/fdfs_client-py
克隆到本地修改
修改文件,安裝時會報錯
setup.py 注釋兩行
'ext_modules': [Extension('fdfs_client.sendfile',
sources=['fdfs_client/sendfilemodule.c'])],
fdfs_client-py/fdfs_client/storage_client.py 注釋以下行
from fdfs_client.sendfile import *
如果在使用時報關(guān)于字符編碼錯可以在 fdfs_client-py/fdfs_client/utils.py
def read(self, filenames):
if isinstance(filenames, basestring):
filenames = [filenames]
read_ok = []
for filename in filenames:
try:
#文件打開編碼為utf-8
with open(filename,encoding='utf-8') as fp:
self.readfp(fp)
except IOError as e:
continue
else:
read_ok.append(filename)
return read_ok
安裝 fdfs_client
python setup.py install
客戶端配置文件(客戶端會調(diào)用配置文件得到client對象)
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=FastDFS客戶端存放日志文件的目錄
# tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
tracker_server = fdfs_server: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
測試:fdfs_client
test.py
from fdfs_client.client import Fdfs_client
with open('./0.jpg','rb') as content:
client = Fdfs_client('./client.conf')
ret = client.upload_by_buffer(content.read())
print(ret)
{
'Group name': 'group1',
'Local file name': ''',
'Storage IP': '192.168.80.131',
'Remote file_id': 'group1/M00/00/03/rBCzi10HvhOAbpotAAAi00HB9mU322.jpg',
'Uploaded size': '8.00KB',
'Status': 'Upload successed.'
}
存儲后端
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
@deconstructible
class FastDFSStorage(Storage):
def __init__(self,base_url=None,client_conf=None):
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 _save(self,name,content):
#截取文件后綴
suffix = name.split('.')[1]
client = Fdfs_client(self.client_conf)
ret = client.upload_by_buffer(content.read(),suffix)
# print(ret)
if ret.get("Status") != "Upload successed.":
raise Exception("upload file faild")
file_name = ret.get('Remote file_id')
return file_name
def exists(self,name):
return False
def url(self,name):
# print(self.base_url+name)
http_url = self.base_url + name
print(http_url)
return http_url
setting.py配置
FDFS_URL = "http://192.168.80.131:8888/"
DEFAULT_FILE_STORAGE = 'fastdfs.storage.FastDFSStorage'
FDFS_CLIENT_CONF = os.path.join(BASE_DIR,'index/client.conf')
model.py
class Avatar(models.Model):
name = models.CharField(max_length=32)
#upload_to 自定義了存儲后端此處填什么沒有影響
avatar = models.FileField(upload_to='icon',verbose_name='頭像')
admin.py
admin.site.register(Avatar)
views.py
def show_image(request):
if request.method == 'GET':
icons = models.Icon.objects.all()
url = []
for icon in icons:
url.append(icon.icon.url)
print(url)
return HttpResponse("<image src='{}'>".format(url[0]))
# return render(request,'show_image.html',{'url':url})