> 技术文档 > GitHub开源的轻量级文件服务器Dufs,可docker一键部署

GitHub开源的轻量级文件服务器Dufs,可docker一键部署


文件服务器

    • 介绍
      • docker安装
      • 二进制安装
      • 配置https
      • API调用

介绍

项目github官网地址

Dufs是一款由Rust编写的轻量级文件服务器,不仅支持静态文件服务,还能轻松上传、下载、搜索文件,甚至支持WebDAV,让我们通过Web方式远程管理文件变得轻而易举。而且,它跨平台,无论是Windows、macOS还是Linux,都能轻松驾驭

性能特色

  • 静态文件服务:一键开启,让你的文件触手可及。
  • 上传下载:支持拖放上传,文件夹打包下载为zip,省时又省力。
  • 文件操作:创建、编辑、搜索文件,一切尽在掌握。
  • 断点续传:再也不用担心大文件传输中断了,Dufs支持断点续传,让你的下载上传无忧。
  • 访问控制:灵活的权限设置,无论是公开分享还是私密访问,都能轻松搞定。
  • HTTPS & WebDAV:安全传输,远程管理,一个都不能少。

docker安装

Ubuntu2404-server

该项目支持多平台安装,这里我使用docker进行快速搭建

root@huhy:~# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEsigoden/dufs latest f7f212903ad7 3 months ago 4.37MB
docker run -v `pwd`:/data -p 5000:5000 --rm sigoden/dufs /data -A
  • -v \\pwd:/data:

    • -v: 这是用于挂载卷的选项。
    • pwd: 这是一个命令替换,返回当前工作目录路径
    • :/data: 把主机上的当前目录(由 pwd 命令获取的路径)挂载到容器内的 /data 目录中。这样,容器可以访问主机上的这个目录中的文件。
      -p 5000:5000:
  • -p: 这是用于端口映射的选项。
    5000:5000: 将主机的 5000 端口映射到容器的 5000 端口。这样,主机访问 localhost:5000 时,会转发到容器的 5000 端口。

  • –rm: 这个选项指定在容器停止运行后,自动删除容器。这样可以防止积累不必要的临时容器。

  • sigoden/dufs: Docker 镜像的名称,sigoden/dufs 是镜像的全名,运行的镜像是 sigoden 用户创建的 dufs 镜像。

  • /data: 这是传递给 dufs 程序的第一个参数,表示 dufs 要共享的目录路径(这里指向容器内的 /data 目录,即挂载的主机目录)。

  • -A: 这是传递给 dufs 程序的一个选项,通常用于表示启用身份验证或匿名访问等配置(具体功能取决于 dufs 的实现)

使用home目录作为共享目录:

root@huhy:/home# lshuhy sigoden.tarroot@huhy:/home# docker run -v `pwd`:/data -p 5000:5000 --rm sigoden/dufs /data -AListening on: http://127.0.0.1:5000/ http://172.17.0.2:5000/ http://[::1]:5000/

界面IP:5000查看

GitHub开源的轻量级文件服务器Dufs,可docker一键部署
win系统可界面上传

GitHub开源的轻量级文件服务器Dufs,可docker一键部署

界面创建目录等

GitHub开源的轻量级文件服务器Dufs,可docker一键部署

二进制安装

命令在docker环境中不能使用,因为该镜像很小,没有bash环境,如果需要使用命令,则使用其他搭建方式,有镜像需要可评论区

上传到home目录下解压

[root@localhost home]# tar -xf dufs-v0.42.0-x86_64-unknown-linux-musl.tar.gz[root@localhost home]# lsdufs[root@localhost home]# ./dufs -AListening on: http://127.0.0.1:5000/ http://192.168.200.100:5000/ http://[::1]:5000/

界面即可访问:IP:5000

  • 也可以指定配置文件来启动
vi config.yaml
# 文件存储路径serve-path: \'.\' bind: 0.0.0.0port: 5001# 访问链接后缀path-prefix: /dufshidden: - tmp - \'*.log\' - \'*.lock\'auth: - \'admin:admin@/:rw\' - \'user:pass@/src:rw,/share\' - \'@/\'allow-all: falseallow-upload: trueallow-delete: trueallow-search: trueallow-symlink: trueallow-archive: trueenable-cors: truerender-index: truerender-try-index: truerender-spa: truelog-format: \'$remote_addr \"$request\" $status $http_user_agent\'log-file: ./dufs.logcompress: low
[root@localhost home]# ./dufs -c config.yamlListening on: http://127.0.0.1:5001/dufs/ http://192.168.200.100:5001/dufs/

界面访问;http://IP:5001/dufs/

启用守护进程systemd

vi /etc/systemd/system/dufs.service
[Unit]Description=dufs file serverAfter=network.target[Service]ExecStart=/home/dufs -ARestart=alwaysUser=rootGroup=rootWorkingDirectory=/home[Install]WantedBy=multi-user.target
systemctl daemon-reload

