我是靠谱客的博主 典雅薯片,最近开发中收集的这篇文章主要介绍SpringBoot整合minioMinioPropertiesMinioUtilsFileTypeUtilsyml配置docker创建minio容器,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

MinioProperties

package com.yujing.user.utils.minio;
import io.minio.MinioClient;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* desc: minio参数
*
* @author: 邢阳
* @mail: xydeveloper@126.com
* @create 2020-06-24 15:33
*/
@Data
@Configuration
@ConfigurationProperties(prefix = "minio")
public class MinioProperties {
@ApiModelProperty(value = "URL,域名,IPv4或者IPv6地址")
private String endpoint;
@ApiModelProperty(value = "端口号")
private Integer port;
@ApiModelProperty(value = "accessKey类似于用户ID,用于唯一标识你的账户")
private String accessKey;
@ApiModelProperty(value = "secretKey是你账户的密码")
private String secretKey;
@ApiModelProperty(value = "如果是true,则用的是https而不是http,默认值是true")
private boolean secure;
@ApiModelProperty(value = "默认存储桶")
private String bucketName;
@ApiModelProperty(value = "图片的最大大小")
private long imageSize;
@ApiModelProperty(value = "其他文件的最大大小")
private long fileSize;
@ApiModelProperty(value = "访问路径超时时间")
private int timeOut;
@ApiModelProperty(value = "非法存储桶名称")
private String illegalBucketName;
/**
* 初始化 MinioClient
*/
@Bean
public MinioClient minioClient() {
return MinioClient.builder().credentials(accessKey, secretKey).endpoint(endpoint, port, secure).build();
}
}

MinioUtils

