minio 整合springboot
2023-12-21 06:17:17
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yiyi</groupId>
<artifactId>yiyi-miio</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<relativePath />
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.5.2</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.12</version>
</dependency>
</dependencies>
</project>
MinioConfig文件
package com.yiyi.config;
import io.minio.MinioClient;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Author:zrf
* @Date:2023/12/20 17:38
* @description:minio上传工具类
*/
@Data
@Configuration
@ConfigurationProperties(prefix = "minio")
public class MinioConfig {
/**
* 服务地址
*/
private String endpoint;
/**
* 访问key
*/
private String accessKey;
/**
* 密钥
*/
private String secretKey;
/**
* 桶名称
*/
private String bucketName;
@Bean
public MinioClient minioClient() {
return MinioClient.builder().endpoint(endpoint).credentials(accessKey, secretKey).build();
}
}
appliaction.yml 配置
server:
port: 8089
minio:
endpoint: http://127.0.0.1:9000
bucketName: dev
accessKey: minioadmin
secretKey: minioadmin
spring:
application:
name: yiyi-minio
servlet:
multipart:
max-file-size: 100MB
max-request-size: 100MB
mvc:
pathmatch:
matching-strategy: ant_path_matcher
MinioUtils 工具类
package com.yiyi.util;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.yiyi.config.MinioConfig;
import io.minio.*;
import io.minio.http.Method;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* @Author:zrf
* @Date:2023/12/19 17:38
* @description:minio上传工具类
*/
@Component
@Slf4j
public class MinioUtils {
@Resource
private MinioConfig minioConfig;
@Resource
private MinioClient minioClient;
/**
* 上传文件
* @param file
* @return
*/
public String upload(MultipartFile file) {
String fileName = file.getOriginalFilename();
try {
fileName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"))+"-"
+ UUID.randomUUID().toString().replaceAll("-","")
+ fileName.substring(fileName.lastIndexOf("."));
log.info("上传文件-文件名:{}", fileName);
minioClient.putObject(PutObjectArgs.builder()
.bucket(minioConfig.getBucketName())
.object(fileName)
.stream(file.getInputStream(), file.getSize(), -1)
.contentType(file.getContentType())
.build());
} catch (Exception e) {
log.info("上传文件-失败:{}", e.getMessage());
e.printStackTrace();
return "上传文件失败";
}
String url = minioConfig.getEndpoint()+"/"+minioConfig.getBucketName()+"/"+fileName;
log.info("上传文件-成功-url:{}", url);
return url;
}
/**
* 预览文件
* @param fileName
* @return
*/
public String preview(String fileName) {
if (StrUtil.isEmpty(fileName)) return "fileName is not null";
try {
return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
.bucket(minioConfig.getBucketName())
.object(fileName)
.method(Method.GET)
.build());
}catch (Exception e){
log.info("预览文件-失败: {}",e.getMessage());
e.printStackTrace();
return "预览文件-失败";
}
}
/**
* 下载
* @param fileName
* @param res
*/
public void download(String fileName, HttpServletResponse res) {
if (StrUtil.isEmpty(fileName)) return;
ByteArrayOutputStream out = null;
try {
GetObjectResponse objectResponse = minioClient.getObject(GetObjectArgs.builder()
.bucket(minioConfig.getBucketName())
.object(fileName)
.build());
out = new ByteArrayOutputStream();
IoUtil.copy(objectResponse,out);
res.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
res.setContentLength(out.toByteArray().length);
res.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
try(ServletOutputStream stream = res.getOutputStream()){
stream.write(out.toByteArray());
stream.flush();
}
}catch (Exception e){
log.info("下载文件-失败: {}",e.getMessage());
e.printStackTrace();
}finally {
if (out != null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 检查存储桶是否存在
*
* @param bucketName
* @return
* @throws Exception
*/
public boolean bucketExists(String bucketName) throws Exception {
return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
}
/**
* 创建存储桶
*
* @param bucketName
* @return
* @throws Exception
*/
public boolean makeBucket(String bucketName) throws Exception {
boolean flag = bucketExists(bucketName);
if (!flag) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
log.info("minio存储桶创建成功:{}",bucketName);
return true;
} else {
return false;
}
}
}
文章来源:https://blog.csdn.net/weixin_42903592/article/details/135119832
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!