定义属性

操作文件
表现层
业务层


minio依赖
官网提供的工具类:FileUploader.java
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.UploadObjectArgs;
import io.minio.errors.MinioException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
public class FileUploader {
public static void main(String[] args)
throws IOException, NoSuchAlgorithmException, InvalidKeyException {
try {
// Create a minioClient with the MinIO server playground, its access key and secret key.
MinioClient minioClient =
MinioClient.builder()
.endpoint(“https://play.min.io”))
.credentials(“Q3AM3UQ867SPQQA43P2F”, “zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG”)
.build();
// Make ‘asiatrip’ bucket if not exist.
boolean found =
minioClient.bucketExists(BucketExistsArgs.builder().bucket(“asiatrip”).build());
if (!found) {
// Make a new bucket called ‘asiatrip’.
minioClient.makeBucket(MakeBucketArgs.builder().bucket(“asiatrip”).build());
} else {
System.out.println(“Bucket ‘asiatrip’ already exists.”);
}
// Upload ‘/home/user/Photos/asiaphotos.zip’ as object name ‘asiaphotos-2015.zip’ to bucket
// ‘asiatrip’.
minioClient.uploadObject(
UploadObjectArgs.builder()
.bucket(“asiatrip”)
.object(“asiaphotos-2015.zip”)
.filename(“/home/user/Photos/asiaphotos.zip”)
.build());
System.out.println(
“‘/home/user/Photos/asiaphotos.zip’ is successfully uploaded as “
+ “object ‘asiaphotos-2015.zip’ to bucket ‘asiatrip’.”);
} catch (MinioException e) {
System.out.println(“Error occurred: “ + e);
System.out.println(“HTTP trace: “ + e.httpTrace());
}
}
}
编写Service实现层:SysFileService
/
文件上传至Minio
/
@Override
public AjaxResult upload(MultipartFile file) {
InputStream inputStream = null;
//创建Minio的连接对象
MinioClient minioClient = getClient();
String bucketName = minioConfig.getBucketName();
try {
inputStream = file.getInputStream();
//判断文件存储的桶是否存在
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if (!found) {
//如果桶不存在则创建通
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
}
//操作文件
String fileName = file.getOriginalFilename();
String objectName = new SimpleDateFormat(“yyyy/MM/dd/“).format(new Date()) + UUID.randomUUID().toString().replaceAll(“-“, “”)
+ fileName.substring(fileName.lastIndexOf(“.”));
//文件上传
//由于使用的是SpringBoot与之进行集成 上传的时候拿到的是MultipartFile 需要通过输入输出流的方式进行添加
PutObjectArgs objectArgs = PutObjectArgs.builder().object(objectName)
.bucket(bucketName)
.contentType(file.getContentType())
.stream(file.getInputStream(),file.getSize(),-1).build();
minioClient.putObject(objectArgs);
//封装访问的url给前端
AjaxResult ajax = AjaxResult.success();
ajax.put(“fileName”, “/“+bucketName+”/“+objectName);
//url需要进行截取
ajax.put(“url”, minioConfig.getEndpoint()+”:”+ minioConfig.getPort()+”/“+ minioConfig.getBucketName()+”/“+fileName);
return ajax;
}catch(Exception e){
e.printStackTrace();
return AjaxResult.error(“上传失败”);
}finally {
//防止内存泄漏
if (inputStream != null) {
try {
inputStream.close(); // 关闭流
} catch (IOException e) {
log.debug(“inputStream close IOException:” + e.getMessage());
}
}
}
}
/
文件下载
将minio中文件的内容写入到response中
通过InputStream读取数据
通过OutputStream写入到response
@param fileName
@param response
@return
/
@Override
public AjaxResult downloadByMinio(String fileName,HttpServletResponse response) {
try {
//获取文件的访问url
AjaxResult ajax = AjaxResult.success();
ajax.put(“fileName”, fileName);
ajax.put(“url”, minioConfig.getEndpoint()+”:”+ minioConfig.getPort()+”/“+ minioConfig.getBucketName()+”/“+fileName);
//获取Minio连接
MinioClient minioClient = getClient();
//获取文件输入流
InputStream file = minioClient.getObject(GetObjectArgs.builder().bucket(minioConfig.getBucketName()).object(fileName).build());
//设置下载的文件名
response.setHeader(“Content-Disposition”, “attachment;filename=” + fileName);
//由于是
ServletOutputStream servletOutputStream = response.getOutputStream();
int len;
byte[] buffer = new byte[1024];
while ((len = file.read(buffer)) > 0) {
servletOutputStream.write(buffer, 0, len);
}
servletOutputStream.flush();
file.close();
servletOutputStream.close();
return ajax;
}catch (Exception e) {
return AjaxResult.error(e.getMessage());
}
}
/*
获取Minio连接
@return
/
private MinioClient getClient(){
MinioClient minioClient =
MinioClient.builder()
.endpoint(“http://"+minioConfig.getEndpoint()+":"+ minioConfig.getPort())
.credentials(minioConfig.getAccessKey(),minioConfig.getSecretKey())
.build();
return minioClient;
}

