docker安装minio及实现文件上传、删除、下载方式
作者:宏辉
这篇文章主要介绍了docker安装minio及实现文件上传、删除、下载方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
docker安装minio及文件上传、删除、下载
要搭建一个分布式文件存储系统,听说fastDFS能实现,于是我搜了一下fastDFS,结果半路杀出一个minio
考虑到拓展性、开发api文档完善度、部署效率等原因我选择了minio。
因为个人没真正使用过fastDFS,所以不作过多评价,占个坑,以后有机会深度体验后回来写评测。
1. docker安装minio步骤
第一步 查镜像
docker search minio
第二步 拉镜像
docker pull minio/minio
第三步 启动容器
docker run -p 9000:9000 --name minio -d --restart=always -e “MINIO_ACCESS_KEY=admin” -e “MINIO_SECRET_KEY=admin123456” -v /home/data:/data -v /home/config:/root/.minio minio/minio server /data
第四步 登录界面
- http//:ip+9000
- ACCESS_KEY:damin
- SECRET_KEY:admin123456
2. minio实现文件上传、删除、下载
项目结构
pom依赖:
<dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>3.0.10</version> </dependency> </dependencies>
启动类:
@SpringBootApplication @EnableDiscoveryClient public class CfUploadApplication { public static void main(String[] args) { SpringApplication.run(CfUploadApplication.class,args); } }
yml配置文件:
server: port: 8002 spring: application: name: upload-service cloud: nacos: discovery: server-addr: localhost:8848 servlet: multipart: enabled: true #开启文件上传 max-file-size: 500MB max-request-size: 500MB minio: endpoint: http://localhost:9000 #Minio服务所在地址 bucketName: cheung #存储桶名称 accessKey: admin #访问的key secretKey: admin123456 #访问的秘钥 logging: level: com.heima: debug
controller代码:
package com.cheung.upload.controller; import io.minio.MinioClient; import io.minio.policy.PolicyType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.InputStream; import java.io.OutputStream; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Date; @RestController @RequestMapping("file") public class UploadController { private static final Logger LOGGER = LoggerFactory.getLogger(UploadController.class); @Value("${minio.endpoint}") private String ENDPOINT; @Value("${minio.bucketName}") private String BUCKETNAME; @Value("${minio.accessKey}") private String ACCESSKEY; @Value("${minio.secretKey}") private String SECRETKEY; //文件创建 @PostMapping("/upload") public String upload(MultipartFile file) { String s = null; try { MinioClient minioClient = new MinioClient(ENDPOINT, ACCESSKEY, SECRETKEY); //存入bucket不存在则创建,并设置为只读 if (!minioClient.bucketExists(BUCKETNAME)) { minioClient.makeBucket(BUCKETNAME); minioClient.setBucketPolicy(BUCKETNAME, "*.*", PolicyType.READ_ONLY); } String filename = file.getOriginalFilename(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 文件存储的目录结构 String objectName = sdf.format(new Date()) + "/" + filename; // 存储文件 minioClient.putObject(BUCKETNAME, objectName, file.getInputStream(), file.getContentType()); LOGGER.info("文件上传成功!"); s = ENDPOINT + "/" + BUCKETNAME + "/" + objectName; } catch (Exception e) { LOGGER.info("上传发生错误: {}!", e.getMessage()); } return s; } //文件删除 @DeleteMapping("/delete") public String delete(String name) { try { MinioClient minioClient = new MinioClient(ENDPOINT, ACCESSKEY, SECRETKEY); minioClient.removeObject(BUCKETNAME, name); } catch (Exception e) { return "删除失败" + e.getMessage(); } return "删除成功"; } //文件下载 @GetMapping("/download") public void downloadFiles(@RequestParam("filename") String filename, HttpServletResponse httpResponse) { try { MinioClient minioClient = new MinioClient(ENDPOINT, ACCESSKEY, SECRETKEY); InputStream object = minioClient.getObject(BUCKETNAME, filename); byte buf[] = new byte[1024]; int length = 0; httpResponse.reset(); httpResponse.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8")); httpResponse.setContentType("application/octet-stream"); httpResponse.setCharacterEncoding("utf-8"); OutputStream outputStream = httpResponse.getOutputStream(); while ((length = object.read(buf)) > 0) { outputStream.write(buf, 0, length); } outputStream.close(); } catch (Exception ex) { LOGGER.info("导出失败:", ex.getMessage()); } } }
上传文件
使用postman进行测试
删除文件
下载文件
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。