一、前言
最近在项目中用的使用minio来上传文件,因此在这里给告诉大家如何实现
二、实现文件上传至minio
1.添加相关的maven依赖
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.4.3</version>
<exclusions>
<exclusion>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.10.0</version>
</dependency>
2.minio连接配置
(1)首先配置minio属性
import com.smartcitysz.dp.upload.constants.PlatformEnum;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "minio")
public class MinioProperties {
private String accessKey;
private String secretKey;
private String endpoint;
private String bucket;
/**
* 访问域名
*/
private String domain = "";
/**
* 启用存储
*/
private Boolean enableStorage = true;
/**
* 存储平台
*/
private PlatformEnum platform = PlatformEnum.MINIO;
/**
* 基础路径
*/
private String basePath = "";
}
(2)接着配置minio连接,代码如下:
import com.smartcitysz.dp.minio.MinioService;
import com.smartcitysz.dp.minio.utils.MinioUtils;
import io.minio.MinioClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(MinioProperties.class )
public class MinioConfiguration {
@Bean
@ConditionalOnBean(MinioProperties.class)
public MinioClient minioClient(MinioProperties minioProperties) {
return MinioClient.builder()
.endpoint(minioProperties.getEndpoint())
.credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
.build();
}
@Bean
@ConditionalOnBean(MinioClient.class)
public MinioUtils minioUtils(MinioClient minioClient) {
return new MinioUtils(minioClient);
}
@Bean
@ConditionalOnBean(value = {MinioClient.class, MinioUtils.class})
public MinioService minionService(MinioProperties minioProperties, MinioUtils minioUtils) {
return new MinioService(minioProperties, minioUtils);
}
}
(3)在YAML添加minio配置
minio:
endpoint: http://xxxxxx:1222
accessKey: xxxxxxxx
secretKey: xxxxxxxxxxx
bucket: my-bucket
3.定义minio工具类,代码如下:
@Slf4j
public class MinioUtils {
private MinioClient minioClient;
public MinioUtils(MinioClient minioClient) {
this.minioClient = minioClient;
}
public MinioUtils() {
}
/**
* 初始化桶
* @throws Exception 抛出异常
*/
public void createBucket(String bucketName)throws Exception{
if (!bucketExists(bucketName)) {
/* 制作桶参数 */
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
}
}
/**
* 验证桶名是否存在
* @param bucketName 桶名
* @return 返回信息
* @throws Exception 抛出异常
*/
public boolean bucketExists(String bucketName)throws Exception {
return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
}
/**
* 文件上传
* @param bucketName 桶名
* @param objectName 路径 + 文件名 + 后缀
* @param inputStream 流对象
* @param contentType 文件类型
* @return 对象写入响应
* @throws Exception 抛出异常
*/
public ObjectWriteResponse uploadFile(String bucketName,String objectName,InputStream inputStream, String contentType) throws Exception{
return minioClient.putObject(
PutObjectArgs.builder().bucket(bucketName).object(objectName).contentType(contentType).stream(inputStream,inputStream.available(), -1).build());
}
/**
* 上传本地文件
* @param bucketName 桶名
* @param objectName 路径 + 文件名 + 后缀
* @param fileName 文件的路径
* @param contentType 文件类型
* @return 对象写入响应
* @throws Exception 抛出异常
*/
public ObjectWriteResponse uploadFile(String bucketName,String objectName,String fileName, String contentType) throws Exception{
return minioClient.uploadObject(
UploadObjectArgs.builder().bucket(bucketName).object(objectName).filename(fileName).contentType(contentType).build());
}
/**
* 通过流上传文件
* @param bucketName 桶名
* @param objectName 路径 + 文件名 + 后缀
* @param inputStream 流对象
* @return 对象写入响应
* @throws Exception 抛出异常
*/
public ObjectWriteResponse uploadFile(String bucketName,String objectName,InputStream inputStream) throws Exception{
return minioClient.putObject(
PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(inputStream,inputStream.available(),-1).build());
}
4.编写实现文件上传的Service文章来源:https://www.toymoban.com/news/detail-706688.html
public class MinioService {
private MinioProperties minioProperties;
private MinioUtils minioUtils;
public MinioService(MinioProperties minioProperties, MinioUtils minioUtils) {
this.minioProperties = minioProperties;
this.minioUtils = minioUtils;
}
public MinioService() {
}
/**
* @param file 使用MultipartFile进行文件上传
* @return 返回的是带有后缀的唯一id 如b0adb5d9b2f741e2b3907e330fd1825e.jpg b0adb5d9b2f741e2b3907e330fd18251.txt
*/
public String uploadFile(MultipartFile file) {
try {
String bucketName = minioProperties.getBucket();
if (!StringUtils.hasLength(bucketName)) {
throw new RuntimeException("bucketName is empty");
}
String fileName = file.getOriginalFilename();
minioUtils.createBucket(bucketName);
String objectName = UUID.randomUUID().toString().replaceAll("-", "")
+ fileName.substring(fileName.lastIndexOf("."));
minioUtils.uploadFile(bucketName, objectName, file);
return objectName;
} catch (Exception e) {
log.error("uploadFile error ,msg is {}", e);
return null;
}
}
/**
* @param file MultipartFile文件对象
* @return 返回的是不带有后缀的唯一id 如b0adb5d9b2f741e2b3907e330fd1825e b0adb5d9b2f741e2b3907e330fd18251
*/
public String uploadFileNoSuffix(MultipartFile file) {
try {
String bucketName = minioProperties.getBucket();
if (!StringUtils.hasLength(bucketName)) {
throw new RuntimeException("bucketName is empty");
}
minioUtils.createBucket(bucketName);
String objectName = UUID.randomUUID().toString().replaceAll("-", "");
minioUtils.uploadFile(bucketName, objectName, file);
return objectName;
} catch (Exception e) {
log.error("uploadFileNoSuffix error ,msg is {}", e);
return null;
}
}
/**
* @param file 使用MultipartFile进行文件上传
* @return 返回的是带有后缀的唯一id 如b0adb5d9b2f741e2b3907e330fd1825e.jpg b0adb5d9b2f741e2b3907e330fd18251.txt
*/
public String uploadFile(File file) {
try {
InputStream inputStream = new FileInputStream(file);
return uploadFile(inputStream);
} catch (Exception e) {
log.error("uploadFile error ,msg is {}", e);
return null;
}
}
/**
* 通过流上传文件
*
* @param inputStream 文件流
* @return 返回的是不带有后缀的唯一id 如b0adb5d9b2f741e2b3907e330fd1825e b0adb5d9b2f741e2b3907e330fd18251
*/
public String uploadFile(InputStream inputStream) throws Exception {
try {
String bucketName = minioProperties.getBucket();
if (!StringUtils.hasLength(bucketName)) {
throw new RuntimeException("bucketName is empty");
}
String objectName = UUID.randomUUID().toString().replaceAll("-", "");
minioUtils.uploadFile(bucketName, objectName, inputStream);
return objectName;
} catch (Exception e) {
log.error("uploadFile error ,msg is {}", e);
return null;
}
}
/**
* 通过流上传文件
*
* @param inputStream 文件流
* @return 返回的是不带有后缀的唯一id 如b0adb5d9b2f741e2b3907e330fd1825e b0adb5d9b2f741e2b3907e330fd18251
*/
public String uploadFile(InputStream inputStream) throws Exception {
try {
String bucketName = minioProperties.getBucket();
if (!StringUtils.hasLength(bucketName)) {
throw new RuntimeException("bucketName is empty");
}
String objectName = UUID.randomUUID().toString().replaceAll("-", "");
minioUtils.uploadFile(bucketName, objectName, inputStream);
return objectName;
} catch (Exception e) {
log.error("uploadFile error ,msg is {}", e);
return null;
}
}
}
4.最后在controller层调用下就可以了。
大家可以自己尝试下文章来源地址https://www.toymoban.com/news/detail-706688.html
到了这里,关于springboot实现文件上传到minio的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!