plus.easydo.starter.file.minio.MinioTemplate Maven / Gradle / Ivy
The newest version!
package plus.easydo.starter.file.minio;
import io.minio.MinioClient;
import io.minio.ObjectStat;
import io.minio.Result;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.InvalidArgumentException;
import io.minio.errors.InvalidBucketNameException;
import io.minio.errors.InvalidResponseException;
import io.minio.errors.NoResponseException;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;
import org.xmlpull.v1.XmlPullParserException;
import plus.easydo.starter.file.minio.config.MinioProperties;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
/**
* @author laoyu
* @version 1.0
*/
@Data
@Component
@RequiredArgsConstructor
@EnableConfigurationProperties(MinioProperties.class)
public class MinioTemplate {
@Autowired
private MinioClient client;
@Autowired
private MinioProperties properties;
public String getDefaultBucket(){
return properties.getDefaultBucket();
}
/**
* 检查文件存储桶是否存在
*
* @param bucketName bucketName
* @return boolean
* @author laoyu
*/
@SneakyThrows
public boolean bucketExists(String bucketName){
return client.bucketExists(bucketName);
}
/**
* 创建bucket
*
* @param bucketName bucketName
* @author laoyu
*/
@SneakyThrows
public void createBucket(String bucketName) {
if (!bucketExists(bucketName)) {
client.makeBucket(bucketName);
}
}
/**
* 获取全部bucket
*
* @return java.util.List
* @author laoyu
* https://docs.minio.io/cn/java-client-api-reference.html#listBuckets
*/
@SneakyThrows
public List getAllBuckets() {
return client.listBuckets();
}
/**
* 根据bucketName获取信息
*
* @param bucketName bucket名称
* @return java.util.Optional
* @author laoyu
*/
@SneakyThrows
public Optional getBucket(String bucketName) {
return client.listBuckets().stream().filter(b -> b.name().equals(bucketName)).findFirst();
}
/**
* 根据bucketName删除信息
* @param bucketName bucket名称
*/
@SneakyThrows
public void removeBucket(String bucketName) {
client.removeBucket(bucketName);
}
/**
* 根据文件前缀查询文件
*
* @param bucketName bucket名称
* @param prefix 前缀
* @param recursive 是否递归查询
* @return MinioItem 列表
*/
@SneakyThrows
public List getAllObjectsByPrefix(String bucketName, String prefix, boolean recursive) {
List objectList = new ArrayList<>();
Iterable> objectsIterator = client.listObjects(bucketName, prefix, recursive);
for (Result- result : objectsIterator) {
objectList.add(new MinioItem(result.get()));
}
return objectList;
}
/**
* 获取文件外链
*
* @param bucketName bucket名称
* @param objectName 文件名称
* @param expires 过期时间 小于等于7
* @return url
*/
@SneakyThrows
public String getObjectURL(String bucketName, String objectName, Integer expires) {
return client.presignedGetObject(bucketName, objectName, expires);
}
/**
* 获取文件外链
*
* @param bucketName bucket名称
* @param objectName 文件名称
* @return url
*/
@SneakyThrows
public String getObjectURL(String bucketName, String objectName) {
return client.presignedGetObject(bucketName, objectName);
}
/**
* 获取文件
*
* @param bucketName bucket名称
* @param objectName 文件名称
* @return 二进制流
*/
@SneakyThrows
public InputStream getObject(String bucketName, String objectName) {
return client.getObject(bucketName, objectName);
}
/**
* 上传文件
*
* @param bucketName bucket名称
* @param objectName 文件名称
* @param stream 文件流
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#putObject
*/
public void putObject(String bucketName, String objectName, InputStream stream) throws Exception {
client.putObject(bucketName,objectName,stream,null, null, null, "application/octet-stream");
}
/**
* 上传文件
*
* @param bucketName bucket名称
* @param objectName 文件名称
* @param stream 文件流
* @param size 大小
* @param contextType 类型
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#putObject
*/
public void putObject(String bucketName, String objectName, InputStream stream, long size, String contextType) throws Exception {
client.putObject(bucketName,objectName,stream,size, null, null, contextType);
}
/**
* 获取文件信息
*
* @param bucketName bucket名称
* @param objectName 文件名称
* @return io.minio.ObjectStat
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#statObject
*/
public ObjectStat getObjectInfo(String bucketName, String objectName) throws Exception {
return client.statObject(bucketName, objectName);
}
/**
* 删除文件
*
* @param bucketName bucket名称
* @param objectName 文件名称
* https://docs.minio.io/cn/java-client-api-reference.html#removeObject
* @throws IOException IOException
* @throws InvalidKeyException InvalidKeyException
* @throws InsufficientDataException InsufficientDataException
* @throws InternalException InternalException
* @throws NoResponseException NoResponseException
* @throws XmlPullParserException XmlPullParserException
* @throws ErrorResponseException ErrorResponseException
* @throws NoSuchAlgorithmException NoSuchAlgorithmException
* @throws InvalidArgumentException InvalidArgumentException
* @throws InvalidResponseException InvalidResponseException
* @throws InvalidBucketNameException InvalidBucketNameException
*/
public void removeObject(String bucketName, String objectName) throws IOException, InvalidKeyException, InsufficientDataException, InternalException, NoResponseException, InvalidBucketNameException, XmlPullParserException, ErrorResponseException, InvalidArgumentException, NoSuchAlgorithmException, InvalidResponseException {
client.removeObject(bucketName, objectName);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy