赞
踩
防火墙开放端口:8080、22122、23000
实际使用过程中发现,Minio文件服务更好用,安装便捷、稳定、自带可视化。MinIO下载
docker pull morunchang/fastdfs
docker run -d --name tracker --net=host morunchang/fastdfs sh tracker.sh
docker run -d --name storage --net=host -e TRACKER_IP=192.168.61.200:22122 -e GROUP_NAME=group1 morunchang/fastdfs sh storage.sh
docker exec -it storage /bin/bash
cd data
vi nginx/conf/nginx.conf
- location /group1/M00 {
- proxy_next_upstream http_502 http_504 error timeout invalid_header;
- proxy_cache http-cache;
- proxy_cache_valid 200 304 12h;
- proxy_cache_key $uri$is_args$args;
- proxy_pass http://fdfs_group1;
- expires 30d;
- }
编辑完后:wq退出编辑
exit
docker restart storage
至此服务安装完毕.
借鉴博客:使用Docker快速搭建FastDFS_米斯特尔.W-CSDN博客_docker fastdfs搭建
集成python
pip install py3Fdfs==2.2.0
配置文件:client.conf
- connect_timeout=30
- network_timeout=60
- tracker_server = 0.0.40.17:22122
- http.tracker_server_port = 8088
上传文件:
- from fdfs_client.client import get_tracker_conf, Fdfs_client
-
- if __name__ == '__main__':
- tracker_conf = get_tracker_conf('./client.conf')
- client = Fdfs_client(tracker_conf)
- filename = client.upload_by_filename(filename='1.jpg')
- print(filename)
集成springboot
- <dependency>
- <groupId>com.github.tobato</groupId>
- <artifactId>fastdfs-client</artifactId>
- <version>1.26.2</version>
- </dependency>
- # 分布式文件系统fastdfs配置
- fdfs:
- # socket连接超时时长
- soTimeout: 1500
- # 连接tracker服务器超时时长
- connectTimeout: 600
- pool:
- # 从池中借出的对象的最大数目
- max-total: 153
- # 获取连接时的最大等待毫秒数100
- max-wait-millis: 102
- # 缩略图生成参数,可选
- thumbImage:
- width: 150
- height: 150
- # 跟踪服务器tracker_server请求地址,支持多个,这里只有一个,如果有多个在下方加- x.x.x.x:port
- trackerList:
- - 192.168.0.1:22122
- #
- # 存储服务器storage_server访问地址
- web-server-url: http://192.168.0.1/
- 在springboot启动类上加
-
- @Import(FdfsClientConfig.class)
- @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
工具类:
- package com.itheima.util;
- import java.io.ByteArrayInputStream;
- import java.io.IOException;
- import java.time.LocalDateTime;
- import java.util.HashSet;
- import java.util.Set;
-
- import com.github.tobato.fastdfs.domain.MataData;
- import com.github.tobato.fastdfs.domain.StorePath;
- import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
- 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.service.FastFileStorageClient;
-
- /**
- * FastDFS客户端包装类
- *
- * @author CL
- *
- */
- @Component
- public class FdfsClientWrapper {
-
- @Autowired
- private FastFileStorageClient fastFileStorageClient;
-
- public String uploadFile(MultipartFile file) throws IOException {
- if (file != null) {
- byte[] bytes = file.getBytes();
- long fileSize = file.getSize();
- String originalFilename = file.getOriginalFilename();
- String extension = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
- return this.uploadFile(bytes, fileSize, extension);
- }
- return null;
- }
-
- /**
- * 文件上传
- *
- * @param bytes 文件字节
- * @param fileSize 文件大小
- * @param extension 文件扩展名
- * @return 返回文件路径(卷名和文件名)
- */
- public String uploadFile(byte[] bytes, long fileSize, String extension) {
- ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
- // 元数据
- Set<MataData> metaDataSet = new HashSet<MataData>();
- metaDataSet.add(new MataData("dateTime", LocalDateTime.now().toString()));
- StorePath storePath = fastFileStorageClient.uploadFile(bais, fileSize, extension, metaDataSet);
- return storePath.getFullPath();
- }
-
- /**
- * 下载文件
- *
- * @param filePath 文件路径
- * @return 文件字节
- * @throws IOException
- */
- public byte[] downloadFile(String filePath) throws IOException {
- byte[] bytes = null;
- if (StringUtils.isNotBlank(filePath)) {
- String group = filePath.substring(0, filePath.indexOf("/"));
- String path = filePath.substring(filePath.indexOf("/") + 1);
- DownloadByteArray byteArray = new DownloadByteArray();
- bytes = fastFileStorageClient.downloadFile(group, path, byteArray);
- }
- return bytes;
- }
-
- /**
- * 删除文件
- *
- * @param filePath 文件路径
- */
- public void deleteFile(String filePath) {
- if (StringUtils.isNotBlank(filePath)) {
- fastFileStorageClient.deleteFile(filePath);
- }
- }
-
- }
测试接口:
-
- import com.itheima.util.FdfsClientWrapper;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.multipart.MultipartFile;
-
- import java.io.IOException;
-
- @RestController
- public class TestController {
- private final FdfsClientWrapper fdfsClientWrapper;
-
- @Autowired
- public TestController(FdfsClientWrapper fdfsClientWrapper) {
- this.fdfsClientWrapper = fdfsClientWrapper;
- }
-
- @RequestMapping("upload")
- public String upload(@RequestParam MultipartFile file) {
- String filePath = null;
- try {
- filePath = fdfsClientWrapper.uploadFile(file);
- } catch (IOException e) {
- return "上传文件失败";
- }
- return filePath;
- }
-
- @RequestMapping("del")
- public String del(@RequestParam String filePath) {
- fdfsClientWrapper.deleteFile(filePath);
- return "删除成功";
- }
- }
访问地址:
fastdfs服务器IP:8080/group1/M00/00/00/rBMvdGFAtNGAVy55AAyEK5YJQm8841.png
三、文件清理
进入容器
docker exec -it storage /bin/bash
cd /data/fast_data/data
这就是文件所在位置,根据需要进行删除即可。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。