FastDFS入门
FastDFS安装
使用Docker安装FastDFS
拉取镜像
docker pull morunchang/fastdfs
运行tracker
docker run -d --name tracker --net=host morunchang/fastdfs sh tracker.sh
运行storage
docker run -d --name storage --net=host -e TRACKER_IP=<your tracker server address>:22122 -e GROUP_NAME=<group name> morunchang/fastdfs sh storage.sh
docker run -d --name storage --net=host -e TRACKER_IP=192.168.238.129:22122 -e GROUP_NAME=group1 morunchang/fastdfs sh storage.sh
修改nginx的配置
docker exec -it storage /bin/bash
vi /etc/nginx/conf/nginx.conf
添加以下内容
location ~ /M00 {
root /data/fast_data/data;
ngx_fastdfs_module;
#禁止浏览器缓存
add_header Cache-Control no-store;
}
重启storage容器
exit
docker restart storage
查看tracker.conf和storage.conf配置文件
docker exec -it storage /bin/bash
cd /etc/fdfs
vim tracker.conf
vim storage.conf
文件上传服务
<!-- FastDFS依赖 -->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.7</version>
</dependency>
server:
port: 9002
spring:
application:
name: fastdfs-fileService
servlet:
multipart:
enabled: true
max-file-size: 10MB # 单个文件上传大小
max-request-size: 20MB # 总文件上传大小
fdfs:
# 链接超时
connect-timeout: 60
# 读取时间
so-timeout: 60
# 生成略缩图参数
thumb-image:
width: 150
height: 150
tracker-list: 192.168.238.129:22122
配置文件
@Configuration
@Import(FdfsClientConfig.class)
public class FDSConfig {
}
工具类
@Component
public class FDSUtil {
@Autowired
private FastFileStorageClient fastFileStorageClient;
public String upload(MultipartFile multipartFile) throws IOException {
String extName = FilenameUtils.getExtension(multipartFile.getOriginalFilename());
StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(multipartFile.getInputStream(), multipartFile.getSize(), extName, null);
return storePath.getFullPath();
}
public void deleteFile(String fileUrl){
StorePath storePath = StorePath.parseFromUrl(fileUrl);
fastFileStorageClient.deleteFile(storePath.getGroup(),storePath.getPath());
}
}
@RestController
public class FDSController {
@Autowired
private FDSUtil fdsUtil;
@PostMapping(value = "/uploadFile",headers = "content-type=multipart/form-data")
public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) {
String result = "";
try {
String upload = fdsUtil.upload(file);
if (StringUtils.isEmpty(upload)) {
result = "上传失败";
} else {
result = upload;
}
} catch (IOException e) {
e.printStackTrace();
result = "服务器异常";
}
return ResponseEntity.ok(result);
}
@GetMapping(value = "/deleteByPath")
public ResponseEntity<String> deleteByPath(String filePathName) {
fdsUtil.deleteFile(filePathName);
return ResponseEntity.ok("success");
}
}