新增minio存储
This commit is contained in:
parent
303789289e
commit
b3a347d5c1
|
@ -0,0 +1,16 @@
|
|||
package net.maku.storage.properties;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* Minio存储配置项
|
||||
*
|
||||
* @author 阿沐 babamu@126.com
|
||||
*/
|
||||
@Data
|
||||
public class MinioStorageProperties {
|
||||
private String endPoint;
|
||||
private String accessKey;
|
||||
private String secretKey;
|
||||
private String bucketName;
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
package net.maku.storage.service;
|
||||
|
||||
import io.minio.BucketExistsArgs;
|
||||
import io.minio.MakeBucketArgs;
|
||||
import io.minio.MinioClient;
|
||||
import io.minio.PutObjectArgs;
|
||||
import net.maku.framework.common.exception.FastException;
|
||||
import net.maku.storage.properties.StorageProperties;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* Minio存储
|
||||
*
|
||||
* @author 阿沐 babamu@126.com
|
||||
*/
|
||||
public class MinioStorageService extends StorageService {
|
||||
private final MinioClient minioClient;
|
||||
|
||||
public MinioStorageService(StorageProperties properties) {
|
||||
this.properties = properties;
|
||||
|
||||
minioClient = MinioClient.builder().endpoint(properties.getMinio().getEndPoint())
|
||||
.credentials(properties.getMinio().getAccessKey(), properties.getMinio().getSecretKey()).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(byte[] data, String path) {
|
||||
return upload(new ByteArrayInputStream(data), path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(InputStream inputStream, String path) {
|
||||
try {
|
||||
//如果BucketName不存在,则创建
|
||||
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(properties.getMinio().getBucketName()).build());
|
||||
if (!found) {
|
||||
minioClient.makeBucket(MakeBucketArgs.builder().bucket(properties.getMinio().getBucketName()).build());
|
||||
}
|
||||
|
||||
minioClient.putObject(
|
||||
PutObjectArgs.builder()
|
||||
.bucket(properties.getMinio().getBucketName())
|
||||
.object(path)
|
||||
.stream(inputStream, inputStream.available(), -1)
|
||||
.build()
|
||||
);
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new FastException("上传文件失败:", e);
|
||||
}
|
||||
|
||||
return properties.getMinio().getEndPoint() + "/" + properties.getMinio().getBucketName() + "/" + path;
|
||||
}
|
||||
}
|
|
@ -24,14 +24,10 @@ spring:
|
|||
storage:
|
||||
enabled: true
|
||||
config:
|
||||
# 存储类型:local、aliyun
|
||||
# 存储类型:local、aliyun、tencent、qiniu、huawei、minio
|
||||
type: local
|
||||
# 访问域名
|
||||
domain: http://localhost:8080
|
||||
# 配置访问前缀
|
||||
prefix:
|
||||
local:
|
||||
# 本地上传路径
|
||||
path: D://upload
|
||||
|
||||
mybatis-plus:
|
||||
|
|
Loading…
Reference in New Issue
Block a user