一、快速开始
Minlo说明:
Minio是Apcche旗下的一款开源的轻量级文件服务器,基于对象存储,协议是基于Apache License v2.0,开源可用于商务。 Minio主要用来存储非结构化的数据,类似文件,图片,照片,日志文件,各类备份文件等,按照官网描述,文件的大小从几KB到5TB。 Minio提供了非常方便,友好的界面,并且文档也是非常丰富,具体可以参考它的文档:https://docs.min.io/cn/ Minlo作用类似于FastDFS和阿里云的OSS进行对象存储 安装minlo服务器参考:https://blog.csdn.net/u010189683/article/details/108171562
1、添加依赖
< dependency> < groupId> io.minio</ groupId> < artifactId> minio</ artifactId> < version> 7.0.2</ version>
</ dependency>
< dependency> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-configuration-processor</ artifactId> < optional> true</ optional>
</ dependency>
2、添加配置项
minio : endpoint : http: //192.168.179.131: 9000/accessKey : adminsecretKey : admin123456
3、配置项和工具类
@Data
@Configuration
@ConfigurationProperties ( prefix = "minio" )
public class MinioConfig { private String endpoint; private String accessKey; private String secretKey; @Bean public MinioClient getMinioClient ( ) throws InvalidEndpointException , InvalidPortException { MinioClient minioClient = new MinioClient ( endpoint, accessKey, secretKey) ; return minioClient; } }
@Component
public class MinioUtil { @Value ( "${minio.endpoint}" ) private String endpoint; @Value ( "${minio.accessKey}" ) private String accessKey; @Value ( "${minio.secretKey}" ) private String secretKey; private static final int DEFAULT_EXPIRY_TIME = 7 * 24 * 3600 ; private static MinioClient minioClient; @Autowired @SneakyThrows public MinioClient setMinioClient ( ) { MinioClient minioClient = new MinioClient ( endpoint, accessKey, secretKey) ; return minioClient; } @SneakyThrows public boolean bucketExists ( String bucketName) { boolean flag = minioClient. bucketExists ( bucketName) ; return flag ? true : false ; } @SneakyThrows public boolean makeBucket ( String bucketName) { boolean flag = bucketExists ( bucketName) ; if ( flag) return false ; minioClient. makeBucket ( bucketName) ; return true ; } @SneakyThrows public List < String > listBucketNames ( ) { List < Bucket > bucketList = listBuckets ( ) ; return bucketList. stream ( ) . map ( item-> bucket. name ( ) ) . collect ( Collector . toList ( ) ) } @SneakyThrows public List < Bucket > listBuckets ( ) { return minioClient. listBuckets ( ) ; } @SneakyThrows public boolean removeBucket ( String bucketName) { boolean flag = bucketExists ( bucketName) ; if ( flag) return false ; Iterable < Result < Item > > myObjects = listObjects ( bucketName) ; for ( Result < Item > result : myObjects) { Item item = result. get ( ) ; if ( item. size ( ) > 0 ) { return false ; } } minioClient. removeBucket ( bucketName) ; flag = bucketExists ( bucketName) ; if ( ! flag) { return true ; } } @SneakyThrows public List < String > listObjectNames ( String bucketName) { List < String > listObjectNames = new ArrayList < > ( ) ; boolean flag = bucketExists ( bucketName) ; if ( flag) { Iterable < Result < Item > > myObjects = listObjects ( bucketName) ; for ( Result < Item > result : myObjects) { Item item = result. get ( ) ; listObjectNames. add ( item. objectName ( ) ) ; } } return listObjectNames; } @SneakyThrows public Iterable < Result < Item > > listObjects ( String bucketName) { boolean flag = bucketExists ( bucketName) ; return flag? minioClient. listObjects ( bucketName) : null ; } @SneakyThrows public boolean putObject ( String bucketName, String objectName, String fileName) { boolean flag = bucketExists ( bucketName) ; if ( flag) { minioClient. putObject ( bucketName, objectName, fileName, null ) ; ObjectStat statObject = statObject ( bucketName, objectName) ; if ( statObject != null && statObject. length ( ) > 0 ) { return true ; } } return false ; } @SneakyThrows public boolean putObject ( String bucketName, String objectName, InputStream stream) { boolean flag = bucketExists ( bucketName) ; if ( flag) { minioClient. putObject ( bucketName, objectName, stream, new PutObjectOptions ( stream. available ( ) , - 1 ) ) ; ObjectStat statObject = statObject ( bucketName, objectName) ; if ( statObject != null && statObject. length ( ) > 0 ) { return true ; } } return false ; } @SneakyThrows public InputStream getObject ( String bucketName, String objectName) { boolean flag = bucketExists ( bucketName) ; if ( flag) { ObjectStat statObject = statObject ( bucketName, objectName) ; if ( statObject != null && statObject. length ( ) > 0 ) { InputStream stream = minioClient. getObject ( bucketName, objectName) ; return stream; } } return null ; } @SneakyThrows public InputStream getObject ( String bucketName, String objectName, long offset, Long length) { boolean flag = bucketExists ( bucketName) ; if ( flag) { ObjectStat statObject = statObject ( bucketName, objectName) ; if ( statObject != null && statObject. length ( ) > 0 ) { InputStream stream = minioClient. getObject ( bucketName, objectName, offset, length) ; return stream; } } return null ; } @SneakyThrows public boolean getObject ( String bucketName, String objectName, String fileName) { boolean flag = bucketExists ( bucketName) ; if ( flag) { ObjectStat statObject = statObject ( bucketName, objectName) ; if ( statObject != null && statObject. length ( ) > 0 ) { minioClient. getObject ( bucketName, objectName, fileName) ; return true ; } } return false ; } @SneakyThrows public boolean removeObject ( String bucketName, String objectName) { boolean flag = bucketExists ( bucketName) ; if ( flag) { minioClient. removeObject ( bucketName, objectName) ; return true ; } return false ; } @SneakyThrows public List < String > removeObject ( String bucketName, List < String > objectNames) { List < String > deleteErrorNames = new ArrayList < > ( ) ; boolean flag = bucketExists ( bucketName) ; if ( flag) { Iterable < Result < DeleteError > > results = minioClient. removeObjects ( bucketName, objectNames) ; for ( Result < DeleteError > result : results) { DeleteError error = result. get ( ) ; deleteErrorNames. add ( error. objectName ( ) ) ; } } return deleteErrorNames; } @SneakyThrows public String presignedGetObject ( String bucketName, String objectName, Integer expires) { boolean flag = bucketExists ( bucketName) ; return bucketExists ( bucketName) ? minioClient. presignedGetObject ( bucketName, objectName, expires) : null ; } @SneakyThrows public String presignedPutObject ( String bucketName, String objectName, Integer expires) { return bucketExists ( bucketName) ? minioClient. presignedPutObject ( bucketName, objectName, expires) : null ; } @SneakyThrows public ObjectStat statObject ( String bucketName, String objectName) { return bucketExists ( bucketName) ? minioClient. statObject ( bucketName, objectName) : null ; } @SneakyThrows public String getObjectUrl ( String bucketName, String objectName) { return bucketExists ( bucketName) ? minioClient. getObjectUrl ( bucketName, objectName) : "" ; }
}