终止掉刚刚启动的dufs,重新启动

[root@localhost home]# systemctl start dufs[root@localhost home]# systemctl status dufs● dufs.service - dufs file server Loaded: loaded (/etc/systemd/system/dufs.service; disabled; vendor preset: disabled) Active: active (running) since Fri 2024-09-27 09:52:02 EDT; 2s ago Main PID: 2388 (dufs) CGroup: /system.slice/dufs.service  └─2388 /home/dufs -ASep 27 09:52:02 localhost.localdomain systemd[1]: Started dufs file server.Sep 27 09:52:02 localhost.localdomain dufs[2388]: Listening on:Sep 27 09:52:02 localhost.localdomain dufs[2388]: http://127.0.0.1:5000/Sep 27 09:52:02 localhost.localdomain dufs[2388]: http://192.168.200.100:5000/Sep 27 09:52:02 localhost.localdomain dufs[2388]: http://[::1]:5000/[root@localhost home]#[root@localhost home]# systemctl enable --now dufsCreated symlink from /etc/systemd/system/multi-user.target.wants/dufs.service to /etc/systemd/system/dufs.service.

命令手册

Dufs is a distinctive utility file server - https://github.com/sigoden/dufsUsage: dufs [OPTIONS] [serve-path]Arguments: [serve-path] Specific path to serve [default: .]Options: -c, --config <file> Specify configuration file -b, --bind <addrs> Specify bind address or unix socket -p, --port <port> Specify port to listen on [default: 5000] --path-prefix <path> Specify a path prefix --hidden <value> Hide paths from directory listings, e.g. tmp,*.log,*.lock -a, --auth <rules> Add auth roles, e.g. user:pass@/dir1:rw,/dir2 -A, --allow-all Allow all operations --allow-upload Allow upload files/folders --allow-delete Allow delete files/folders --allow-search Allow search files/folders --allow-symlink Allow symlink to files/folders outside root directory --allow-archive Allow zip archive generation --enable-cors Enable CORS, sets `Access-Control-Allow-Origin: *` --render-index Serve index.html when requesting a directory, returns 404 if not found index.html --render-try-index Serve index.html when requesting a directory, returns directory listing if not found index.html --render-spa  Serve SPA(Single Page Application) --assets <path> Set the path to the assets directory for overriding the built-in assets --log-format <format> Customize http log format --log-file <file> Specify the file to save logs to, other than stdout/stderr --compress <level> Set zip compress level [default: low] [possible values: none, low, medium, high] --completions <shell> Print shell completion script for <shell> [possible values: bash, elvish, fish, powershell, zsh] --tls-cert <path> Path to an SSL/TLS certificate to serve with HTTPS --tls-key <path> Path to the SSL/TLS certificate\'s private key -h, --help  Print help -V, --version  Print version
基本用法:dufs [OPTIONS] [serve-path] serve-path: 指定要服务的路径,默认为当前目录(.)配置相关:-c, --config <file>: 指定配置文件网络相关:-b, --bind <addrs>: 指定绑定地址或unix socket-p, --port <port>: 指定监听端口,默认5000--path-prefix <path>: 指定路径前缀隐藏文件:--hidden <value>: 在目录列表中隐藏路径,如tmp,.log,.lock权限控制:-a, --auth <rules>: 添加认证规则,如user:pass@/dir1:rw,/dir2-A, --allow-all: 允许所有操作--allow-upload: 允许上传文件/文件夹--allow-delete: 允许删除文件/文件夹--allow-search: 允许搜索文件/文件夹--allow-symlink: 允许链接到根目录外的文件/文件夹--allow-archive: 允许生成zip压缩包跨域:--enable-cors: 启用CORS,设置Access-Control-Allow-Origin: *渲染选项:--render-index: 请求目录时提供index.html,找不到则返回404--render-try-index: 请求目录时提供index.html,找不到则返回目录列表--render-spa: 服务单页应用(SPA)资源:--assets <path>: 设置资源目录路径,用于覆盖内置资源日志:--log-format <format>: 自定义HTTP日志格式--log-file <file>: 指定保存日志的文件压缩:--compress <level>: 设置zip压缩级别[默认:low] [可选值:none, low, medium, high]Shell补全:--completions <shell>: 打印shell补全脚本 [可选值:bash, elvish, fish, powershell, zsh]HTTPS:--tls-cert <path>: SSL/TLS证书路径--tls-key <path>: SSL/TLS证书私钥路径帮助:-h, --help: 打印帮助信息-V, --version: 打印版本信息

修改默认的端口

dufs . -p 8080

共享当前目录并允许所有操作(上传、删除等)

dufs -A 

只允许上传:

dufs --allow-upload 

指定特定目录

dufs temp 

指定文件

dufs temp.doc

制定8080端口

dufs -p 8080

配置https

使用自签名证书

