对象存储服务(Object Storage Service,简称 OSS),以 HTTP RESTful API 的形式对外提供服务,提供的海量、安全、低成本、高可靠的云存储服务,适合存放任意类型的文件。本文介绍基于基于MinIO的文件存储实现。
版本:
- JDK 11
- Spring boot 2.6.9
- io.minio 8.4.1
配置类
@Data
@ConfigurationProperties("alkaid.oss.minio")
public class MinioProperties {
/** 服务器参数 */
private Server server = new Server();
/** 连接参数 */
private Connection connection = new Connection();
/** 请求方法 */
private Method method = new Method();
@Data
public static class Method {
/**
* 单位:秒
*/
private Duration postExpiry = Duration.ofDays(7);
/**
* 单位:秒
*/
private Duration getExpiry = Duration.ofDays(7);
/**
* 单位:秒
*/
private Duration deleteExpiry = Duration.ofDays(7);
}
@Data
public static class Connection {
/**
* Define the connect timeout for the Minio Client.
*/
private Duration connectTimeout = Duration.ofSeconds(10);
/**
* Define the write timeout for the Minio Client.
*/
private Duration writeTimeout = Duration.ofSeconds(60);
/**
* Define the read timeout for the Minio Client.
*/
private Duration readTimeout = Duration.ofSeconds(10);
}
@Data
public static class Server {
/**
* URL for Minio instance. Can include the HTTP scheme. Must include the port.
* If the port is not provided, then the port of the HTTP is taken.
*/
private String endpoint;
private int port;
/**
* Access key (login) on Minio instance
*/
private String accessKey;
/**
* Secret key (password) on Minio instance
*/
private String secretKey;
/**
* If the scheme is not provided in {@code url} property, define if the
* connection is done via HTTP or HTTPS.
*/
private boolean secure = false;
}
}
核心服务
-
GetOptService
/**
* get操作
*
* @author Lucky Yang
* @since 0.0.1
*/
@Slf4j
@Service
public class GetOptService {
@Autowired
private MinioClient client;
@Autowired
private MinioProperties properties;
/**
* 获取对象流
*
* @param bucket
* @param objectId
* @param consumer
* @return
*/
public InputStream getObject(final String bucket, final String objectId, final Consumer<GetObjectArgs.Builder> consumer) {
final String minioId = MinioUtils.minioId(objectId);
try {
final GetObjectArg