zoukankan      html  css  js  c++  java
  • python django项目中添加fastdfs分布式系统

    Fdfs python中配置

    下载所需要的包

    从fdfs官方文档中找到官方python包的git地址https://github.com/JaceHo/fdfs_client-py,进入git中下载一下.zip文件,然后在.zip文件路径下进入虚拟环境并键入

    pip install fdfs_client-py-master.zip
    pip install mutagen
    pip install requests
    1
    2
    3
    创建fdfs文件夹存储fdfs功能文件

    文件位置任选,我放在我的同名文件夹中的utils文件夹中,因为比较好找。别忘了是创建python_package,当然你也可以创建文件夹写一个__init__.py文件。
    创建client.conf文件,最好直接用text的,否则不停飘红

    # the base path to store log files
    base_path=/home/lvbu89757/shanghuishop/shanghuishop/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.211.130:22122
    1
    2
    3
    4
    5
    6
    7
    这两条是必填项,为了设置日志和告诉程序tracker路径

    # 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=/home/lvbu89757/shanghuishop/shanghuishop/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.211.130: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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    从网上下载的配置文件

    调试一下conf文件是否成功


    其中Fdfs_client后面是我当前的conf文件所在位置
    后面client.upload_by_filename是图片的路径

    现在我们要实现我们存储文件的逻辑
    创建fastdfs_storage.py文件
    添加以下内容

    from django.core.files.storage import Storage
    from django.conf import settings
    from fdfs_client.client import Fdfs_client
    from django.utils.deconstruct import deconstructible

    这个装饰器提供了迁移时序列化的方法
    @deconstructible
    class FastdfsStorageClass(Storage):
    初始化类,获得base_url和client_conf路径
    def __init__(self,base_url=None,client_conf=None):
    # base_url will be used to combine the complete file and photo's url
    # client_conf fdfs's completed file url

    if base_url is None:
    base_url=settings.FDFS_URL

    if client_conf is None:
    client_conf=settings.FDFS_CLIENT_CONFIG
    self.base_url=base_url

    self.client_conf=client_conf

    def _open(self,name,mode):
    # will be used by Storage.open()
    pass

    def _save(self,name,content):
    # will be used by Storage.save()
    # name:file who will be saved's name

    client_obj=Fdfs_client(self.client_conf)
    ret=client_obj.upload_by_buffer(content.read())
    if ret.get('Status')!='Upload successed.':
    raise Exception('upload failure')


    filename=ret.get('Remote file_id')

    return filename

    def url(self,name):
    return self.base_url+name

    def exists(self, name):
    return False

    def delete(self, name):
    return print('okay')

    def list_all_groups(self):
    client = Fdfs_client(self.client_conf)
    return client.list_all_groups(http://www.my516.com)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    最后在setting文件中添加字段

    改变django文件储存方法
    DEFAULT_FILE_STORAGE='webbacksoftware.utils.fdfs.fdfs_storage.FastdfsStorageClass'

    FDFS_URL='http://127.0.0.1:8888/'
    找到配置文件
    FDFS_CLIENT_CONFIG=os.path.join(BASE_DIR,'utils/fdfs/client.conf')
    1
    2
    3
    4
    5
    6


    项目文件的布局

    测试图片保存

    由于shell测试出了点问题,我新注册了一个模型来进行测试
    在模块的model.py文件夹内添加字段

    class Image(models.Model):
    img=models.ImageField()
    1
    2
    然后在该模块的admin.py文件夹下注册这个模块

    from django.contrib import admin
    from . import models
    # Register your models here.

    admin.site.register(models.Image)
    1
    2
    3
    4
    5
    创建管理员
    运行python manage.py createsuperuser
    至于他要什么东西大家应该都懂得,我就不说了。
    使用这个管理员账户进入后端管理站点(别忘了开后端),127.0.0.1:8000/admin站点

    ---------------------

  • 相关阅读:
    nginx平滑升级及回滚
    redis源码安装
    memcached安装
    Harbor源码部署
    Maven源码部署
    tomcat单机多实例(未完待续)
    部署tomcat
    nginx编译参数详解
    CentOS7 安装pip/pip3
    nginx 部署配置
  • 原文地址:https://www.cnblogs.com/hyhy904/p/11186720.html
Copyright © 2011-2022 走看看