網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
1.安裝fdfs_client
2.將/etc/fdfs/client.conf復(fù)制到windows項(xiàng)目下
配置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=windows中確實(shí)存在的目錄存放log文件
# tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
tracker_server= 虛擬機(jī)的ip: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
3.使用python上傳文件
存儲(chǔ)文件方法
from django.core.files.storage import Storage
from fdfs_client.client import Fdfs_client
class FdfsStorage(Storage):
"""fast dfs文件存儲(chǔ)類"""
def __init__(self, client_conf=None, base_url=None):
"""初始化"""
if client_conf is None:
client_conf = './utils/fdfs/client.conf'
self.client_conf = client_conf
if base_url is None:
base_url = 'http://192.168.0.128:8888/'
self.base_url = base_url
def _open(self, name, mode='rb'):
"""打開文件時(shí)使用"""
pass
def _save(self, name, content):
"""
保存文件時(shí)使用
name:上傳文件的名字
content:包含你上傳文件內(nèi)容的File對(duì)象
"""
# 1.創(chuàng)建Fdfs_client對(duì)象
client = Fdfs_client(self.client_conf)
# 2.上傳文件到fast dfs
res = client.upload_by_buffer(content.read())
if res['Status'] != 'Upload successed.':
# 上傳失敗
raise Exception('上傳文件到fast dfs失敗')
# 獲取返回的文件id
filename = res['Remote file_id']
return filename
def exists(self, name):
"""django判斷文件名是否可用"""
return False
def url(self, name):
"""django返回訪問(wèn)文件的url路徑"""
return self.base_url + name
配置項(xiàng)目的settings文件(將django默認(rèn)的存儲(chǔ)方法設(shè)置為自定義的)
# 設(shè)置Django的文件存儲(chǔ)類
DEFAULT_FILE_STORAGE = 'utils.fdfs.storage.FdfsStorage'
原文鏈接:https://blog.csdn.net/weixin_43670190/article/details/123048811
相關(guān)推薦
- 2022-08-27 python基礎(chǔ)篇之pandas常用基本函數(shù)匯總_python
- 2022-04-25 C#實(shí)現(xiàn)簡(jiǎn)單串口通信_(tái)C#教程
- 2022-04-03 sqoop如何指定pg庫(kù)的模式(方法詳解)_相關(guān)技巧
- 2022-12-15 C語(yǔ)言利用鏈表實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)_C 語(yǔ)言
- 2022-04-12 el-form表單驗(yàn)證的一些方法總結(jié)
- 2022-04-27 Shell獲取路徑操作(dirname?$0?pwd)的實(shí)現(xiàn)_linux shell
- 2022-04-22 mac安裝oh-my-zsh出現(xiàn)command not found: npm問(wèn)題解決
- 2022-02-18 ImportError:can't import name 'Flask'
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支