在上一篇中介紹了使用 FastDFS 與 Nginx 在 Linux 系統上實現分布式圖片服務器,現在介紹在 windows 系統下使用該分布式圖片服務器。
需要安裝 fdfs_client 模塊和配置 client.conf 配置文件。
下載 fdfs_client 模塊和 client.conf 配置文件:點我下載
windows 下使用 fdfs_client 實現文件上傳
1、安裝 fdfs_client 模塊
在 windows 環境下使用 pip install fdfs_client
會報錯,所以直接安裝是不行的,但是可以直接把已經下載好的 fdfs_client 模塊 copy 到 python解釋器的 /lib 文件夾中。
2、修改 fdfs_client/storage_client.py 文件
需要修改 fdfs_client 模塊中的 storage_client.py 文件,將第12行刪除或注釋(否則使用時會報錯 ImportError: No module named sendfile)
3、安裝 mutagen 和 requests
pip install mutagen
pip install requests
4、修改 fastdfs 配置文件: client_config
① 修改 base_path 為你 windows 電腦一個真實存在的文件夾
例如:
base_path=E:\fdfs_log
② 修改 tracker_server 為你 Linux 系統的 IP 地址 + :22122
例如:
tracker_server=192.168.159.140:22122
# 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=E:\fdfs_log
# 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.159.140: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
5、文件上傳測試
導入模塊:
from fdfs_client.client import Fdfs_client
導入配置文件:
注意:windows環境下絕對路徑會發生轉義,需要加上 "r" 說明是原生字符串
client = Fdfs_client(r'配置文件絕對路徑')
上傳圖片:
注意:windows環境下絕對路徑會發生轉義,需要加上 "r" 說明是原生字符串
client.upload_by_filename(r'圖片文件絕對路徑')
如果你看到如下畫面你就成功了!!!