go-fastDFS详解

安装

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

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

安装 go-fastdfs-web

官网:https://github.com/perfree/go-fastdfs-web

//方式1:go-fastdfs-web-1.3.4.zip需要有jre环境
unzip go-fastdfs-web-1.3.4.zip

//方式2:自带jre环境
unzip go-fastdfs-web-linux-jre.zip


chmod +x goFastDfsWeb.sh

1.运行
./goFastDfsWeb.sh start
2.查看运行状态
./goFastDfsWeb.sh status
3.重新启动
./goFastDfsWeb.sh restart
4.停止
./goFastDfsWeb.sh stop

访问网页:http://192.168.100.245:8088
地址填自己的,次访问需要填写gofastdfs相关信息,设置web页面访问的账号密码等

报错:/bin/bash^M: bad interpreter: No such file or directory

sed -i "s/\r//" goFastDfsWeb.sh
替换结尾符为unix格式

API

通用说明

  • 统一使用POST请求

  • 返回格式统一位json

    {
        "status":"ok",
        "message":"",
        "data":{}
    }
    
  • url中的group只有在support_group_manage设置为true才有。

        例如:
        http://10.1.5.9:8080/group/reload
        默认:
        http://10.1.5.9:8080/reload
        说明:url中的group为cfg.json中的group参数值。
    

配置管理 reload

http://10.1.5.9:8080/group/reload

参数:
action: set(修改参数),get获取参数,reload重新加载参数
cfg:json参数 与 action=set配合完成参数设置

统计信息 stat

http://10.1.50.90:8080/group/stat
实例:
http://192.168.100.245:8080/group1/stat


{
  "data": [
    {
      "date": "20200518",
      "fileCount": 0,
      "totalSize": 0
    },
    {
      "date": "all",
      "fileCount": 0,
      "totalSize": 0
    }
  ],
  "message": "",
  "status": "ok"
}
我的分组名是group1

查看所有信息

http://192.168.100.245:8080/group1/status

{
  "data": {
    "Fs.AutoRepair": true,
    "Fs.FileStats": [
      {
        "date": "20200518",
        "fileCount": 0,
        "totalSize": 0
      },
      {
        "date": "all",
        "fileCount": 0,
        "totalSize": 0
      }
    ],
    "Fs.Local": "http://192.168.100.245:8080",
    "Fs.Peers": null,
    "Fs.QueueFileLog": 0,
    "Fs.QueueFromPeers": 0,
    "Fs.QueueToPeers": 0,
    "Fs.QueueUpload": 0,
    "Fs.RefreshInterval": 1800,
    "Fs.ShowDir": true,
    "Sys.Alloc": 69223872,
    "Sys.DiskInfo": {
      "free": 77464055808,
      "fstype": "ext2/ext3",
      "inodesFree": 6076898,
      "inodesTotal": 6553600,
      "inodesUsed": 476702,
      "inodesUsedPercent": 7.273895263671876,
      "path": "/share/go-fastdfs",
      "total": 105149030400,
      "used": 22299648000,
      "usedPercent": 22.35246602603762
    },
    "Sys.Frees": 1292,
    "Sys.GCCPUFraction": 0.0014392510636491254,
    "Sys.GCSys": 5738768,
    "Sys.HeapAlloc": 69223872,
    "Sys.HeapObjects": 4531,
    "Sys.MemInfo": {
      "active": 972828672,
      "available": 2460647424,
      "buffers": 342609920,
      "cached": 1335914496,
      "commitlimit": 4616744960,
      "committedas": 971468800,
      "dirty": 65536,
      "free": 1065754624,
      "highfree": 0,
      "hightotal": 0,
      "hugepagesfree": 0,
      "hugepagesize": 2097152,
      "hugepagestotal": 0,
      "inactive": 697466880,
      "laundry": 0,
      "lowfree": 0,
      "lowtotal": 0,
      "mapped": 169656320,
      "pagetables": 6590464,
      "shared": 1548288,
      "slab": 255270912,
      "sreclaimable": 179089408,
      "sunreclaim": 76181504,
      "swapcached": 0,
      "swapfree": 3077566464,
      "swaptotal": 3077566464,
      "total": 3078361088,
      "used": 334082048,
      "usedPercent": 10.85259456086264,
      "vmallocchunk": 0,
      "vmalloctotal": 35184372087808,
      "vmallocused": 0,
      "wired": 0,
      "writeback": 0,
      "writebacktmp": 0
    },
    "Sys.NumCpu": 2,
    "Sys.NumGC": 4,
    "Sys.NumGoroutine": 436,
    "Sys.TotalAlloc": 90464984
  },
  "message": "",
  "status": "ok"
}

文件上传 upload

http://10.1.50.90:8080/group/upload
参数:
file:上传的文件
scene:场景
output:输出
path:自定义路径
具体请参阅示例代码(用浏览器访问http://127.0.0.1:8080)

74

文件删除 delete

http://10.1.50.90:8080/group/delete
参数:
md5:文件的摘要(md5|sha1) 视配置定
path:文件路径
md5与path二选一
说明:md5或path都是上传文件时返回的信息,要以json方式返回才能看到(参阅浏览器上传)
http://127.0.0.1:8080/delete?md5=430a71f5c5e093a105452819cc48cc9c
实例:

http://192.168.100.245:8080/group1/delete?path=files/default/20200518/17/54/0/main请求格式.txt

{
  "data": null,
  "message": "remove success",
  "status": "ok"
}

文件信息get_file_info

http://10.1.50.90:8080/group/get_file_info
参数:
md5:文件的摘要(md5|sha1) 视配置定
path:文件路径
md5与path二选一
说明:md5或path都是上传文件时返回的信息,要以json方式返回才能看到(参阅浏览器上传)
例子:http://127.0.0.1:8080/get_file_info?md5=430a71f5c5e093a105452819cc48cc9c

文件列表list_dir

http://10.1.50.90:8080/group/list_dir
参数:
dir:要查看文件列表的目录名
例子:http://127.0.0.1:8080/list_dir?dir=default
实例:
http://192.168.100.245:8080/group1/list_dir?dir=default

{
  "data": [
    {
      "is_dir": true,
      "md5": "46234dfe8b0f485618049dfe47aede84",
      "mtime": 1589795303,
      "name": "20200518",
      "path": "default",
      "size": 4096
    }
  ],
  "message": "",
  "status": "ok"
}

修复统计信息repair_stat

http://10.1.50.90:8080/group/repair_stat
参数:
date:要修复的日期,格式如:20190725
例子:http://127.0.0.1:8080/repair_stat?date=20190725

同步失败修复repair

http://10.1.50.90:8080/group/repair
参数:
force:是否强行修复(0|1)
例子:http://127.0.0.1:8080/repair?force=1

从文件目录中修复元数据(性能较差)

http://10.1.50.90:8080/group/repair_fileinfo
需要开启搬迁功能,修改cfg.json配置文件中的 enable_migrate 设为true

使用实例python

上传

[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 ~]# 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×