zoukankan      html  css  js  c++  java
  • Django + FastDFS (分布式远程服务器存储文件)

    之前随笔过一篇Docker来搭建分布式文件系统FastDfs就跳过了

    https://www.cnblogs.com/xcsg/p/10901461.html

    FastDFS的Python  (django客户端)

    1.安装依赖

    #安装库
    pip install py3Fdfs
    pip install mutagen
    pip isntall requests

    2.配置settings.py

    # 激活重写存储文件引擎方法
    DEFAULT_FILE_STORAGE = 'utils.fdfs.storage.FDFSStorage'
    # 图片存阿里云的地址
    FDAS_URL = 'http://39.97.117.229:8888/'
    
    # 存进阿里云服务器里因为没有后缀(jpg/png),图片连接要放进 img 标签里才能显示
    # <img src="http://39.106.64.101:8888/group1/M00/00/00/rBHmx10A9JmAVEKNAABf71XWOL06003319" alt=""># 配置 Fastdfs 配置文件
    FDFS_CLIENT_CONF = os.path.join(BASE_DIR, 'client.conf')

    3.配置client.conf 文件

    connect_timeout=30
    network_timeout=60
    tracker_server=39.97.117.229:22122
    log_level=info
    use_connection_pool = false
    connection_pool_max_idle_time = 3600
    load_fdfs_parameters_from_tracker=false
    use_storage_id = false
    storage_ids_filename = storage_ids.conf
    http.tracker_server_port=80

    4.封装上传文件 fastdfs.py

    from django.core.files.storage import Storage
    from fdfs_client.client import Fdfs_client,get_tracker_conf
    from video_back.settings import FDAS_URL,FDFS_CLIENT_CONF
    
    # 重写存储引擎方法
    class FastDfsStroage(Storage):
    
        def __init__(self, base_url = None, client_conf = None):
            """
                初始化对象
                :param base_url:
                :param client_conf:
            """
            if base_url is None:
                base_url = FDAS_URL
                # 'http://39.106.64.101:8888/'
            self.base_url = base_url
    
            if client_conf is None:
                client_conf = FDFS_CLIENT_CONF
                # FDFS_CLIENT_CONF = os.path.join(BASE_DIR, 'client.conf')
            self.client_conf = client_conf
    
    
        def _open(self, name, mode = 'rb'):
        
            """
                打开文件
                :param name:
                :param mode:
                :return:
            """
            pass
    
        def _save(self,content):
            """
                保存文件
                :param name: 传入文件名
                :param content: 文件内容
                :return:保存到数据库中的FastDFSDE文件名
            """
     
            client = Fdfs_client(get_tracker_conf(FDFS_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")
            #byte转str
            file_name = str(file_name,encoding = "utf-8")
            return file_name
    
    
        def exists(self, name):
            """
                检查文件是否重复, FastDFS自动区分重复文件
                :param name:
                :return:
            """
            return False
    
        def url(self, name):
            """
                获取name文件的完整url
                :param name:
                :return:
            """
            return self.base_url + name

    5.上传逻辑views.py

    #导入封装类
    from .fastdfs import *
    from django.http import JsonResponse
    def upload(request):
        img = request.FILES.get('file')
        img_fast = FastDfsStroage()
        #存入远程服务器
        ret = img_fast._save(img)
        #返回存入图片的url
        url = img_fast.url(ret)
        if url:
            return JsonResponse({'url':url})

    效果如下:

  • 相关阅读:
    C#与C++与互操作
    WPF GridView动态添加项并读取数据
    C#使用消息队列(MSMQ)
    使用代码浏览WPF控件模版
    PowerShell将运行结果保存为文件
    opencv + ffmpeg
    vmware
    HttpComponents Downloads
    pytorch 安装
    opencv 3.4.0 的编译
  • 原文地址:https://www.cnblogs.com/xcsg/p/11371091.html
Copyright © 2011-2022 走看看