package com.yujing.user.utils.minio;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.ObjectId;
import cn.hutool.core.text.StrPool;
import cn.hutool.core.util.URLUtil;
import com.yujing.user.common.ExceptionEnum;
import com.yujing.user.common.MyRuntimeException;
import com.yujing.user.entity.vo.MinioFileVo;
import io.minio.*;
import io.minio.http.Method;
import io.minio.messages.DeleteObject;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import lombok.SneakyThrows;
import javax.annotation.PostConstruct;
/**
* desc: minio工具类
*
* @author: 邢阳
* @mail: xydeveloper@126.com
* @create 2020-06-24 15:39
*/
@Component
public class MinioUtils {
@Autowired
private MinioClient minioClient;
@Autowired
public MinioProperties minioProperties;
/**
* 占位符
*/
private static final String BUCKET_PARAM = "${bucket}";
/**
* bucket权限-读写
*/
private static final String READ_WRITE = "{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:GetBucketLocation","s3:ListBucket","s3:ListBucketMultipartUploads"],"Resource":["arn:aws:s3:::" + BUCKET_PARAM + ""]},{"Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:DeleteObject","s3:GetObject","s3:ListMultipartUploadParts","s3:PutObject","s3:AbortMultipartUpload"],"Resource":["arn:aws:s3:::" + BUCKET_PARAM + "/*"]}]}";
/**
* 系统启动创建默认存储桶
*/
@PostConstruct
public void init() {
String bucketName = minioProperties.getBucketName();
if (BooleanUtils.negate(bucketExists(bucketName))) {
makeBucket(bucketName);
}
}
/**
* 检查存储桶是否存在
*
* @param bucketName 存储桶名称
* @return
*/
@SneakyThrows
public boolean bucketExists(String bucketName) {
if (StringUtils.equals(minioProperties.getIllegalBucketName(), bucketName)) {
throw new MyRuntimeException(ExceptionEnum.ILLEGAL_BUCKET_NAME);
}
return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
}
/**
* 创建存储桶
*
* @param bucketName 存储桶名称
*/
@SneakyThrows
public boolean makeBucket(String bucketName) {
if (BooleanUtils.negate(bucketExists(bucketName))) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
minioClient.setBucketPolicy(SetBucketPolicyArgs.builder()
.bucket(bucketName)
.config(READ_WRITE.replace(BUCKET_PARAM, bucketName))
.build());
return Boolean.TRUE;
} else {
return Boolean.FALSE;
}
}
/**
* 列出所有存储桶名称
*
* @return
*/
@SneakyThrows
public List<String> listBucketNames() {
List<String> bucketListName = new ArrayList<>();
listBuckets().forEach(bucket -> bucketListName.add(bucket.name()));
return bucketListName;
}
/**
* 列出所有存储桶
*
* @return
*/
@SneakyThrows
public List<Bucket> listBuckets() {
return minioClient.listBuckets();
}
/**
* 删除存储桶
*
* @param bucketName 存储桶名称
* @return
*/
@SneakyThrows
public boolean removeBucket(String bucketName) {
if (bucketExists(bucketName)) {
Iterable<Result<Item>> myObjects = listObjects(bucketName);
for (Result<Item> result : myObjects) {
Item item = result.get();
// 有对象文件,则删除失败
if (item.size() > 0) {
return Boolean.FALSE;
}
}
// 删除存储桶,注意,只有存储桶为空时才能删除成功。
minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
return BooleanUtils.negate(bucketExists(bucketName));
}
return Boolean.FALSE;
}
/**
* 列出存储桶中的所有对象名称
*
* @param bucketName 存储桶名称
* @return
*/
@SneakyThrows
public List<String> listObjectNames(String bucketName) {
List<String> listObjectNames = new ArrayList<>();
if (bucketExists(bucketName)) {
for (Result<Item> result : listObjects(bucketName)) {
listObjectNames.add(result.get().objectName());
}
} else {
listObjectNames.add(ExceptionEnum.BUCKET_IS_EXIST.getMsg());
}
return listObjectNames;
}
/**
* 列出存储桶中的所有对象
*
* @param bucketName 存储桶名称
* @return
*/
@SneakyThrows
public Iterable<Result<Item>> listObjects(String bucketName) {
return bucketExists(bucketName) ? minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).build()) : null;
}
/**
* 文件上传
*
* @param bucketName
* @param multipartFile
*/
@SneakyThrows
public MinioFileVo upload(String bucketName, MultipartFile multipartFile) {
String objectName = ObjectId.next();
if (BooleanUtils.negate(bucketExists(bucketName))) {
makeBucket(bucketName);
}
minioClient.putObject(PutObjectArgs.builder().bucket(bucketName)
.object(objectName).stream(new ByteArrayInputStream(multipartFile.getBytes()),
-1, minioProperties.getFileSize())
.contentType(FileTypeUtils.getFileType(multipartFile)).build());
MinioFileVo minioFileVo = new MinioFileVo()
.setFileUrl(getObjectUrl(bucketName, objectName))
.setObjectName(StringUtils.join(bucketName, StrPool.DASHED, objectName));
return minioFileVo;
}
/**
* 文件访问路径
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @return
*/
@SneakyThrows
private String getObjectUrl(String bucketName, String objectName) {
return bucketExists(bucketName) ? minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
.method(Method.GET).bucket(bucketName).object(objectName).expiry(minioProperties.getTimeOut(),
TimeUnit.MINUTES).build()) : null;
}
/**
* 文件访问路径
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @return
*/
@SneakyThrows
public String getFileUrl(String bucketName, String objectName) {
String fileUrl = getObjectUrl(bucketName,objectName);
URLUtil.toUrlForHttp(fileUrl).openConnection().getInputStream();
return fileUrl;
}
/**
* 删除一个对象
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
*/
@SneakyThrows
public boolean delete(String bucketName, String objectName) {
Boolean flag = bucketExists(bucketName);
if (flag) {
minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
}
return flag;
}
/**
* 以流的形式获取一个文件对象
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @return
*/
@SneakyThrows
public InputStream getObject(String bucketName, String objectName) {
if (bucketExists(bucketName)) {
StatObjectResponse statObject = statObject(bucketName, objectName);
if (statObject != null && statObject.size() > 0) {
return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
}
}
return null;
}
/**
* 获取对象的元数据
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @return
*/
@SneakyThrows
public StatObjectResponse statObject(String bucketName, String objectName) {
return bucketExists(bucketName) ? minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build()) : null;
}
/**
* 删除指定桶的多个文件对象,返回删除错误的对象列表,全部删除成功,返回空列表
*
* @param bucketName
存储桶名称
* @param objectNames 含有要删除的多个object名称的迭代器对象
* @return
*/
@SneakyThrows
public boolean deleteList(String bucketName, List<String> objectNames) {
if (bucketExists(bucketName)) {
List<DeleteObject> objects = new LinkedList<>();
for (int i = 0; i < objectNames.size(); i++) {
objects.add(new DeleteObject(objectNames.get(i)));
}
if (CollUtil.isNotEmpty(minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(bucketName).objects(objects).build()))) {
return Boolean.FALSE;
}
}
return Boolean.TRUE;
}
/**
* 以流的形式获取一个文件对象(断点下载)
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @param offset
起始字节的位置
* @param length
要读取的长度 (可选,如果无值则代表读到文件结尾)
* @return
*/
@SneakyThrows
public InputStream getObject(String bucketName, String objectName, long offset, Long length) {
if (bucketExists(bucketName)) {
StatObjectResponse statObject = statObject(bucketName, objectName);
if (statObject != null && statObject.size() > 0) {
return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).offset(offset).length(length).build());
}
}
return null;
}
/**
* 通过InputStream上传对象
*
* @param bucketName
存储桶名称
* @param objectName
存储桶里的对象名称
* @param inputStream 要上传的流
* @param contentType 要上传的文件类型 MimeTypeUtils.IMAGE_JPEG_VALUE
* @return
*/
@SneakyThrows
public boolean upload(String bucketName, String objectName, InputStream inputStream, String contentType) {
if (bucketExists(bucketName)) {
minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(inputStream, -1, minioProperties.getFileSize()).contentType(contentType).build());
StatObjectResponse statObject = statObject(bucketName, objectName);
if (statObject != null && statObject.size() > 0) {
return true;
}
}
return Boolean.FALSE;
}
}

