美文网首页
go-fastdfs安装以及python调用

go-fastdfs安装以及python调用

作者: Devops海洋的渔夫 | 来源:发表于2019-08-15 08:07 被阅读0次

    上一篇Centos7下FastDFS从安装到入门中讲述了FastDFS的安装以及概念。

    但是这个服务不是基于http上传的方式,还需要特定去安装客户端,这个就不是很方便了。尤其在win10系统使用python去安装客户端是挺费劲的。

    然后我从github找到了这个基于http上传的go-fastdfs,更加方便使用。无需安装客户端,python可以直接通过request进行http上传文件。

    Github仓库

    https://github.com/sjqzhang/go-fastdfs

    查看当前的relases版本

    访问 https://github.com/sjqzhang/go-fastdfs/releases

    可以看到当前最新的版本就是 v1.3.1 ,下面使用Centos7系统来安装看看。

    使用Centos7安装使用

    下载并安装

    mkdir go-fastdfs # 创建一个工作目录
    cd go-fastdfs # 进入工作目录
    # 下载二进制执行文件
    wget --no-check-certificate  https://github.com/sjqzhang/go-fastdfs/releases/download/v1.3.1/fileserver -O fileserver 
    chmod +x fileserver # 设置文件权限
    ./fileserver & # 启动服务
    

    执行启动如下:

    [root@centos7 go-fastdfs]# ls
    conf  data  files  fileserver  log  static
    [root@centos7 go-fastdfs]# 
    [root@centos7 go-fastdfs]# 
    [root@centos7 go-fastdfs]# ./fileserver &
    [1] 2180
    [root@centos7 go-fastdfs]# Listen on :8080
    
    [root@centos7 go-fastdfs]# 
    

    这样默认是启动8080端口号作为服务的。

    curl命令上传

    # 随便写一个txt文件,用来测试上传
    [root@centos7 go-fastdfs]# echo 123 > 1.txt
    [root@centos7 go-fastdfs]# 
    # 使用curl直接上传请求,可以看到返回了file id信息
    [root@centos7 go-fastdfs]# curl -F file=@1.txt http://192.168.196.129:8080/upload
    http://192.168.196.129:8080/group1/default/20190814/11/09/7/1.txt
    [root@centos7 go-fastdfs]# 
    

    python3使用request库执行上传

    [root@centos7 go-fastdfs]# ipython3
    Python 3.7.1 (default, Feb 18 2019, 11:27:32) 
    Type 'copyright', 'credits' or 'license' for more information
    IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.
    
    In [1]: ls                                                                               
    1.txt  conf/  data/  files/  fileserver*  log/  static/
    
    In [2]: import requests                                                                  
    
    In [3]: url = 'http://192.168.196.129:8080/upload'                                       
    
    In [4]: files = {'file': open('1.txt', 'rb')}                                            
    
    In [5]: options={'output':'json','path':'','scene':''} #参阅浏览器上传的选项             
    
    In [6]: r = requests.post(url,data=options, files=files)                                 
    
    In [7]: print(r.text)                                                                    
    {"url":"http://192.168.196.129:8080/group1/default/20190814/11/09/7/1.txt","md5":"ba1f2511fc30423bdbb183fe33f3dd0f","path":"/group1/default/20190814/11/09/7/1.txt","domain":"http://192.168.196.129:8080","scene":"default","size":4,"mtime":1565752146,"scenes":"default","retmsg":"","retcode":0,"src":"/group1/default/20190814/11/09/7/1.txt"}
    
    In [8]:     
    

    下载文件

    [root@centos7 ~]# wget http://192.168.196.129:8080/group1/default/20190814/11/09/7/1.txt
    --2019-08-14 11:14:47--  http://192.168.196.129:8080/group1/default/20190814/11/09/7/1.txt
    Connecting to 192.168.196.129:8080... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 4 [application/octet-stream]
    Saving to: ‘1.txt’
    
    100%[====================================================================================================================================>] 4           --.-K/s   in 0s      
    
    2019-08-14 11:14:47 (636 KB/s) - ‘1.txt’ saved [4/4]
    
    [root@centos7 ~]# cat 1.txt 
    123
    [root@centos7 ~]# 
    

    可以看到,go-fastdfs不会去修改文件的存储命名,其实这个也是很方便的。

    查看日志

    # 进入log目录下
    [root@centos7 go-fastdfs]# cd log/
    # 可以看到有三个日志文件
    [root@centos7 log]# ls
    access.log  fileserver.log  tusd.log
    [root@centos7 log]# tail -f access.log 
    2019-08-14 11:09:06 [INF] [fileserver.go:3772] [main.HttpHandler.ServeHTTP.func1] [Access] 2019/08/14 - 11:09:06 | map[Access-Control-Allow-Headers:[Authorization, Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, X-Requested-By, If-Modified-Since, X-File-Name, X-File-Type, Cache-Control, Origin] Access-Control-Allow-Methods:[GET, POST, OPTIONS, PUT, DELETE] Access-Control-Allow-Origin:[*] Access-Control-Expose-Headers:[Authorization]] | 44.204373ms | 192.168.196.129 | POST | 200 |/upload
    2019-08-14 11:12:29 [INF] [fileserver.go:3772] [main.HttpHandler.ServeHTTP.func1] [Access] 2019/08/14 - 11:12:29 | map[Access-Control-Allow-Headers:[Authorization, Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, X-Requested-By, If-Modified-Since, X-File-Name, X-File-Type, Cache-Control, Origin] Access-Control-Allow-Methods:[GET, POST, OPTIONS, PUT, DELETE] Access-Control-Allow-Origin:[*] Access-Control-Expose-Headers:[Authorization]] | 475.454µs | 192.168.196.129 | POST | 200 |/upload
    2019-08-14 11:14:47 [INF] [fileserver.go:3772] [main.HttpHandler.ServeHTTP.func1] [Access] 2019/08/14 - 11:14:47 | map[Accept-Ranges:[bytes] Access-Control-Allow-Headers:[Authorization, Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, X-Requested-By, If-Modified-Since, X-File-Name, X-File-Type, Cache-Control, Origin] Access-Control-Allow-Methods:[GET, POST, OPTIONS, PUT, DELETE] Access-Control-Allow-Origin:[*] Access-Control-Expose-Headers:[Authorization] Content-Disposition:[attachment] Content-Length:[4] Content-Type:[application/octet-stream] Last-Modified:[Wed, 14 Aug 2019 03:09:06 GMT]] | 401.005µs | 192.168.196.129 | GET | 200 |/group1/default/20190814/11/09/7/1.txt
    ^C
    [root@centos7 log]# 
    [root@centos7 log]# tail fileserver.log 
    2019-08-14 11:06:00 [INF] [fileserver.go:3394] [main.init.0] succes init log access
    2019-08-14 11:06:00 [INF] [fileserver.go:386] [main.ParseConfig] {:8080 [http://192.168.196.129:8080] group1 false true [] 1800 true  true [] [] default {abc@163.com abc smtp.163.com:25}  false 600 0 true http://192.168.196.129:8080 md5 7 false [127.0.0.1] false false true false true false  false true true 0}
    2019-08-14 11:06:00 [INF] [fileserver.go:388] [main.ParseConfig] config parse success
    2019-08-14 11:09:06 [INF] [fileserver.go:2077] [main.(*Server).SaveUploadFile] upload: files/default/20190814/11/09/7/1.txt
    2019-08-14 11:12:29 [INF] [fileserver.go:2077] [main.(*Server).SaveUploadFile] upload: files/default/20190814/11/12/7/1.txt
    [root@centos7 log]# 
    [root@centos7 log]# tail -f tusd.log 
    ^C
    [root@centos7 log]# 
    

    如果只是单纯查看文件上传的记录,只需要看fileserver.log即可。

    查看相关配置

    [root@centos7 go-fastdfs]# cd conf/
    [root@centos7 conf]# ls
    cfg.json
    [root@centos7 conf]# vim cfg.json 
    [root@centos7 conf]# cat cfg.json 
    {
        "绑定端号": "端口",
        "addr": ":8080",
        "PeerID": "集群内唯一,请使用0-9的单字符,默认自动生成",
        "peer_id": "7",
        "本主机地址": "本机http地址,默认自动生成(注意端口必须与addr中的端口一致),必段为内网,自动生成不为内网请自行修改,下同",
        "host": "http://192.168.196.129:8080",
        "集群": "集群列表,注意为了高可用,IP必须不能是同一个,同一不会自动备份,且不能为127.0.0.1,且必须为内网IP,默认自动生成",
        "peers": ["http://192.168.196.129:8080"],
        "组号": "用于区别不同的集群(上传或下载)与support_group_manage配合使用,带在下载路径中",
        "group": "group1",
        "是否支持按组(集群)管理,主要用途是Nginx支持多集群": "默认不支持,不支持时路径为http://10.1.5.4:8080/action,支持时为http://10.1.5.4:8080/group(配置中的group参数)/action,action为动作名,如status,delete,sync等",
        "support_group_manage": false,
        "是否合并小文件": "默认不合并,合并可以解决inode不够用的情况(当前对于小于1M文件)进行合并",
        "enable_merge_small_file": false,
        "允许后缀名": "允许可以上传的文件后缀名,如jpg,jpeg,png等。留空允许所有。",
        "extensions": [],
        "重试同步失败文件的时间": "单位秒",
        "refresh_interval": 1800,
        "是否自动重命名": "默认不自动重命名,使用原文件名",
        "rename_file": false,
        "是否支持web上传,方便调试": "默认支持web上传",
        "enable_web_upload": true,
        "是否支持非日期路径": "默认支持非日期路径,也即支持自定义路径,需要上传文件时指定path",
        "enable_custom_path": true,
        "下载域名": "用于外网下载文件的域名,不包含http://",
        "download_domain": "",
        "场景列表": "当设定后,用户指的场景必项在列表中,默认不做限制(注意:如果想开启场景认功能,格式如下:'场景名:googleauth_secret' 如 default:N7IET373HB2C5M6D ",
        "scenes": [],
        "默认场景": "默认default",
        "default_scene": "default",
        "是否显示目录": "默认显示,方便调试用,上线时请关闭",
        "show_dir": true,
        "邮件配置": "",
        "mail": {
            "user": "abc@163.com",
            "password": "abc",
            "host": "smtp.163.com:25"
        },
        "告警接收邮件列表": "接收人数组",
        "alarm_receivers": [],
        "告警接收URL": "方法post,参数:subject,message",
        "alarm_url": "",
        "下载是否需带token": "真假",
        "download_use_token": false,
        "下载token过期时间": "单位秒",
        "download_token_expire": 600,
        "是否自动修复": "在超过1亿文件时出现性能问题,取消此选项,请手动按天同步,请查看FAQ",
        "auto_repair": true,
        "文件去重算法md5可能存在冲突,默认md5": "sha1|md5",
        "file_sum_arithmetic": "md5",
        "管理ip列表": "用于管理集的ip白名单,",
        "admin_ips": ["127.0.0.1"],
        "是否启用迁移": "默认不启用",
        "enable_migrate": false,
        "文件是否去重": "默认去重",
        "enable_distinct_file": true,
        "是否开启跨站访问": "默认开启",
        "enable_cross_origin": true,
        "是否开启Google认证,实现安全的上传、下载": "默认不开启",
        "enable_google_auth": false,
        "认证url": "当url不为空时生效,注意:普通上传中使用http参数 auth_token 作为认证参数, 在断点续传中通过HTTP头Upload-Metadata中的auth_token作为认证参数,认证流程参考认证架构图",
        "auth_url": "",
        "下载是否认证": "默认不认证(注意此选项是在auth_url不为空的情况下生效)",
        "enable_download_auth": false,
        "默认是否下载": "默认下载",
        "default_download": true,
        "本机是否只读": "默认可读可写",
        "read_only": false,
        "是否开启断点续传": "默认开启",
        "enable_tus": true,
        "同步单一文件超时时间(单位秒)": "默认为0,程序自动计算,在特殊情况下,自已设定",
        "sync_timeout": 0
    }
        [root@centos7 conf]# 
    

    可以看到配置项还是挺多的,不过目前的需求就是能够上传、下载文件即可,所以其他配置有需要的时候上github仓库中查阅配置即可。

    相关文章

      网友评论

          本文标题:go-fastdfs安装以及python调用

          本文链接:https://www.haomeiwen.com/subject/wogwjctx.html