写在前面:
最近有一个想法,做一个程序员师徒体系。由于在大学期间的我在学习java的时分十分地迷茫,找不到自己的方向,也没有一个社会上有经历的前辈去指导,所以走了很多的弯路。后来工作了,想把自己的避坑经历分享给他人,但是发现身边都是有经历的开发者,也没有机会去分享自己的想法,所以富贵同学就想做一个程序员专属的师徒体系,秉承着学徒可以有人指导少走弯路,师傅能桃李满天下的意图,所以开端做这个师徒体系,也会同步更新该体系所用到的技术,而且作为教程分享给我们,期望我们可以关注一波。
minio在该项目中完成的中心文件为:MinioConfig,MinioUtil,FileController
做这个的意图想的是谈天的过程中必定会有文件的发送,所以我选择了minio去集成,用springboot集成minio首先介绍一下什么是minio:
minio是一个基于Apache License v2.0开源协议的目标存储服务。它兼容亚马逊S3云存储服务接口,十分适合于存储大容量非结构化的数据,例如图片、视频、日志文件、备份数据和容器/虚拟机镜像等,而一个目标文件可以是恣意巨细,从几kb到最大5T不等。
然后再感谢spring-minio提供的思路,该教程作为他的升级版补充了一些sdk的api。
如果没有装置minio请参照:
blog.csdn.net/csdnerM/art…
第一步,我们导入minio的jar包
<!--minio-->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.2.0</version>
</dependency>
<!--minio-->
第二步,编写装备类
package com.wangfugui.apprentice.config;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MinioConfig {
@Value("${minio.url}")
private String url;
@Value("${minio.accessKey}")
private String accessKey;
@Value("${minio.secretKey}")
private String secretKey;
@Bean
public MinioClient getMinioClient() {
return MinioClient.builder().endpoint(url)
.credentials(accessKey, secretKey).build();
}
}
这一步的意图是将MinioClient 给注入到spring的容器中以供我们运用
第三步,编写util类
package com.wangfugui.apprentice.common.util;
import com.wangfugui.apprentice.dao.dto.Fileinfo;
import io.minio.BucketExistsArgs;
import io.minio.CopyObjectArgs;
import io.minio.CopySource;
import io.minio.GetObjectArgs;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.ListObjectsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveBucketArgs;
import io.minio.RemoveObjectArgs;
import io.minio.Result;
import io.minio.StatObjectArgs;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
@Component
public class MinioUtil {
@Autowired
private MinioClient minioClient;
/**
* 创建一个桶
*/
public void createBucket(String bucket) throws Exception {
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
if (!found) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
}
}
/**
* 上传一个文件
*/
public void uploadFile(InputStream stream, String bucket, String objectName) throws Exception {
minioClient.putObject(PutObjectArgs.builder().bucket(bucket).object(objectName)
.stream(stream, -1, 10485760).build());
}
/**
* 列出一切的桶
*/
public List<String> listBuckets() throws Exception {
List<Bucket> list = minioClient.listBuckets();
List<String> names = new ArrayList<>();
list.forEach(b -> {
names.add(b.name());
});
return names;
}
/**
* 列出一个桶中的一切文件和目录
*/
public List<Fileinfo> listFiles(String bucket) throws Exception {
Iterable<Result<Item>> results = minioClient.listObjects(
ListObjectsArgs.builder().bucket(bucket).recursive(true).build());
List<Fileinfo> infos = new ArrayList<>();
results.forEach(r->{
Fileinfo info = new Fileinfo();
try {
Item item = r.get();
info.setFilename(item.objectName());
info.setDirectory(item.isDir());
infos.add(info);
} catch (Exception e) {
e.printStackTrace();
}
});
return infos;
}
/**
* 下载一个文件
*/
public InputStream download(String bucket, String objectName) throws Exception {
InputStream stream = minioClient.getObject(
GetObjectArgs.builder().bucket(bucket).object(objectName).build());
return stream;
}
/**
* 删去一个桶
*/
public void deleteBucket(String bucket) throws Exception {
minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucket).build());
}
/**
* 删去一个目标
*/
public void deleteObject(String bucket, String objectName) throws Exception {
minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(objectName).build());
}
/**
* 仿制文件
*
* @Param: [sourceBucket, sourceObject, targetBucket, targetObject]
* @return: void
* @Author: MrFugui
* @Date: 2021/11/15
*/
public void copyObject(String sourceBucket, String sourceObject, String targetBucket, String targetObject) throws Exception {
this.createBucket(targetBucket);
minioClient.copyObject(CopyObjectArgs.builder().bucket(targetBucket).object(targetObject)
.source(CopySource.builder().bucket(sourceBucket).object(sourceObject).build()).build());
}
/**
* 获取文件信息
*
* @Param: [bucket, objectName]
* @return: java.lang.String
* @Author: MrFugui
* @Date: 2021/11/15
*/
public String getObjectInfo(String bucket, String objectName) throws Exception {
return minioClient.statObject(StatObjectArgs.builder().bucket(bucket).object(objectName).build()).toString();
}
/**
* 生成一个给HTTP GET请求用的presigned URL。浏览器/移动端的客户端可以用这个URL进行下载,即使其所在的存储桶是私有的。
*
* @Param: [bucketName, objectName, expires]
* @return: java.lang.String
* @Author: MrFugui
* @Date: 2021/11/15
*/
public String getPresignedObjectUrl(String bucketName, String objectName, Integer expires) throws Exception {
GetPresignedObjectUrlArgs build = GetPresignedObjectUrlArgs
.builder().bucket(bucketName).object(objectName).expiry(expires).method(Method.GET).build();
return minioClient.getPresignedObjectUrl(build);
}
/**
* 获取minio中一切的文件
*
* @Param: []
* @return: java.util.List<boot.spring.domain.Fileinfo>
* @Author: MrFugui
* @Date: 2021/11/15
*/
public List<Fileinfo> listAllFile() throws Exception {
List<String> list = this.listBuckets();
List<Fileinfo> fileinfos = new ArrayList<>();
for (String bucketName : list) {
fileinfos.addAll(this.listFiles(bucketName));
}
return fileinfos;
}
}
这儿集成了minio sdk中的api 这是minio的api文档
第四步,编写controller类
package com.wangfugui.apprentice.controller;
import com.wangfugui.apprentice.common.util.MinioUtil;
import com.wangfugui.apprentice.common.util.ResponseUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
@Api(tags = "文件操作接口")
@RestController
@RequestMapping(value = "/file")
public class FileController {
@Autowired
MinioUtil minioUtil;
@ApiOperation("上传一个文件")
@RequestMapping(value = "/uploadfile", method = RequestMethod.POST)
public ResponseUtils fileupload(@RequestParam MultipartFile uploadfile, @RequestParam String bucket,
@RequestParam(required = false) String objectName) throws Exception {
minioUtil.createBucket(bucket);
if (objectName != null) {
minioUtil.uploadFile(uploadfile.getInputStream(), bucket, objectName + "/" + uploadfile.getOriginalFilename());
} else {
minioUtil.uploadFile(uploadfile.getInputStream(), bucket, uploadfile.getOriginalFilename());
}
return ResponseUtils.success();
}
@ApiOperation("列出一切的桶")
@RequestMapping(value = "/listBuckets", method = RequestMethod.GET)
public ResponseUtils listBuckets() throws Exception {
return ResponseUtils.success(minioUtil.listBuckets());
}
@ApiOperation("递归列出一个桶中的一切文件和目录")
@RequestMapping(value = "/listFiles", method = RequestMethod.GET)
public ResponseUtils listFiles(@RequestParam String bucket) throws Exception {
return ResponseUtils.success(minioUtil.listFiles(bucket));
}
@ApiOperation("下载一个文件")
@RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
public void downloadFile(@RequestParam String bucket, @RequestParam String objectName,
HttpServletResponse response) throws Exception {
InputStream stream = minioUtil.download(bucket, objectName);
ServletOutputStream output = response.getOutputStream();
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(objectName.substring(objectName.lastIndexOf("/") + 1), "UTF-8"));
response.setContentType("application/octet-stream");
response.setCharacterEncoding("UTF-8");
IOUtils.copy(stream, output);
}
@ApiOperation("删去一个文件")
@RequestMapping(value = "/deleteFile", method = RequestMethod.GET)
public ResponseUtils deleteFile(@RequestParam String bucket, @RequestParam String objectName) throws Exception {
minioUtil.deleteObject(bucket, objectName);
return ResponseUtils.success();
}
@ApiOperation("删去一个桶")
@RequestMapping(value = "/deleteBucket", method = RequestMethod.GET)
public ResponseUtils deleteBucket(@RequestParam String bucket) throws Exception {
minioUtil.deleteBucket(bucket);
return ResponseUtils.success();
}
@ApiOperation("仿制一个文件")
@GetMapping("/copyObject")
public ResponseUtils copyObject(@RequestParam String sourceBucket, @RequestParam String sourceObject, @RequestParam String targetBucket, @RequestParam String targetObject) throws Exception {
minioUtil.copyObject(sourceBucket, sourceObject, targetBucket, targetObject);
return ResponseUtils.success();
}
@GetMapping("/getObjectInfo")
@ApiOperation("获取文件信息")
public ResponseUtils getObjectInfo(@RequestParam String bucket, @RequestParam String objectName) throws Exception {
return ResponseUtils.success(minioUtil.getObjectInfo(bucket, objectName));
}
@GetMapping("/getPresignedObjectUrl")
@ApiOperation("获取一个连接以供下载")
public ResponseUtils getPresignedObjectUrl(@RequestParam String bucket, @RequestParam String objectName, @RequestParam Integer expires) throws Exception {
return ResponseUtils.success(minioUtil.getPresignedObjectUrl(bucket, objectName, expires));
}
@GetMapping("/listAllFile")
@ApiOperation("获取minio中一切的文件")
public ResponseUtils listAllFile() throws Exception {
return ResponseUtils.success(minioUtil.listAllFile());
}
}
留意这儿的ResponseUtils请去往库房中收取,库房地址: SpringBoot+MinIO
妈耶,完了在yml中装备minio的地址,你没发现吧,叫你上课不认真听
第五步,装备yml文件
minio:
url: http://1localhost:9000 #目标存储服务的URL
accessKey: root #账户
secretKey: 123456 #暗码
这步咋来的?请看我的Linux装置MinIO
第六步,启动项目,拜访api
就ok啦!! 到这儿就完成了springboot集成minio完成文件的上传等功能
拓宽: 经过链接拜访文件
下载minio client
wget https://dl.min.io/client/mc/release/linux-amd64/mc
赋予权限
chmod a+x mc
添加minio server
./mc config host add minio http://ip:port admin passwd
设置需求开放下载的bucket, 留意需求带minio
./mc policy set download minio/yourbucket
浏览器拜访
http://ip:9000/apprentice/xxxx.png
说在之后
师徒体系我会一向更新,由于是开源的项目,所以我也期望又更多的小伙伴参加进来!! 这是程序员师徒管理体系的地址: 程序员师徒管理体系