FileTypeUtils

package com.yujing.user.utils.minio;
import cn.hutool.core.io.FileTypeUtil;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
/**
* desc: 文件格式工具类
*
* @author: 邢阳
* @mail: xydeveloper@126.com
* @create 2018-07-25 22:26
*/
public class FileTypeUtils {
private final static String IMAGE_TYPE = "image/";
private final static String AUDIO_TYPE = "audio/";
private final static String VIDEO_TYPE = "video/";
private final static String APPLICATION_TYPE = "application/";
private final static String TXT_TYPE = "text/";
public static String getFileType(MultipartFile multipartFile) {
try {
String type = FileTypeUtil.getType(multipartFile.getInputStream());
if (type.equalsIgnoreCase("JPG") || type.equalsIgnoreCase("JPEG") || type.equalsIgnoreCase("GIF") || type.equalsIgnoreCase("PNG") || type.equalsIgnoreCase("BMP") || type.equalsIgnoreCase("PCX") || type.equalsIgnoreCase("TGA") || type.equalsIgnoreCase("PSD") || type.equalsIgnoreCase("TIFF")) {
return IMAGE_TYPE + type;
}
if (type.equalsIgnoreCase("mp3") || type.equalsIgnoreCase("OGG") || type.equalsIgnoreCase("WAV") || type.equalsIgnoreCase("REAL") || type.equalsIgnoreCase("APE") || type.equalsIgnoreCase("MODULE") || type.equalsIgnoreCase("MIDI") || type.equalsIgnoreCase("VQF") || type.equalsIgnoreCase("CD")) {
return AUDIO_TYPE + type;
}
if (type.equalsIgnoreCase("mp4") || type.equalsIgnoreCase("avi") || type.equalsIgnoreCase("MPEG-1") || type.equalsIgnoreCase("RM") || type.equalsIgnoreCase("ASF") || type.equalsIgnoreCase("WMV") || type.equalsIgnoreCase("qlv") || type.equalsIgnoreCase("MPEG-2") || type.equalsIgnoreCase("MPEG4") || type.equalsIgnoreCase("mov") || type.equalsIgnoreCase("3gp")) {
return VIDEO_TYPE + type;
}
if (type.equalsIgnoreCase("doc") || type.equalsIgnoreCase("docx") || type.equalsIgnoreCase("ppt") || type.equalsIgnoreCase("pptx") || type.equalsIgnoreCase("xls") || type.equalsIgnoreCase("xlsx") || type.equalsIgnoreCase("zip") || type.equalsIgnoreCase("jar")) {
return APPLICATION_TYPE + type;
}
if (type.equalsIgnoreCase("txt")) {
return TXT_TYPE + type;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}

yml配置

#minio配置
minio:
endpoint: http://10.168.2.110:9002/
port: 9002
access-key: minio
secret-key: minio@123
secure: false
bucket-name: yujing
image-size: 10485760
file-size: 1073741824
time-out: 2
illegal-bucket-name: minio

docker创建minio容器

docker run -p 9002:9000 --name minio
-d --restart=always 
--privileged=true 
-e TZ="Asia/Shanghai" 
-e MINIO_ROOT_USER=minio 
-e MINIO_ROOT_PASSWORD=minio@123 
-v /usr/local/develop/minio//data:/data 
-v /usr/local/develop/minio/config:/root/.minio 
minio/minio:RELEASE.2021-06-17T00-10-46Z server /data

最后

以上就是典雅薯片为你收集整理的SpringBoot整合minioMinioPropertiesMinioUtilsFileTypeUtilsyml配置docker创建minio容器的全部内容,希望文章能够帮你解决SpringBoot整合minioMinioPropertiesMinioUtilsFileTypeUtilsyml配置docker创建minio容器所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部