docker搭建minio以及与java的整合使用

minio是一个高性能、分布式的对象存储系统,如果不想再多花钱买一个阿里云对象存储,又不想直接将文件上传到项目路径下,强烈推荐使用minio,对服务器要求不高,而且传输速度很快,方便对文件的统一管理,下面的是单机部署方式。

安装运行

查找镜像

sudo docker search minio

拉取镜像

sudo docker pull minio/minio

创建目录

一个用来存放配置,一个用来存储上传文件的目录
启动前需要先创建Minio外部挂载的配置文件( /docker/minio/config),和存储上传文件的目录( /docker/minio/data),注意要给这两个路径足够的权限,否则可能会出错

mkdir -p /docker/minio/config
mkdir -p /docker/minio/data

运行服务

docker run -p 9000:9000 -p 9090:9090 \
     --net=host \
     --name minio \
     -d --restart=always \
     -e "MINIO_ACCESS_KEY=minioadmin" \
     -e "MINIO_SECRET_KEY=minioadmin" \
     -v /home/minio/data:/data \
     -v /home/minio/config:/root/.minio \
     minio/minio server \
     /data --console-address ":9090" -address ":9000"

9090端口指的是minio的客户端端口

MINIO_ACCESS_KEY :账号

MINIO_SECRET_KEY :密码(账号长度必须大于等于5,密码长度必须大于等于8位)

可以通过http://localhost:9000访问minio

java调用minio服务

yml配置文件

#对象存储
minio:
  endPoint: http://ip:9000
  accessKey: minioadmin
  secretKey: minioadmin
  bucketName: bucket

将配置文件中的信息实例化成bean

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "minio")
public class MinIOProperties {
    private String endPoint;
    private String accessKey;
    private String secretKey;
    private String bucketName;
}

使用之前注入的配置对象将minio的客户端实例化

import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MinIOConfig {
    @Autowired
    private MinIOProperties minIOProperties;

    @Bean
    public MinioClient minioClient() {
        return MinioClient.builder()
                .endpoint(minIOProperties.getEndPoint())
                .credentials(minIOProperties.getAccessKey(), minIOProperties.getSecretKey())
                .build();
    }
}

封装minio的常用方法成工具类

import io.minio.*;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Component
public class MinIOUtils {
    @Value("${minio.bucketName}")
    private String bucketName;
    @Autowired
    private MinioClient minioClient;
    public String uploadFile(InputStream inputStream, Long size, String filename) {
        return uploadFile(inputStream,size,bucketName,filename);
    }

    /**
     * 文件上传
     * @param inputStream
     * @param size
     * @param bucketName
     * @param filename
     * @return
     */
    public String uploadFile(InputStream inputStream, Long size,String bucketName, String filename) {
        try {
            ObjectWriteResponse objectWriteResponse = minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(bucketName).object(filename).stream(inputStream, size, -1).build());
            return objectWriteResponse.object();

        } catch (ErrorResponseException | XmlParserException | ServerException | NoSuchAlgorithmException | IOException | InvalidResponseException | InvalidKeyException | InternalException | InsufficientDataException e) {
            e.printStackTrace();
        }
        return "失败";
    }

    /**
     * 文件下载
     * @param objectName
     * @return
     */
    public InputStream downloadInputStream(String objectName) {
        InputStream stream = null;
        try {
            stream = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
        } catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidKeyException | InvalidResponseException | IOException | NoSuchAlgorithmException | ServerException | XmlParserException e) {
            e.printStackTrace();
        }
        return stream;
    }

    /**
     * 删除文件
     * @param objectName
     * @return
     */
    public boolean deleteFile(String objectName){
        try {
            minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
        } catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidKeyException | InvalidResponseException | IOException | NoSuchAlgorithmException | ServerException | XmlParserException e) {
            e.printStackTrace();
        }
        return true;
    }
}

  • 1
    点赞
  • 2
    收藏
  • 打赏
    打赏
  • 0
    评论
