MinIO 是一款基于Go语言发开的高性能、分布式的对象存储系统。客户端支持Java,Net,Python,Javacript, Golang语言。
MinIO 英文官网 中文官网 注:中文官方更新不及时,会有很多坑,最好以英文官网为准
1、Object:存储到minio的基本对象,如图片、文件,字节流,Anything
2、Bucket:用来存储Object的逻辑空间。每个Bucket之间的数据是互相隔离的。对于客户端而言,就相当于存放文件的顶层文件夹。
非常适合于存储大容量非结构化的数据,例如图片、视频、日志文件、备份数据和容器/虚拟机镜像等,而一个对象文件可以是任意大小,从 KB 到最大 TB 不等
<dependency>
<groupId>io.miniogroupId>
<artifactId>minioartifactId>
<version>8.4.3version>
dependency>
application.yml 配置文件 增加minio 地址及用户名密码的配置
minio:
endpoint: http://xx.xx.xx.xx:xxxx
accessKey: xxxx
secretKey: xxxx
java工具类,获取客户端、查询、创建桶、对象等
import io.minio.*;
import io.minio.errors.*;
import io.minio.messages.Item;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
@Slf4j
@Service
public class MinIOUtil {
@Value("${minio.endpoint}")
private String endpoint;
@Value("${minio.accessKey}")
private String accessKey;
@Value("${minio.secretKey}")
private String secretKey;
private MinIOUtil() {
}
private MinioClient getClient() {
return MinioClient.builder()
.endpoint(this.endpoint)
.credentials(this.accessKey, this.secretKey)
.build();
}
public boolean bucketExists(String bucketName) {
try {
return getClient().bucketExists(
BucketExistsArgs.builder()
.bucket(bucketName)
.build());
} catch (Exception ex) {
log.error("bucketExists异常", ex);
return false;
}
}
public boolean makeBucket(String bucketName) {
try {
getClient().makeBucket(
MakeBucketArgs.builder()
.bucket(bucketName)
.build());
} catch (Exception ex) {
log.error("makeBucket异常", ex);
return false;
}
log.debug("makeBucket成功:{}", bucketName);
return true;
}
public boolean mkdir(String bucketName, String dir) {
try {
getClient().putObject(
PutObjectArgs.builder()
.bucket(bucketName)
.object(dir)
.stream(new ByteArrayInputStream(new byte[] {}), 0, -1)
.build());
} catch (Exception ex) {
log.error("mkdir异常", ex);
return false;
}
return true;
}
public boolean putObject(String bucketName, String objName, InputStream inputStream) {
try {
getClient().putObject(
PutObjectArgs.builder()
.bucket(bucketName)
.object(objName)
.stream(inputStream, inputStream.available(), -1)
.build());
} catch (Exception ex) {
log.error("putObject异常,bucketName:{},objName:{}", bucketName, objName, ex);
return false;
}
log.debug("putObject成功,bucketName:{},objName:{}", bucketName, objName);
return true;
}
public Iterable<Result<Item>> listObjects(String bucketName, String prefix) {
return getClient().listObjects(ListObjectsArgs.builder()
.bucket(bucketName)
.prefix(prefix)
.build());
}
public InputStream getObject(String bucketName, String objName) throws ServerException, InsufficientDataException,
ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException,
InvalidResponseException, XmlParserException, InternalException {
return getClient().getObject(
GetObjectArgs.builder()
.bucket(bucketName)
.object(objName)
.build());
}
public boolean removeObject(String bucketName, String objName) {
try {
getClient().removeObject(
RemoveObjectArgs.builder()
.bucket(bucketName)
.object(objName)
.build());
} catch (Exception ex) {
log.error("removeObject异常", ex);
return false;
}
return true;
}
}