天天看點

python-django架構中使用FastDFS分布式檔案系統

一、安裝FastDFS

1-1:執行docker指令安裝

# 安裝tracker
docker run -dti --network=host --name tracker -v /var/fdfs/tracker:/var/fdfs youkou1/fastdfs tracker

# 安裝storage
docker run -dti --network=host --name storage -e TRACKER_SERVER=IP位址:22122 -v /var/fdfs/storage:/var/fdfs youkou1/fastdfs storage
      
python-django架構中使用FastDFS分布式檔案系統

1-2 測試是否安裝成功:執行指令 docker ps -a

python-django架構中使用FastDFS分布式檔案系統

TRACKER_SERVER:IP位址說明不要使用lo 和docker下的IP位址因為在安裝鏡像中配的是enp3s0下面的IP位址。

python-django架構中使用FastDFS分布式檔案系統

1-3、在項目中建立用戶端配置檔案  fastdfs/client.conf

python-django架構中使用FastDFS分布式檔案系統

client.conf配置檔案内容:注意tracker_serverIP位址。

# 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=utils/fastdfs/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.31.170: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
use_connection_pool = false

# connections whose the idle time exceeds this time will be closed
# unit: second
# default value is 3600
connection_pool_max_idle_time = 3600

# if load FastDFS parameters from tracker server
# 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
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
storage_ids_filename = storage_ids.conf


#HTTP settings
http.tracker_server_port=80
      

1-4:在xhell中進入項目的虛拟環境安裝python相關包 (在虛拟環境中)

# 安裝相關包 
pip install fdfs_client.zip
pip install mutagen
pip install requests
      

1-5:如果pip install fdfs_client.zip 安裝報錯

python-django架構中使用FastDFS分布式檔案系統

請執行如下指令

sudo apt-get install python3 python-dev python3-dev build-essential libssl-dev libffi-dev libxml2-dev libxslt1-dev zlib1g-dev python-pip
      

  

python-django架構中使用FastDFS分布式檔案系統

然後在執行安裝指令:

python-django架構中使用FastDFS分布式檔案系統

檢視所安裝的包:

python-django架構中使用FastDFS分布式檔案系統

分析上傳步驟: 

  1) 指定圖檔絕對位址 

  2) 指定圖檔伺服器位址;使用 Client.conf 配置檔案 

  3) 加載圖檔伺服器,連接配接圖檔伺服器 

  4) 建立圖檔伺服器 tracker 用戶端 

  5) 從用戶端擷取 tracker 連接配接 

  6) 建立 storage 用戶端,存儲圖檔

測試代碼:

from fdfs_client.client import Fdfs_client
FDFS_Client = Fdfs_client('/common/fastdfs/client.conf')
ret = FDFS_Client.upload_by_filename('/media/2018.png')
print(ret)
      

  如果執行不報錯,傳回結果如下說明就成功了:

python-django架構中使用FastDFS分布式檔案系統

1-6 如果進行測試 以下報的是文法錯誤,這個明顯是python2的文法,我現在用的是python3,進過查詢相關資料,決定換一個不用 pip install fdfs_client.zip 

python-django架構中使用FastDFS分布式檔案系統

删除   fdfs_client.zip  執行指令

pip uninstall fdfs_client-py
      

使用pip install py3Fdfs

python-django架構中使用FastDFS分布式檔案系統

 再次測試: 

python-django架構中使用FastDFS分布式檔案系統

 在浏覽器中檢視:

http://192.168.31.170:8888/group1/M00/00/00/wKgfqlz5yYyAHe5nAAfh_rrm7jw601.png

注意:這裡的端口8888也是在上面安裝環境中配好的。

繼續閱讀