<p><img src="https://img-bss.csdnimg.cn/202010270552121581.png" alt="" width="750" height="422" /></p> <div class="line" data-key="374"><strong><span class="prism-token token title important">简介</span></strong></div> <div class="line" data-key="414" data-slate-fragment="JTdCJTIyb2JqZWN0JTIyJTNBJTIyZG9jdW1lbnQlMjIlMkMlMjJkYXRhJTIyJTNBJTdCJTdEJTJDJTIybm9kZXMlMjIlM0ElNUIlN0IlMjJvYmplY3QlMjIlM0ElMjJibG9jayUyMiUyQyUyMnR5cGUlMjIlM0ElMjJsaW5lJTIyJTJDJTIyZGF0YSUyMiUzQSU3QiU3RCUyQyUyMm5vZGVzJTIyJTNBJTVCJTdCJTIyb2JqZWN0JTIyJTNBJTIydGV4dCUyMiUyQyUyMmxlYXZlcyUyMiUzQSU1QiU3QiUyMm9iamVjdCUyMiUzQSUyMmxlYWYlMjIlMkMlMjJ0ZXh0JTIyJTNBJTIyJTIzJTIzJTIwJUU0JUI4JTkzJUU2JUEwJThGJUU3JUFFJTgwJUU0JUJCJThCJUVGJUJDJTg4NjAlRTUlQUQlOTclRUYlQkMlODklMjIlMkMlMjJtYXJrcyUyMiUzQSU1QiU1RCU3RCU1RCU3RCU1RCU3RCUyQyU3QiUyMm9iamVjdCUyMiUzQSUyMmJsb2NrJTIyJTJDJTIydHlwZSUyMiUzQSUyMmxpbmUlMjIlMkMlMjJkYXRhJTIyJTNBJTdCJTdEJTJDJTIybm9kZXMlMjIlM0ElNUIlN0IlMjJvYmplY3QlMjIlM0ElMjJ0ZXh0JTIyJTJDJTIybGVhdmVzJTIyJTNBJTVCJTdCJTIyb2JqZWN0JTIyJTNBJTIybGVhZiUyMiUyQyUyMnRleHQlMjIlM0ElMjJTcHJpbmclMjBCb290JTIwMi54JUU5JTg1JThEJUU3JUJEJUFFJUU0JUJDJTk4JUU1JThDJTk2JUUzJTgwJTgxJUU4JTg3JUFBJUU1JThBJUE4JUU4JUEzJTg1JUU5JTg1JThEJUUzJTgwJTgxJUU2JUE4JUExJUU2JTlEJUJGJUU0JUJEJUJGJUU3JTk0JUE4JUUzJTgwJTgxJUU3JTk0JTlGJUU1JTkxJUJEJUU1JTkxJUE4JUU2JTlDJTlGJUUzJTgwJTgxJUU1JUI1JThDJUU1JTg1JUE1JUU1JUJDJThGJUU1JUFFJUI5JUU1JTk5JUE4JUUzJTgwJTgxJUU1JTg1JUE4JUU1JUIxJTgwJUU1JUJDJTgyJUU1JUI4JUI4JUU1JUE0JTg0JUU3JTkwJTg2JUUzJTgwJTgxJUU1JTg1JUE4JUU1JUIxJTgwJUU2JTk3JUE1JUU1JUJGJTk3JUU3JUFFJUExJUU3JTkwJTg2JUUzJTgwJTgxJUU1JTg4JTg2JUU1JUI4JTgzJUU1JUJDJThGJUU2JTk2JTg3JUU0JUJCJUI2JUU3JUIzJUJCJUU3JUJCJTlGJUU3JUFEJTg5JUU3JTlGJUE1JUU4JUFGJTg2JUU5JTlCJTg2JUU1JTkwJTg4JTIyJTJDJTIybWFya3MlMjIlM0ElNUIlNUQlN0QlNUQlN0QlNUQlN0QlNUQlN0Q="><span data-key="413">Spring Boot 2.x配置优化、自动装配、模板使用、生命周期、嵌入式容器、全局异常处理、全局日志管理、分布式文件系统等知识集合</span></div> <div class="line" data-key="414" data-slate-fragment="JTdCJTIyb2JqZWN0JTIyJTNBJTIyZG9jdW1lbnQlMjIlMkMlMjJkYXRhJTIyJTNBJTdCJTdEJTJDJTIybm9kZXMlMjIlM0ElNUIlN0IlMjJvYmplY3QlMjIlM0ElMjJibG9jayUyMiUyQyUyMnR5cGUlMjIlM0ElMjJsaW5lJTIyJTJDJTIyZGF0YSUyMiUzQSU3QiU3RCUyQyUyMm5vZGVzJTIyJTNBJTVCJTdCJTIyb2JqZWN0JTIyJTNBJTIydGV4dCUyMiUyQyUyMmxlYXZlcyUyMiUzQSU1QiU3QiUyMm9iamVjdCUyMiUzQSUyMmxlYWYlMjIlMkMlMjJ0ZXh0JTIyJTNBJTIyJTIzJTIzJTIwJUU0JUI4JTkzJUU2JUEwJThGJUU3JUFFJTgwJUU0JUJCJThCJUVGJUJDJTg4NjAlRTUlQU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> </div> <div class="line" data-key="431"><span data-key="430"><span data-slate-leaf="true" data-offset-key="430:0"><span class="prism-token token list punctuation"><span data-slate-content="true">*</span></span></span><span data-slate-leaf="true" data-offset-key="430:1"><span data-slate-content="true"> 针对希望对Spring Boot有系统深入学习的初中高级程序员</span></span></span></div> <div class="line" data-key="433"><span data-key="432"><span data-slate-leaf="true" data-offset-key="432:0"><span class="prism-token token list punctuation"><span data-slate-content="true">*</span></span></span><span data-slate-leaf="true" data-offset-key="432:1"><span data-slate-content="true"> 视频专栏:看得懂、学得会、做得出</span></span></span></div> <div class="line" data-key="435"><span data-key="434"><span data-slate-leaf="true" data-offset-key="434:0"><span class="prism-token token list punctuation"><span data-slate-content="true">*</span></span></span><span data-slate-leaf="true" data-offset-key="434:1"><span data-slate-content="true"> 视频中没有废话、全是干货,经过细致备课与后期剪辑制作</span></span></span></div> <div class="line" data-key="437"><span data-key="436"><span data-slate-leaf="true" data-offset-key="436:0"><span class="prism-token token list punctuation"><span data-slate-content="true">*</span></span></span><span data-slate-leaf="true" data-offset-key="436:1"><span data-slate-content="true"> 让您用最少的时间学到最多的知识</span></span></span></div> <div class="line" data-key="439" data-slate-fragment="JTdCJTIyb2JqZWN0JTIyJTNBJTIyZG9jdW1lbnQlMjIlMkMlMjJkYXRhJTIyJTNBJTdCJTdEJTJDJTIybm9kZXMlMjIlM0ElNUIlN0IlMjJvYmplY3QlMjIlM0ElMjJibG9jayUyMiUyQyUyMnR5cGUlMjIlM0ElMjJsaW5lJTIyJTJDJTIyZGF0YSUyMiUzQSU3QiU3RCUyQyUyMm5vZGVzJTIyJTNBJTVCJTdCJTIyb2JqZWN0JTIyJTNBJTIydGV4dCUyMiUyQyUyMmxlYXZlcyUyMiUzQSU1QiU3QiUyMm9iamVjdCUyMiUzQSUyMmxlYWYlMjIlMkMlMjJ0ZXh0JTIyJTNBJTIyKiUyMCUyMCUyMCVFOSU5MiU4OCVFNSVBRiVCOSVFNSVCOCU4QyVFNiU5QyU5QiVFNSVB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JTNBJTVCJTdCJTIyb2JqZWN0JTIyJTNBJTIybGVhZiUyMiUyQyUyMnRleHQlMjIlM0ElMjIqJTIwJTIwJTIwJUU3JTk0JUIxJUU2JUI1JTg1JUU1JTg1JUE1JUU2JUI3JUIxJUVGJUJDJThDJUU2JTk3JUEyJUU1JThDJTg1JUU1JTkwJUFCU3ByaW5nJUU3JTlBJTg0JUU1JTlGJUJBJUU3JUExJTgwJUU2JUIzJUE4JUU4JUE3JUEzJUU3JUFEJTg5JUU1JTg1JUE1JUU5JTk3JUE4JUU3JTlGJUE1JUU4JUFGJTg2JUUzJTgwJTgxJUU0JUI5JTlGJUU0JUJDJTlBJUU1JThDJTg1JUU1JTkwJUFCJUU1JTg4JTg2JUU1JUI4JTgzJUU1JUJDJThGJUU0JUJBJThCJUU1JThBJUExJUU3JTlBJTg0JUU2JUI3JUIxJUU1JTg1JUE1JUU1JTg2JTg1JUU1JUFFJUI5JTIyJTJDJTIybWFya3MlMjIlM0ElNUIlNUQlN0QlNUQlN0QlNUQlN0QlNUQlN0Q="><span data-key="438"><span data-slate-leaf="true" data-offset-key="438:0"><span class="prism-token token list punctuation"><span data-slate-content="true">*</span></span></span><span data-slate-leaf="true" data-offset-key="438:1"><span data-slate-content="true"> 由浅入深,既包含Spring的基础注解等入门知识、也会包含分布式事务的深入内容</span></span></span></div> <p> </p>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:黑客帝国 设计师:我叫白小胖 返回首页
评论

打赏作者

普通码农我姓吴

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值