1、配置文件服务器
@AllArgsConstructor
@Getter
public enum MinioEnum {
ADDRESS("***********","minio地址"),
BUCKET_NAME("****","存储桶名称"),
ACCESS_KEY("****","访问的key"),
SECRET_KEY("*******","访问的秘钥"),
AVATAR("****","文件在桶中的存放路径"),
;
final String value;
final String remark;
}
2 编写接口
@Api(tags = "minio文件管理")
@ApiOperation(value = "文件上传")
@PostMapping("/upLoadAvatar")public ResponseObj upLoadAvatar(MultipartFile file) {
try{
MinioClient minioClient =
MinioClient.builder()
.endpoint(MinioEnum.ADDRESS.getValue())
.credentials(MinioEnum.ACCESS_KEY.getValue(), MinioEnum.SECRET_KEY.getValue())
.build();
boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(MinioEnum.BUCKET_NAME.getValue()).build());
if (!exists){
minioClient.makeBucket(MakeBucketArgs.builder().bucket(MinioEnum.BUCKET_NAME.getValue()).build());
}
String fileName =file.getOriginalFilename();
String oobName =new SimpleDateFormat("yyyy-MM-dd-").format(new Date())
+ UUID.randomUUID().toString().replaceAll("-", "")
+ fileName.substring(fileName.lastIndexOf("."));
minioClient.putObject(PutObjectArgs.builder()
.bucket(MinioEnum.BUCKET_NAME.getValue())
.object(MinioEnum.AVATAR.getValue()+"/"+oobName)
.stream(file.getInputStream(),file.getSize(),-1)
.contentType(file.getContentType())
.build());
Map<String, String> reqParams = new HashMap<>();
reqParams.put("response-content-type", "application/json");
String url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
.bucket(MinioEnum.BUCKET_NAME
.getValue()).object(MinioEnum.AVATAR.getValue()+"/"+ file.getOriginalFilename())
.method(Method.GET)
.extraQueryParams(reqParams)
.build());
String url2=MinioEnum.ADDRESS.getValue()+"/"+MinioEnum.BUCKET_NAME.getValue()+"/"+MinioEnum.AVATAR.getValue()+"/"+oobName;
return ResponseObj.success(oobName);
}catch (Exception e){
e.printStackTrace();
return ResponseObj.success("异常了");
}
}
@GetMapping("/downLoadAvatar")
@ApiOperation(value = "文件下载")
public void upLoadAvatar2(String name,HttpServletResponse response) throws IOException {
InputStream in=null;
try{
MinioClient minioClient =
MinioClient.builder()
.endpoint(MinioEnum.ADDRESS.getValue())
.credentials(MinioEnum.ACCESS_KEY.getValue(), MinioEnum.SECRET_KEY.getValue())
.build();
StatObjectResponse statObject = minioClient.statObject(StatObjectArgs.builder()
.bucket(MinioEnum.BUCKET_NAME.getValue())
.object(MinioEnum.AVATAR.getValue()+"/"+name)
.build());
response.setContentType(statObject.contentType());
response.setHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(name, "UTF-8"));
in = minioClient.getObject(GetObjectArgs.builder()
.bucket(MinioEnum.BUCKET_NAME.getValue())
.object(MinioEnum.AVATAR.getValue()+"/"+name)
.build());
IOUtils.copy(in,response.getOutputStream());
}catch (Exception e){
e.printStackTrace();
}
finally {
if (in != null) {
in.close();
}
}
}
}
3、导入POM
<!-- https://mvnrepository.com/artifact/io.minio/minio -->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.4.3</version>
</dependency>
<!-- 官方 miniodemo需要的依赖,没有的话启动会说找不到okhttp啥的;-->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.2</version>
</dependency>