運維使用docker部署好以後FastDFS分佈式文件系統以後,提供給我接口以下:html
fastdfs tracker 192.168.1.216 192.168.1.217 storage 192.168.1.216 192.168.1.217
咱們只須要在配置文件中進行配置便可,而後利用客戶端提供的接口經過簡單的代碼就能夠將文件上傳到分佈式文件系統中python
至於內部實現機制,能夠參考個人另一篇博客:分佈式文件系統Fastdfs原理及部署linux
再次提醒在安裝客戶端可能會遇到各類不可控的因素,致使你上傳失敗,在windows中在進行安裝客戶端fdfs_client模塊或在linux安裝fdsff_client模塊以後,在使用接口時均可能會出現問題,小夥伴們能夠在使用時自行踩坑,我只介紹我踩坑以後最終的實現方法,便可繞過踩坑,介紹流程包括fastdfs客戶端的安裝、配置、django中調用相應的接口docker
因客戶端模塊在自行安裝會出現問題,因此我提供該客戶端的模塊,下載連接:fast_client模塊數據庫
進入fdfs_client-py-master.zip所在目錄django
pip install fdfs_client-py-master.zip
pip install mutagen
pip install requests
配置文件在django項目中的存放位置:windows
配置文件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=/Users/delron/Desktop # tracker_server can ocur more than once, and tracker_server format is # "host:port", host can be hostname or ip address #改爲本身的ip便可,配置一個便可tracker會自行幫助咱們進行任務調度 tracker_server=192.168.1.217: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
自定義文件,如下僅供參考:分佈式
如下是實現分佈式文件系統實現文件的上傳、修改、刪除的代碼:ui
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/4/26 10:09 # @Author : suihongliang # @Site : # @File : fastdfs_service.py # @Software: PyCharm """ 使用fastdfs分佈式文件存儲系統實現文件的上傳、下載、刪除 """ 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 from utils.logger_utils import get_logging logger = get_logging(__name__) @deconstructible class FastDFSStorage(Storage): def __init__(self, client_conf=None): """ 初始化 :param client_conf: FastDFS客戶端配置文件的路徑 """ if client_conf is None: client_conf = settings.FDFS_CLIENT_CONF self.client_conf = client_conf # def upload(self, content): # """ # 在FastDFS中保存文件 # :param content: 經過 # :return: 保存到數據庫中的FastDFS的文件名 # """ # client = Fdfs_client(self.client_conf) # ret = client.upload_by_buffer(content.read()) # if ret.get("Status") != "Upload successed.": # raise Exception("upload file failed") # file_name = ret.get("Remote file_id") # return file_name def upload(self, local_path): """ 將文件上傳到fastdfs分佈式文件系統中 :param local_path: 上傳文件的本地路徑 :return: """ client = Fdfs_client(self.client_conf) ret = client.upload_by_file(local_path) logger.info(ret) print(ret) if ret.get("Status") != "Upload successed.": raise Exception("upload file failed") remote_file_id = ret.get("Remote file_id") logger.info("存儲在fastdfs上的文件路徑:", remote_file_id) return True, remote_file_id def update(self, local_path, remote_file_id): """ 對修改後的文件進行更新 :param local_path: :param remote_file_id: @return: dictionary { 'Status' : 'Modify successed.', 'Storage IP' : storage_ip } """ client = Fdfs_client(self.client_conf) try: local_path=bytes(local_path.encode("utf-8")) remote_file_id=bytes(remote_file_id.encode("utf-8")) ret_update = client.modify_by_file(local_path, remote_file_id) logger.info("文件更新成功",ret_update) return True, ret_update except Exception as e: logger.warning(u'文件更新失敗,錯誤信息:%s' % repr(e)) return None, "文件更新失敗" def download(self, local_path, remote_file_id): """ 從fastdfs分佈式文件系統進行下載文件 :param local_path: 本地保存文件路徑 :param remote_file_id: 上傳到fastdfs文件系統中自動生成的文件路徑即文件id @return dict { 'Remote file_id' : remote_file_id, 'Content' : local_filename, 'Download size' : downloaded_size, 'Storage IP' : storage_ip } """ client = Fdfs_client(self.client_conf) try: ret_download = client.download_to_file(local_path, remote_file_id) return True, ret_download except Exception as e: logger.warning(u'文件下載失敗,錯誤信息:%s' % repr(e)) return None, "文件下載失敗" def delete(self, remote_file_id): """ 從fastdfs分佈式文件系統中將文件刪除 :param remote_file_id: 上傳到fastdfs文件系統中自動生成的文件路徑即文件id @return tuple ('Delete file successed.', remote_file_id, storage_ip) """ client = Fdfs_client(self.client_conf) try: ret_delete = client.delete_file(remote_file_id) return ret_delete except Exception as e: logger.warning(u'文件刪除失敗,錯誤信息:%s' % repr(e)) return None
在django建立的settings中須要作的配置信息以下:
能夠根據本身代碼實現進行相應的配置:
# fastdf配置文件設置 # DEFAULT_FILE_STORAGE = 'distributedstorage.utils.fastdfs.fdfs_storage.FastDFSStorage' # FastDFS # fastdfs tracker 192.168.1.212 192.168.1.213 storage 192.168.1.212 192.168.1.213 # FDFS_CLIENT_CONF = os.path.join(BASE_DIR, 'utils/fastdfs/client.conf') FDFS_CLIENT_CONF = os.path.join(BASE_DIR, 'client.conf')
在調用接口時,傳遞相應的參數便可完成文件的上傳、下載、刪除和更新,成功使用客戶端進行文件的增、刪、改和下載後返回文件的參數見上述代碼。
原創不易,轉載需說明,但願對你有所幫助!