docker部署minio并使用springboot连接的操作方法
作者:[奸笑]这个不是斜眼笑[奸笑]
这篇文章主要介绍了docker部署minio并使用springboot连接的操作方法,本文以minio为例结合实例代码给大家详细讲解,需要的朋友可以参考下
需求:工作中,在微信小程序播放时,返回文件流并不能有效的使用,前端需要一个可以访问的地址,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开启防火墙
开启端口
sudo firewall-cmd --zone=public --add-port=6379/tcp --permanent
重启防火墙
sudo firewall-cmd --reload
打开浏览器访问 ip:9001

看到此页面即为成功
springboot整合minio
pom.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>配置类
@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
到此这篇关于docker部署minio并使用springboot连接的文章就介绍到这了,更多相关docker部署minio内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
