aa72ced8 by 蔡永松

Minio服务集成

1 parent 6b9b1868
......@@ -366,7 +366,7 @@
</profiles>
<properties>
<oracle.version>11.2.0.4</oracle.version>
<oracle.version>11.2.0.3</oracle.version>
<geotools.version>24-SNAPSHOT</geotools.version>
<org.springframework.version>5.1.8.RELEASE</org.springframework.version>
<org.spring.boot.version>2.1.8.RELEASE</org.spring.boot.version>
......
package com.pashanhoo.common.minio;
import io.minio.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Date;
/**
* @author CAIYONGSONG
* @commpany www.pashanhoo.com
* @date 2022/7/20
*/
@Component
public class FileServerOptUtil {
private Logger log = LoggerFactory.getLogger(FileServerOptUtil.class);
@Autowired
MinioClient minioClient;
/**
* 获取桶中某个对象的输入流
* @param bucketName
* @param path
* @return
*/
public InputStream getObjectInputStream(String bucketName, String path){
log.info("从桶:" + bucketName + ",获取:" + path + "对象流!");
InputStream stream = null;
try {
stream = minioClient.getObject(
GetObjectArgs.builder().bucket(bucketName).object(path).build());
} catch (Exception e) {
log.error("从桶:" + bucketName + ",获取:" + path + "对象流错误!");
e.printStackTrace();
}
return stream;
}
/**
*
* @param bucketName
* @param dirPath 如:path/to/
* @return
*/
public boolean mkdir(String bucketName,String dirPath){
log.info("在桶:" + bucketName + ",中创建:" + dirPath);
boolean result = false;
try {
minioClient.putObject(
PutObjectArgs.builder().bucket(bucketName).object(dirPath).stream(
new ByteArrayInputStream(new byte[] {}), 0, -1)
.build());
result = true;
} catch (Exception e) {
log.error("在桶:" + bucketName + ",中创建:" + dirPath + "错误!");
e.printStackTrace();
}
return result;
}
/**
* 往对象存储服务目标桶的目标位置上传文件
* @param bucketName 桶
* @param toPath 目标位置如:test/2.txt
* @param fromPath 本地文件位置
* @return
*/
public boolean uploadObject(String bucketName,String toPath,String fromPath ){
log.info("从本地:" + fromPath + "往桶:" + bucketName + ",中上传文件:" + toPath);
boolean result = false;
try {
minioClient.uploadObject(
UploadObjectArgs.builder()
.bucket(bucketName)
.object(toPath)
.filename(fromPath)
.build());
result = true;
} catch (Exception e) {
log.error("从本地:" + fromPath + "往桶:" + bucketName + ",中上传文件:" + toPath + "错误!");
e.printStackTrace();
}
return result;
}
/**
* 从桶中下载文件到本地文件
* @param bucketName
* @param fromPath
* @param toPath
* @return
*/
public boolean downloadFile(String bucketName,String fromPath,String toPath){
log.info("从桶:" + bucketName + "中将" + fromPath + "文件下载到:" + toPath);
boolean result = false;
try {
minioClient.downloadObject(
DownloadObjectArgs.builder()
.bucket(bucketName)
.object(fromPath)
.filename(toPath)
.build());
result = true;
} catch (Exception e) {
log.error("从桶:" + bucketName + "中将" + fromPath + "文件下载到:" + toPath + "错误!");
e.printStackTrace();
}
return result;
}
public static String extractFilename(MultipartFile file){
if (file.isEmpty()) {
throw new RuntimeException("文件不存在!");
}
return DateUtils.dateToStr(new Date()) + "/" + System.currentTimeMillis() + "_" + file.getOriginalFilename() ;
}
}
\ No newline at end of file
package com.pashanhoo.common.minio;
import io.minio.MinioClient;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
/**
* @author CAIYONGSONG
* @commpany www.pashanhoo.com
* @date 2022/7/20
*/
@Data
@Component
@ConfigurationProperties(prefix = "minio")
public class MinioConfig {
private Logger log = LoggerFactory.getLogger(MinioConfig.class);
// minio地址
private String minioUrl;
//minio账号
private String accessKey;
//minio密码
private String secretKey;
//存储桶名称 */
public String bucketName;
// "如果是true,则用的是https而不是http,默认值是true"
public static Boolean secure = false;
@Bean
public MinioClient getMinioClient(){
log.info("初始化MinioClient客户端:minioUrl:" + minioUrl + ",accessKey:" + accessKey + ",secretKey:" + secretKey);
MinioClient minioClient = MinioClient.builder()
.endpoint(minioUrl)
.credentials(accessKey,secretKey)
.build();
return minioClient;
}
}
package com.pashanhoo.common.minio;
import io.minio.*;
import io.minio.messages.DeleteError;
import io.minio.messages.DeleteObject;
import io.minio.messages.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.FastByteArrayOutputStream;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author CAIYONGSONG
* @commpany www.pashanhoo.com
* @date 2022/7/20
*/
@Component
public class MinioUtil {
@Autowired
private MinioClient minioClient;
/**
* 查看存储bucket是否存在
*
* @param bucketName 存储bucket
* @return boolean
*/
public Boolean bucketExists(String bucketName) {
Boolean found;
try {
found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
} catch (Exception e) {
e.printStackTrace();
return false;
}
return found;
}
/**
* 创建存储bucket
*
* @param bucketName 存储bucket名称
* @return Boolean
*/
public Boolean makeBucket(String bucketName) {
try {
minioClient.makeBucket(MakeBucketArgs.builder()
.bucket(bucketName)
.build());
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 删除存储bucket
*
* @param bucketName 存储bucket名称
* @return Boolean
*/
public Boolean removeBucket(String bucketName) {
try {
minioClient.removeBucket(RemoveBucketArgs.builder()
.bucket(bucketName)
.build());
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 文件上传
*
* @param file 文件
* @param bucketName 存储bucket
* @return Boolean
*/
public Boolean upload(MultipartFile file, String fileName, String bucketName) {
try {
PutObjectArgs objectArgs = PutObjectArgs.builder().bucket(bucketName).object(fileName)
.stream(file.getInputStream(), file.getSize(), -1).contentType(file.getContentType()).build();
//文件名称相同会覆盖
minioClient.putObject(objectArgs);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 文件下载
*
* @param bucketName 存储bucket名称
* @param fileName 文件名称
* @param res response
* @return Boolean
*/
public void download(String bucketName, String fileName, HttpServletResponse res) {
GetObjectArgs objectArgs = GetObjectArgs.builder().bucket(bucketName)
.object(fileName).build();
try (GetObjectResponse response = minioClient.getObject(objectArgs)) {
byte[] buf = new byte[1024];
int len;
try (FastByteArrayOutputStream os = new FastByteArrayOutputStream()) {
while ((len = response.read(buf)) != -1) {
os.write(buf, 0, len);
}
os.flush();
byte[] bytes = os.toByteArray();
res.setCharacterEncoding("utf-8");
//设置强制下载不打开
res.setContentType("application/force-download");
res.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
try (ServletOutputStream stream = res.getOutputStream()) {
stream.write(bytes);
stream.flush();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 查看文件对象
*
* @param bucketName 存储bucket名称
* @return 存储bucket内文件对象信息
*/
public List<ObjectItem> listObjects(String bucketName) {
Iterable<Result<Item>> results = minioClient.listObjects(
ListObjectsArgs.builder().bucket(bucketName).build());
List<ObjectItem> objectItems = new ArrayList<>();
try {
for (Result<Item> result : results) {
Item item = result.get();
ObjectItem objectItem = new ObjectItem();
objectItem.setObjectName(item.objectName());
objectItem.setSize(item.size());
objectItems.add(objectItem);
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return objectItems;
}
/**
* 批量删除文件对象
*
* @param bucketName 存储bucket名称
* @param objects 对象名称集合
*/
public Iterable<Result<DeleteError>> removeObjects(String bucketName, List<String> objects) {
List<DeleteObject> dos = objects.stream().map(e -> new DeleteObject(e)).collect(Collectors.toList());
Iterable<Result<DeleteError>> results = minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(bucketName).objects(dos).build());
return results;
}
}
package com.pashanhoo.common.minio;
import io.swagger.annotations.ApiModel;
/**
* @author CAIYONGSONG
* @commpany www.pashanhoo.com
* @date 2022/7/20
*/
@ApiModel("用户实体类")
public class ObjectItem {
long size;
String objectName;
public long getSize() {
return size;
}
public void setSize(long size) {
this.size = size;
}
public String getObjectName() {
return objectName;
}
public void setObjectName(String objectName) {
this.objectName = objectName;
}
}
package com.pashanhoo.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @author CAIYONGSONG
* @commpany www.pashanhoo.com
* @date 2022/8/18
*/
@Configuration
@Slf4j
public class TestTemplateBeanConfig {
@Bean
RestTemplate restTemplate(){
log.info("完成初始化RestTemplate得bean");
return new RestTemplate();
}
}
package com.pashanhoo.qys.controller;
import com.pashanhoo.common.Result;
import com.pashanhoo.qys.service.SysFileService;
import io.minio.GetObjectArgs;
import io.minio.ListObjectsArgs;
import io.minio.MinioClient;
import io.minio.messages.Item;
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.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
/**
* @author CAIYONGSONG
* @commpany www.pashanhoo.com
* @date 2022/7/20
*/
@Api(tags = "Minio文件上传")
@RestController
@RequestMapping("/system/file")
public class SysFileController {
@Autowired
private SysFileService sysFileService;
@Autowired
private MinioClient minioClient;
/**
* 图片上传minio
*
* @param file 图片文件
* @return 返回
*/
@ApiOperation("点选文件-文件上传")
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public Result uploadFileMinio(MultipartFile file) {
String url = sysFileService.uploadFileMinio(file);
if (!StringUtils.isEmpty(url)) {
return Result.ok(url);
}
return Result.error(-1,"上传失败");
}
@PostMapping(value = "/upload-files" )
@ApiOperation(value = "文件上传(支持批量)", notes = "文件上传(支持批量)")
public Result uploadFiles(@RequestPart MultipartFile[] files ) throws IOException {
String url = sysFileService.uploadFileMinio(files);
if (!StringUtils.isEmpty(url)) {
return Result.ok(url);
}
return Result.error(-1,"上传失败");
}
// 暂未实现
@ApiOperation("文件下载")
@RequestMapping(value = "/download", method = RequestMethod.POST)
public String download(HttpServletResponse response) {
try {
Iterable<io.minio.Result<Item>> results = minioClient.listObjects(
ListObjectsArgs.builder().bucket("eci-bucket").build()//获取bucket里所有文件信息
);
String fileName = null;
for (io.minio.Result<Item> result : results) {
Item item = result.get();
fileName = item.objectName();
System.out.println(item.lastModified() + "\t" + item.size() + "\t" + item.objectName());
}
InputStream object = minioClient.getObject(GetObjectArgs.builder().bucket("eci-bucket").object(fileName).build());
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
IOUtils.copy(object, bos);
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
bos.flush();
return "success";
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
}
}
\ No newline at end of file
package com.pashanhoo.qys.service;
import org.springframework.web.multipart.MultipartFile;
/**
* @author CAIYONGSONG
* @commpany www.pashanhoo.com
* @date 2022/7/20
*/
public interface SysFileService {
public String uploadFileMinio(MultipartFile file);
public String uploadFileMinio(MultipartFile[] file);
}
package com.pashanhoo.qys.service.impl;
import com.pashanhoo.common.minio.FileServerOptUtil;
import com.pashanhoo.common.minio.MinioConfig;
import com.pashanhoo.common.minio.MinioUtil;
import com.pashanhoo.qys.service.SysFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
/**
* @author CAIYONGSONG
* @commpany www.pashanhoo.com
* @date 2022/7/20
*/
@Service
@Slf4j
public class SysFileServiceImpl implements SysFileService {
@Autowired
private MinioConfig minioConfig;
@Autowired
private MinioUtil minioUtil;
@Override
public String uploadFileMinio(MultipartFile[] files){
Long st = System.currentTimeMillis();
log.info("开始上传批量文件" + files.length);
// 判断存储桶是否存在
if (!minioUtil.bucketExists(minioConfig.getBucketName())) {
minioUtil.makeBucket(minioConfig.getBucketName());
}
StringBuilder sbf = new StringBuilder();
for (MultipartFile file : files) {
if (file.isEmpty()) {
throw new RuntimeException("文件不存在!");
}
log.info("循环开始上传单个文件,文件大小:" + file.getSize());
// 生成文件名 按照要求-带文件夹路径的文件的全路径
String fineName = FileServerOptUtil.extractFilename(file);
try {
// 上传文件
minioUtil.upload(file, fineName, minioConfig.getBucketName());
} catch (Exception e) {
System.out.println("上传minio服务器期出错了 " + e.getMessage());
e.printStackTrace();
return null;
}
sbf.append(minioConfig.getMinioUrl()).append("/").append( minioConfig.getBucketName())
.append("/").append(fineName).append(",");
}
//String url = minioConfig.getMinioUrl() + "/" + minioConfig.getBucketName() + "/" + fineName;
String resUrl = sbf.toString().substring(0,(sbf.length()-1));
log.info("上传文件结束,总耗时:"+(System.currentTimeMillis() - st) +"s, 文件路径:" + resUrl );
return resUrl;
}
@Override
public String uploadFileMinio(MultipartFile file) {
Long st = System.currentTimeMillis();
if (file.isEmpty()) {
throw new RuntimeException("文件不存在!");
}
log.info("开始上传单个文件,文件大小:" + (file.getSize()/1024) + "KB");
// 判断存储桶是否存在
if (!minioUtil.bucketExists(minioConfig.getBucketName())) {
minioUtil.makeBucket(minioConfig.getBucketName());
}
// 生成文件名 按照要求-带文件夹路径的文件的全路径
String fineName = FileServerOptUtil.extractFilename(file);
try {
// 上传文件
minioUtil.upload(file, fineName, minioConfig.getBucketName());
} catch (Exception e) {
System.out.println("上传minio服务器期出错了 " + e.getMessage());
e.printStackTrace();
return null;
}
String url = minioConfig.getMinioUrl() + "/" + minioConfig.getBucketName() + "/" + fineName;
log.info("上传文件结束,总耗时:"+(System.currentTimeMillis() - st) +"ms, 文件路径:" + url );
return url;
}
}
\ No newline at end of file
......@@ -7,8 +7,8 @@ server:
spring:
servlet:
multipart:
maxFileSize: 10MB
maxRequestSize: 10MB
maxFileSize: 100MB
maxRequestSize: 500MB
application:
name: hzbdcsyn
jackson:
......@@ -71,5 +71,16 @@ management:
logging:
config: "classpath:logback-spring.xml"
# Minio配置
minio:
# minio配置的地址,端口9000
minioUrl: http://172.16.56.30:90
# 账号
accessKey: minioadmin
# 密码
secretKey: minioadmin
# MinIO桶名字
bucketName: ecibucket
swagger2:
enable: false
......
......@@ -7,8 +7,8 @@ server:
spring:
servlet:
multipart:
maxFileSize: 10MB
maxRequestSize: 10MB
maxFileSize: 100MB
maxRequestSize: 500MB
application:
name: hzbdcsyn
jackson:
......@@ -71,5 +71,16 @@ management:
logging:
config: "classpath:logback-spring.xml"
# Minio配置
minio:
# minio配置的地址,端口9000
minioUrl: http://172.16.56.30:90
# 账号
accessKey: minioadmin
# 密码
secretKey: minioadmin
# MinIO桶名字
bucketName: ecibucket
swagger2:
enable: true
......
spring:
profiles:
active: dev
active: test
......