• django之在docker里搭建FastDFS分布式文件系统


    1.环境

    • Python版本:Python 3.6.8(python -v)
    • FastDFS版本:lastest
    • 服务器操作系统:centos 8.3.1(cat /proc/version)
    • fdfs_client_py版本:1.2.6

    2.注意事项

    在正式开始之前,确保你的22122-23000这个范围内的端口处于打开状态,另外不要让8888端口被占用。

    3.开始配置

    docker pull fastdfs # 从远程镜像库中将fastdfs下载到本地镜像库中

    sudo docker network create --subnet=39.101.205.23/16 me-network # 固定docker IP地址

    docker run -dti --network me-network --ip "your ip address" --name tracker -v /var/fdfs/tracker:/var/fdfs delron/fastdfs tracker # 指定 tracker Ip地址

    docker run -dti --network=host --name storage -e TRACKER_SERVER="your ip address" -v /var/fdfs/storage:/var/fdfs delron/fastdfs storage # 安装storage

    docker container ls -a # 查看容器是否开启


    上图是fdfs挂载的目录结构。

    上图是上传的数据存储的目录结构。

    4.使用python调用接口上传数据到FastDFS分布式文件系统中

    # 创建utils/fastdfs/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=/Home/pyvip/test99/logs/fast.log
    
    # tracker_server can ocur more than once, and tracker_server format is
    #  "host:port", host can be hostname or ip address
    tracker_server=公网IP: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
    
    # FastDFS
    FDFS_URL = 'http://你的公网IP:8888/'
    FDFS_CLIENT_CONF = os.path.join(BASE_DIR, 'utils/fastdfs/client.conf')
    
    文件上传后访问路径
    http://你的公网IP:8888/group1/M00/00/00/wKjYiVzeVpyAFbG8AACylQSEBq8752.jpg
    
    # 安装相关包
    # fdfs_client.zip文件从百度云中下载
    pip install fdfs_client-py
    pip install mutagen -i https://pypi.douban.com/simple
    pip install requests
    
    # 通过 python manage.py shell 进入命令行 在命令行中测试
    
    from fdfs_client.client import Fdfs_client
    client = Fdfs_client('utils/fastdfs/client.conf')
    ret = client.upload_by_filename("media/son.jpg")
    
    # FastDFS
    # django settings里面的配置 地址
    FDFS_URL = 'http://"你的公网":8888/'
    FDFS_CLIENT_CONF = os.path.join(BASE_DIR, 'utils/fastdfs/client.conf')
    
    文件上传后访问路径
    http://"你的公网":8888/group1/M00/00/00/wKjYiVzeVpyAFbG8AACylQSEBq8752.jpg
    

    5.存在的问题(已解决)

    from fdfs_client.client import Fdfs_client
    import os
    
    
    client = Fdfs_client("./client.conf")
    
    print(client)
    
    if os.path.isfile("linux.jpg"):
        # 使用远程解释器解释远程python文件,必须要使用相对路径或绝对路径,不要跟django的寻包混在一起,而且只能上传远程服务器的文件,想要上传本地文件,只能换解释器,让Python代码在本地运行
        print(os.path.isfile("dj32_test/media/linux.jpg"))
        # 只有与本文件在同级的conf文件和上传文件才可以上传,否则就会报错
        response = client.upload_by_filename("邓丽君 - 船歌.mp3")
        print(response)
    else:
        print("不是文件")
    
    # response
    {'Group name': 'group1', 'Remote file_id': 'group1/M00/00/00/J2UAAV6_18SADQXxAJEvbdc5rgM981.mp3', 'Status': 'Upload successed.', 'Local file name': '邓丽君 - 船歌.mp3', 'Uploaded size': '9.00MB', 'Storage IP': '39.101.0.1'}
    

    client截图:

  • 相关阅读:
    C++17 filesystem文件系统
    简易版本vue的实现
    javaSE基础04
    javaSE基础03
    javaSE基础02
    JavaSE基础01
    Document对象和window对象
    进程和子进程及端口的常用命令
    vue分页组件二次封装---每页请求特定数据
    css图片垂直水平居中及放大(实现水平垂直居中的效果有哪些方法?)
  • 原文地址:https://www.cnblogs.com/loveprogramme/p/12901787.html
Copyright © 2020-2023  润新知