openssl req -x509 -newkey rsa:4096 -keyout my.key -out my.crt -days 365 -nodes -subj \"/CN=localhost\"
[root@localhost ssl]# pwd/root/ssl[root@localhost ssl]# lsmy.crt my.key
  • 创建后可直接使用,如果是使用system管理则修改
cat /etc/systemd/system/dufs.service
[Unit]Description=dufs file serverAfter=network.target[Service]Type=simpleExecStart=/root/dufs --tls-cert /root/ssl/my.crt --tls-key /root/ssl/my.key -c /root/config.yamlRestart=alwaysRestartSec=5sUser=rootGroup=rootWorkingDirectory=/root[Install]WantedBy=multi-user.target
  • 配置文件修改为443端口
serve-path: \'/siku_file/sftp_user/upload\'
bind: 0.0.0.0port: 443path-prefix:allow-all: falseallow-upload: trueallow-delete: trueallow-search: trueallow-symlink: trueallow-archive: true

浏览器即可实现https://IP访问

API调用

  1. 上传文件
curl -T path-to-file http://127.0.0.1:5000/new-path/path-to-file
  • -T path-to-file: 上传本地文件 path-to-file 到服务器。
  • http://127.0.0.1:5000/new-path/path-to-file: 将文件上传到服务器的指定路径 new-path/path-to-file。
  1. 下载文件
curl http://127.0.0.1:5000/path-to-file  # download the filecurl http://127.0.0.1:5000/path-to-file?hash # retrieve the sha256 hash of the file
  • 下载文件: 直接访问文件的 URL 将文件下载到本地。
  • 获取文件的 SHA256 哈希值: 通过在 URL 后添加 ?hash,获取文件的 SHA256 哈希值,而不是文件内容。
  1. 下载文件夹为 ZIP 文件
curl -o path-to-folder.zip http://127.0.0.1:5000/path-to-folder?zip
  • -o path-to-folder.zip: 指定下载后保存的 ZIP 文件名。
  • ?zip: 在文件夹 URL 后添加 ?zip,将该文件夹打包为 ZIP 文件进行下载。
4. 删除文件/文件夹
curl -X DELETE http://127.0.0.1:5000/path-to-file-or-folder
  • -X DELETE: 使用 DELETE 方法删除指定路径的文件或文件夹。
  1. 创建目录
curl -X MKCOL http://127.0.0.1:5000/path-to-folder
  • -X MKCOL: 使用 MKCOL 方法创建一个新的目录。
  1. 移动文件/文件夹
curl -X MOVE http://127.0.0.1:5000/path -H \"Destination: http://127.0.0.1:5000/new-path\"
  • -X MOVE: 使用 MOVE 方法将文件或文件夹从 path 移动到 new-path。
  • -H “Destination: http://127.0.0.1:5000/new-path”: 指定新的路径(目标路径)。
  1. 列出/搜索目录内容
curl http://127.0.0.1:5000?q=Dockerfile  # search for files, similar to `find -name Dockerfile`curl http://127.0.0.1:5000?simple  # output names only, similar to `ls -1`curl http://127.0.0.1:5000?json  # output paths in json format
  • ?q=Dockerfile: 搜索目录中与 Dockerfile 匹配的文件,类似于 find -name Dockerfile。
  • ?simple: 只输出文件或文件夹的名称,类似于 ls -1。
  • ?json: 以 JSON 格式输出目录内容。
  1. 授权访问
curl http://127.0.0.1:5000/file --user user:pass  # basic authcurl http://127.0.0.1:5000/file --user user:pass --digest # digest auth
  • –user user:pass: 使用基本验证,提供用户名 user 和密码 pass 进行身份验证。
  • –digest: 使用摘要认证(Digest Authentication),这是更安全的一种认证方式。
  1. 断点续传下载
curl -C- -o file http://127.0.0.1:5000/file
  • -C-: 断点续传下载,自动从上次中断处恢复下载。
  • -o file: 将下载的文件保存为指定名称。

10.支持断点续传的上传 (Resumable uploads)

upload_offset=$(curl -I -s http://127.0.0.1:5000/file | tr -d \'\\r\' | sed -n \'s/content-length: //p\')dd skip=$upload_offset if=file status=none ibs=1 | \\ curl -X PATCH -H \"X-Update-Range: append\" --data-binary @- http://127.0.0.1:5000/file
  • 第一行: 获取已上传的文件大小,作为断点续传的起点。
    • -I: 获取文件的头信息(header)。
    • tr -d ‘\\r’: 删除回车符。
    • sed -n ‘s/content-length: //p’: 提取 Content-Length 头的值,表示已上传的字节数。
  • 第二行: 从断点处继续上传文件。
    • dd skip=$upload_offset if=file status=none ibs=1: 跳过已上传的部分,从断点处开始读取文件。
    • curl -X PATCH -H “X-Update-Range: append” --data-binary @-: 使用 PATCH 方法上传文件剩余部分。