1. Quick start
Minlo description:
Minio is an open source lightweight file server under Apcche, based on object storage, the protocol is based on Apache License v2.0, open source can be used for business.
Minio is mainly used to store unstructured data, such as files, pictures, photos, log files, various backup files, etc. According to the official website description, the file size ranges from a few KB to 5TB.
Minio provides a very convenient and friendly interface, and the documentation is also very rich. For details, please refer to its documentation: https://docs.min.io/cn/
Minlo is similar to FastDFS and Alibaba Cloud's OSS for object storage
Install minlo server reference: https://blog.csdn.net/u010189683/article/details/108171562
1. Add dependencies
< 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. Add configuration items
minio :
endpoint : http: //192.168.179.131: 9000/
accessKey : admin
secretKey : admin123456
3. Configuration items and tools
@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) : "" ;
}
}