背景
因为不想购买对象储存服务(要收费,公司不让),把文件直接放在后端服务器上好像又不是那么方便的管理
so
直接使用开源的文件管理系统,我用的是fastDFS(我这边是使用单台storage示例)
搭建fastDFS
这里我使用的是docker进行搭建,当然也可以使用二进制去make然后再运行对应的linux程序
pull docker image
docker search fastdfs
这里我使用的是 delron/fastdfs
docker pull delron/fastdfs
创建挂载的文件夹
mkdir /usr/fdfs/tracker
mkdir /usr/fdfs/storage
运行镜像
先运行tracker,tracker 是跟踪服务器,起调度作用
docker run -dti --network=host --name fdfs-tracker -v /var/fdfs/tracker:/var/fdfs -v /etc/localtime:/etc/localtime delron/fastdfs tracker
使用 --network=host
,此时,Docker 容器的网络会附属在主机上,两者是互通的。
运行storage
docker run -dti --network=host --name fdfs-storage -e TRACKER_SERVER={{IP}}:22122 -v /var/fdfs/storage:/var/fdfs -v /etc/localtime:/etc/localtime delron/fastdfs storage
-v /etc/localtime:/etc/localtime
的目的是让容器时区和宿主时区一致
确认时间是否一致,开启防盗链之后需要时间有效期
docker exec -it {{storageImageId}} /bin/bash
date
修改storage内的开启防盗链并配置过期时间、加密key
docker exec -it {{storageImageId}} /bin/bash
vi /etc/fdfs/http.conf
修改其中的
http.anti_steal.check_token=true
http.anti_steal.token_ttl=1800
http.anti_steal.secret_key=FastDFS1234567890
http.anti_steal.token_check_fail=/var/fdfs/data/00/00/error.png
需要先上传一个文件到 挂载的data仓库内去
-
check_token 修改false -> true,启动token确认
-
token_ttl 防盗链时效
-
secret_key 加密key
-
token_check_fail 校验失败时,返回的文件
这时候访问文件如果没携带?token=xxx&ts=xxxx会返回nginx的400 badRequest,如果token验证失败,会返回配置的token_check_fail文件
java生成访问token
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.29-SNAPSHOT</version>
</dependency>
这时候你会发现,拉不到包 不要怀疑,阿里仓库也没有
解决方案: git clone github.com/happyfish10…
(超时的话,就去下载zip 然后解压,并且需要账号jdk配置好maven)
进到下载的文件中 mvn clean install
如果报错 6或7版本啥的,在pom里将
<jdk.version>1.6</jdk.version>
修改成
<jdk.version>1.8</jdk.version>
然后可以选择打入私服或者本地引用(放lib systemPath)
ProtoCommon.getToken(fileUrl, ts, secretKey);
其中file url是指上传完后 bucket(group1)后的地址 M00/00/00/xxxx.xx
- 代码示例(参考:blog.csdn.net/D_A_I_H_A_O…)
private static String secretKey="FastDFS1234567890";
//容器默认的nginx的端口是8888
private static String remoteUrl="http://xxx.xxx.xxx.xxx:8888";
public static String getTokenUrl(String group, String remoteFile) throws UnsupportedEncodingException, NoSuchAlgorithmException, MyException {
int ts = (int) (System.currentTimeMillis()/1000);
String token = ProtoCommon.getToken(remoteFile, ts, secretKey);
StringBuffer url = new StringBuffer(remoteUrl);
url.append("/")
.append(group)
.append("/")
.append(remoteFile)
.append("?")
.append("token=")
.append(token)
.append("&ts=")
.append(ts);
return url.toString();
}
然后赶紧把任务todo打上勾☑️,日报写上可以 开始合理休(mo)息(yu)了 ~