【OSS对象存储】- 基于MinIO的文件存储

对象存储服务(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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值