docker部署minio并使用springboot连接

需求:工作中,在微信小程序播放时,返回文件流并不能有效的使用,前端需要一个可以访问的地址,springboot默认是有资源拦截器的,但是不适合生产环境的使用
可以提供使用的有例如fastdfs或者minio,这里以minio为例

环境

|--------|-----------------------------------------------------------------------------------|
| 软件 | 版本 |
| docker | 24.0.4 |
| minio | RELEASE.2023-10-24T05-18-28Z (commit-id=97cc12fdc539361cf175ffc2f00480eec0836d82) |

minio安装

docker命令

bash 复制代码
docker run -d \
-p 9000:9000 \
-p 9001:9001 \
--name minio 
--restart=always 
--privileged=true \
-v /home/minio/data:/data \
-e "MINIO_ROOT_USER=user" \
-e "MINIO_ROOT_PASSWORD=password" \
minio/minio server /data 
--console-address ":9001"

开启linux防火墙

centos开启防火墙

打开浏览器访问 ip:9001

看到此页面即为成功

springboot整合minio

pom.xml

XML 复制代码
<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.9.0</version>
</dependency>

<dependency>
    <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
    <version>8.5.6</version>
    <exclusions>
        <exclusion>
            <artifactId>okhttp</artifactId>
            <groupId>com.squareup.okhttp3</groupId>
        </exclusion>
    </exclusions>
</dependency>

配置类

java 复制代码
@Configuration
public class MinioConfig {

    @Value("${minio.endpoint}")
    private String endpoint;

    @Value("${minio.accessKey}")
    private String accessKey;

    @Value("${minio.secretKey}")
    private String secretKey;

    @Bean
    public MinioClient minioClient(){
        return
                MinioClient.builder()
                        .endpoint(endpoint)
                        .credentials(accessKey, secretKey)
                        .build();
    }
}

文件上传的工具类

java 复制代码
@Slf4j
public class MinioUtils {

    public static String uploadFile(MinioClient minioClient, InputStream inputStream, String bucket, String filename) {
        try {
            boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket("public").build());
            if (!found) {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket("public").build());
            }

            ObjectWriteResponse response = minioClient.putObject(
                    PutObjectArgs
                            .builder()
                            .bucket(bucket)
                            .object(filename)
                            .stream(inputStream, inputStream.available(), -1)
                            .contentType(InferStatusConstant.WAV_CONTENT_TYPE)
                            .build()
            );

            String url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
                            .bucket(bucket)
                            .expiry(7 * 24 * 60 * 60)
                            .object(filename)
                            .method(Method.GET)
                            .build());
            log.info("分享地址:" + url);
            return url;
        } catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidKeyException |
                 InvalidResponseException | IOException | NoSuchAlgorithmException | ServerException |
                 XmlParserException e) {
            throw new RuntimeException(e);
        }
    }
}

测试类:

java 复制代码
@Test
public void uploadFileToMinio() {
    try (FileInputStream stream = new FileInputStream("/path/to/file")) {
        String url = MinioUtils.uploadFile(minioClient, stream, "test", "/test/test1.wav");
        System.out.println(url);
    } catch (Exception e) {

    }
}

遇到的一些问题

运行springboot的测试类没有上传,debug之后显示s3 api requests must be made to api port.

解决方案:

进入docker

bash 复制代码
docker exec -it minio bash

进入后,查看信息

bash 复制代码
mc config host ls

找到自己的服务,我的为localhost,查看下方的url等信息均不对,移除当前服务

bash 复制代码
mc config host remove 服务名

添加新的服务,注意url信息,注意端口

bash 复制代码
mc config host add 服务名 http://IP:9000 user password --api S3v4

不需要重启,重新运行测试代码,发现运行成功

在使用的过程中生成分享连接为127.0.0.1/XXXXXXX

解决方案同上,修改自己的ip

相关推荐
❀͜͡傀儡师9 分钟前
Docker 安装部署 OceanBase
docker·容器·oceanbase
ifanatic31 分钟前
[每周一更]-(第154期):Docker 底层深度剖析:掌控 CPU 与内存资源的艺术
docker
创码小奇客1 小时前
Spring Boot 集成 Talos:打造智能调参系统,让模型性能自动飙升
java·spring boot·trae
潘多编程1 小时前
AWS上部署Spring Boot应用的完整指南
spring boot·云计算·aws
Full Stack Developme3 小时前
PostgreSQL dblink 与 Spring Boot @Transactional 的事务整合
数据库·spring boot·postgresql
David爱编程4 小时前
Cilium 与 Calico 网络安全能力横向评测
云原生·容器·kubernetes
David爱编程4 小时前
Kubernetes中使用Calico实现零信任网络访问控制
云原生·容器·kubernetes
草堂春睡足4 小时前
【数据迁移】Windows11 下将 Ubuntu 从 C 盘迁移到 D 盘
linux·windows·ubuntu·docker
程序猿小郑5 小时前
Docker环境离线安装指南
docker
热心市民梁先生6 小时前
oect刷入arm系统安装docker
运维·docker·容器