Docker 安装 SeaweedFS
官方镜像:https://hub.docker.com/r/chrislusf/seaweedfs
Github: https://github.com/seaweedfs/seaweedfs
官方文档:https://github.com/seaweedfs/seaweedfs/wiki
端口说明:https://github.com/seaweedfs/seaweedfs/blob/master/docker/Dockerfile.local
# volume server grpc port
EXPOSE 18080
# volume server http port
EXPOSE 8080
# filer server grpc port
EXPOSE 18888
# filer server http port
EXPOSE 8888
# master server shared grpc port
EXPOSE 19333
# master server shared http port
EXPOSE 9333
# s3 server http port
EXPOSE 8333
# webdav server http port
EXPOSE 7333单机部署
建议开发环境开放 7333,9333 端口,以及根据需要开放 8888 或 8333 端口(二选一或者都开放均可,看实际需要)。
比如你要使用 Amazon Simple Storage Service(Amazon S3)协议接口的话,就要开放 8333 端口,不用的话就可以不需要开放端口。
docker run \
--name seaweedfs \
--restart=unless-stopped \
-p 7333:7333 \
-p 8333:8333 \
-p 9333:9333 \
-p 8888:8888 \
-v /opt/seaweedfs/data:/data \
-d chrislusf/seaweedfs:3.97 server -filer -s3 -dir="/data"命令说明:
-filer:启用 filer server。-s3:启用 s3 server。- 其中,
-filer和-s3也可以只使用其中的一个。
文件上传测试
请求格式:向 http://<filer地址>:<端口>/<路径> 发起一个 multipart/form-data 类型的 POST 请求。其中,文件数据放在名为 file 的表单字段中。
示例(使用 curl):
假设你的 Filer 运行在 192.168.61.71:8888,想将本地文件 example.txt 上传到 Filer 的 /test 目录下。curl -F file=@/path/to/your/local/example.txt http://127.0.0.1:8888/test/如果上传成功,Filer 会返回一个包含文件详细信息的 JSON 响应。
{
"name":"example.txt",
"size":364581
}查看已上传的文件
浏览器访问
# 浏览器访问 8888 filer server http port 即可。
http://127.0.0.1:8888curl 访问
# 示例:使用 curl 调用 Filer 接口来查询 test 文件夹下的某个文件的内容
curl http://127.0.0.1:8888/test/demo.txt其它工具访问
也可以使用查看工具检查是否已上传成功:https://github.com/seaweedfs/seaweedfs/wiki/Applications
文件下载测试
访问格式:http://<filer地址>:<端口>/<文件路径>要下载刚才上传的 /test/example.txt 文件:
# 使用 curl 下载
curl -O http://127.0.0.1:8888/test/example.txt
# 使用浏览器访问下载
http://127.0.0.1:8888/test/example.txt使用 JWT 保护 filer HTTP
默认 filer server 是没有认证保护的,这在内网环境下问题不大,但如果要暴露到公网,就需要添加 JWT 保护。
生成默认的配置文件 security.toml
# 在宿主机上执行此命令,生成默认的 security.toml
docker run --rm chrislusf/seaweedfs:3.97 scaffold -config=security > security.toml生成密钥 key
需要为 jwt.filer_signing.key 和 jwt.filer_signing.read.key 分别生成两个不同的密钥 key。
后续需要根据这个 key 来使用 JWT 工具栏生成一个 JWT token,以供文件的上传和下载使用。
# 执行两次,生成两个密钥
openssl rand -base64 32
# 生成结果类似这样的两个字符串
AfpqdFEUpOzBQkwFWBW8lWiC9D33wRXycKBKOAh6wvM=修改密钥
把上面生成的密码配置到如下位置:
# 如果此JWT密钥已配置,则Filer仅接受使用此JWT签名的HTTP写入请求[citation:1]
[jwt.filer_signing]
key = "你的写入密钥" # 在此处填入你的密钥字符串
expires_after_seconds = 10 # seconds
# 如果此JWT密钥已配置,则Filer仅接受使用此JWT签名的HTTP读取请求[citation:1]
[jwt.filer_signing.read]
key = "你的读取密钥" # 在此处填入你的密钥字符串
expires_after_seconds = 10 # seconds挂载配置文件启动
注意:把 security.toml 挂载到 /etc/seaweedfs/security.toml 位置,即可自动读取。
# Put this file to one of the location, with descending priority
# ./security.toml
# $HOME/.seaweedfs/security.toml
# /etc/seaweedfs/security.toml
# this file is read by master, volume server, filer, and workerdocker run \
--name seaweedfs \
--restart=unless-stopped \
-p 7333:7333 \
-p 8333:8333 \
-p 9333:9333 \
-p 8888:8888 \
-v /opt/seaweedfs/security.toml:/etc/seaweedfs/security.toml \
-v /opt/seaweedfs/data:/data \
-d chrislusf/seaweedfs:3.97 server -filer -s3 -dir="/data"若 security.toml 在其它位置,那么启动时就要指定具体的配置文件的位置:
docker run \
--name seaweedfs \
--restart=unless-stopped \
-p 7333:7333 \
-p 8333:8333 \
-p 9333:9333 \
-p 8888:8888 \
-v /opt/seaweedfs/security.toml:/etc/seaweedfs/security.toml \
-v /opt/seaweedfs/data:/data \
-d chrislusf/seaweedfs:3.97 server -filer -s3 -dir="/data" -security.config=/etc/seaweedfs/security.tomlHow to send a JWT to the Filer
The Filer supports receiving a JWT in three different forms:
Through the Authorization: Bearer <token> header
Via the request's query parameters: http://localhost:8888/buckets/all?jwt=token
In an HTTP-only cookie named AT(Access Token)即使用 JWT 来上传或下载文件有以下三种使用方式:
通过设置请求头:Authorization: Bearer <token>
通过请求的查询参数:http://localhost:8888/buckets/all?jwt=token
在一个名为AT(访问令牌)的 HTTP-only cookie 中使用工具类生成 JWT token
@Test
public void generateJwt() {
// 这里的 key 就是使用 openssl 生成的密钥 key
String key = "AfpqdFEUpOzBQkwFWBW8lWiC9D33wRXycKBKOAh6wvM=";
String token = JWTUtil.createToken(null, key.getBytes(StandardCharsets.UTF_8));
log.info(token);
boolean verify = JWTUtil.verify(token, key.getBytes(StandardCharsets.UTF_8));
Assertions.assertTrue(verify);
}生成的 JWT 格式:eyJ0eXAiO4JKV1QiLCJhbGciO3JIUzI1NiJ9.e30.xtrTYsadhSeqg6bjBR4gMUkIETvADSI1EJQItCP8Uds
启用 JWT 后查看已上传文件
浏览器访问(带 jwt)
# 浏览器访问 8888 filer server http port, 同时带上 jwt 参数即可。
http://127.0.0.1:8888?jwt=eyJ0eXAiO4JKV1QiLCJhbGciO3JIUzI1NiJ9.e30.xtrTYsadhSeqg6bjBR4gMUkIETvADSI1EJQItCP8Uds当然,通过给 header 增加 Authorization: Bearer token 的方式也是可以的。
启用 JWT 后的文件上传测试
curl -F file=@/path/to/your/local/example.txt http://127.0.0.1:8888/test/
curl -X POST \
-H "Authorization: Bearer 你的写操作JWT令牌" \
-F "file=@/path/to/your/local/file.jpg" \
http://127.0.0.1:8888/path/to/upload/
# 示例 (Authorization 请求头)
curl -X POST \
-H "Authorization: Bearer eyJ0eXAiO4JKV1QiLCJhbGciO3JIUzI1NiJ9.e30.xtrTYsadhSeqg6bjBR4gMUkIETvADSI1EJQItCP8Uds" \
-F "file=@/path/to/your/local/file.jpg" \
http://127.0.0.1:8888/path/to/upload/
# 示例 (jwt 查询参数)
curl -X POST \
-F "file=@/path/to/your/local/file.jpg" \
http://127.0.0.1:8888/path/to/upload?jwt=eyJ0eXAiO4JKV1QiLCJhbGciO3JIUzI1NiJ9.e30.xtrTYsadhSeqg6bjBR4gMUkIETvADSI1EJQItCP8Uds如果上传成功,Filer 会返回一个包含文件详细信息的 JSON 响应。
{
"name":"example.txt",
"size":364581
}启用 JWT 后的文件下载测试
访问格式:http://<filer地址>:<端口>/<文件路径>要下载刚才上传的 /test/example.txt 文件:
# 使用 curl 下载
curl -X GET \
-H "Authorization: Bearer 你的读操作JWT令牌" \
-o downloaded_file.jpg \
http://127.0.0.1:8888/path/to/your/file.jpg
# 使用浏览器访问下载
http://127.0.0.1:8888/path/to/your/file.jpg?jwt=你的读操作JWT令牌
# 示例 (Authorization 请求头)
curl -X GET \
-H "Authorization: Bearer eyJ0eXAiO4JKV1QiLCJhbGciO3JIUzI1NiJ9.e30.xtrTYsadhSeqg6bjBR4gMUkIETvADSI1EJQItCP8Uds" \
-o downloaded_file.jpg \
http://127.0.0.1:8888/path/to/your/file.jpg
# 示例 (jwt 查询参数)
curl -X GET \
-o downloaded_file.jpg \
http://127.0.0.1:8888/path/to/your/file.jpg?jwt=eyJ0eXAiO4JKV1QiLCJhbGciO3JIUzI1NiJ9.e30.xtrTYsadhSeqg6bjBR4gMUkIETvADSI1EJQItCP8Uds