docker部署minio并使用springboot连接

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

可以提供使用的有例如fastdfs或者minio,这里以minio为例

环境

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

minio安装

docker命令

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

docker部署minio并使用springboot连接_第1张图片

看到此页面即为成功

springboot整合minio

pom.xml


    com.squareup.okhttp3
    okhttp
    4.9.0



    io.minio
    minio
    8.5.6
    
        
            okhttp
            com.squareup.okhttp3
        
    

 配置类

@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();
    }
}

文件上传的工具类

@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);
        }
    }
}

 测试类:

@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

docker exec -it minio bash

进入后,查看信息

mc config host ls

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

mc config host remove 服务名

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

mc config host add 服务名 http://IP:9000 user password --api S3v4

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

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

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

你可能感兴趣的:(springboot,docker,docker,spring,boot,容器)