平常开发难免会需要图片的上传和下载,所以搭建一个自己的图片服务器是很必要的。不仅仅便于后端存储只需要存url,前端也可以直接通过url展示图片。
搭建一个新的服务可能还会影响到其他服务的配置,所以这里强烈推荐使用docker进行部署。部署过程其实还是挺简单的,只是可能还是会有一些不经意的坑会踩。这里就简单分享一下搭建流程。
因为我的服务器是Ubuntu所以直接使用下面的命令就可以了
apt install docker.io
这里安装完后可能使用docker还不会有提示,需要关掉当前的shell,重新连接,再次进入就会有docker的命令提示了。
但是,因为docker默认是需要root才能执行的,为避免麻烦每次输入sudo,这里可以设置一下用户组。
sudo usermod -a -G docker $USER
这里使用id查看一下就会显示当前用户的group多了一个docker,同样需要重新打开shell,再次使用docker命令就不用再输入sudo了。
搜索需要的镜像
docker search fastdfs
这里我们需选择delron/fastdfs,因为这个镜像包含了我们需要的Nginx。
在pull镜像之前我们可以对我们的docker源配置为国内的源,避免下载太慢。docker下载好后有时候没有daemon.json,我直接新建一个就好。
sudo vim /etc/docker/daemon.json
然后拷贝下面的内容
#这里可以连添加多个源,一般一个就够了
{
"registry-mirrors": ["https://km0lwhqm.mirror.aliyuncs.com", "http://hub-mirror.c.163.com", "https://registry.docker-cn.com"]
}
修改完后,需要重新加载我们的配置文件:(这里也可以直接使用 sudo systemctl restart docker 对docker进行重启,如不想停掉docker中其他的服务就使用下面的命令进行加载配置文件)
sudo systemctl daemon-reload
配置完成后就可以进行pull操作了
docker pull delron/fastdfs
启动tracker:这里的-v的参数前面是实际数据映射的路径,可以根据自己的需要定义。
docker run -d --network=host --name tracker -v /home/tracker:/var/fdfs delron/fastdfs tracker
启动storage:这里的ip需要时公网ip否则否面上传图片可能会报错。
docker run -d --network=host --name storage -e TRACKER_SERVER=你自己的公网IP:22122 -v /home/storage:/var/fdfs -e GROUP_NAME=group1 delron/fastdfs storage
其实到这里我们的fastDFS服务的配置已经基本完成了。
我们可以测试一下,直接从本地拷贝一张图片到刚才我们配置的 /home/storage/ 路径下上传一张图片,用于后面的测试。
然后进入storage: docker exec -it storage bash;切换到内部映射路径下:cd /var/fdfs/
可以看到我们映射的路径下有对应的图片,我们使用命令进行一次上传操作:/usr/bin/fdfs_upload_file /etc/fdfs/client.conf <你自己的图片>
上传成功后我们会得到一个路径,拼上对应的ip和端口(默认8888)访问一下
http://你的ip:8888/group1/M00/00/00/wKgAnl5gzK6ALRs6AABrOeFd0Ug370.png
端口问题:如果使用的是默认的配置那么,fastdfs的端口分别是8888,22122,23000这三个端口。
这里我们只需要在我们服务器控制台的安全组配置中加入对应的端口就可以了。
1.新建一个SpringBoot项目
2.加入依赖
com.github.tobato
fastdfs-client
1.26.2
3.配置yml文件
# ===================================================================
# 分布式文件系统FDFS配置
# ===================================================================
fdfs:
so-timeout: 15001
connect-timeout: 15001
thumb-image: #缩略图生成参数,一般头像使用,根据需求配置
width: 80
height: 80
tracker-list[0]: 你的服务的ip:22122
4.构建一个client
package com.clf.miniwechat.utils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
@Component
public class FastDFSClient {
@Autowired
private FastFileStorageClient storageClient;
/**
* 上传文件
*
* @param file
* 文件对象
* @return 文件访问地址
* @throws IOException
*/
public String uploadFile(MultipartFile file) throws IOException {
StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
FilenameUtils.getExtension(file.getOriginalFilename()), null);
return storePath.getPath();
}
public String uploadFile2(MultipartFile file) throws IOException {
StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(),
FilenameUtils.getExtension(file.getOriginalFilename()), null);
return storePath.getPath();
}
public String uploadQRCode(MultipartFile file) throws IOException {
StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
"png", null);
return storePath.getPath();
}
public String uploadFace(MultipartFile file) throws IOException {
StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(),
"png", null);
return storePath.getPath();
}
public String uploadBase64(MultipartFile file) throws IOException {
StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(),
"png", null);
return storePath.getPath();
}
/**
* 将一段字符串生成一个文件上传
*
* @param content
* 文件内容
* @param fileExtension
* @return
*/
public String uploadFile(String content, String fileExtension) {
byte[] buff = content.getBytes(Charset.forName("UTF-8"));
ByteArrayInputStream stream = new ByteArrayInputStream(buff);
StorePath storePath = storageClient.uploadFile(stream, buff.length, fileExtension, null);
return storePath.getPath();
}
/**
* 删除文件
*
* @param fileUrl
* 文件访问地址
* @return
*/
public void deleteFile(String fileUrl) {
if (StringUtils.isEmpty(fileUrl)) {
return;
}
try {
StorePath storePath = StorePath.praseFromUrl(fileUrl);
storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
} catch (FdfsUnsupportStorePathException e) {
e.getMessage();
}
}
}
5.最后在test中进行测试