Java MINIO使用踩坑&解决

本文档介绍了在使用Minio时遇到的XML解析错误、不兼容的OkHttp版本以及SSL异常的问题及其解决方案。针对XMLStreamException,原因是文档格式不正确,解决方法是确保Minio依赖与示例代码版本匹配,并正确配置端口。对于OkHttp版本问题,需要在pom文件中引入合适版本的OkHttp依赖。最后,SSLException的解决办法是去掉URL的's',使用HTTP而非HTTPS连接Minio服务器。

1.javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,3]
Message: 文档中根元素前面的标记必须格式正确。

javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,3]
Message: 文档中根元素前面的标记必须格式正确。
	at io.minio.Xml.unmarshal(Xml.java:55)
	at io.minio.S3Base.getRegion(S3Base.java:698)
	at io.minio.S3Base.execute(S3Base.java:470)
	at io.minio.S3Base.executeHead(S3Base.java:728)
	at io.minio.MinioClient.bucketExists(MinioClient.java:1135)
	at com.glorypan.console.utils.FileUploader.main(FileUploader.java:25)
Error occurred: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,3]
Message: 文档中根元素前面的标记必须格式正确。
HTTP trace: null

 解决:

minio的官方文档有中英文两版,中文版的示例版本是7.0.2,英文版是8.3.0,如果是用docker直接pull的话,是最新版latest,pom文件引入依赖的时候要注意对应。

<dependency>
    <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
    <version>8.3.0</version>
</dependency>

并且 运行端口9000是api的端口9001是网页版的端口。

docker run -p 9000:9000 -p 9001:9001 --name minio -v D:\data:/data -e "MINIO_ROOT_USER=user" -e "MINIO_ROOT_PASSWORD=password" minio/minio server /data --console-address ":9001"

即创建minioclient时要用9000,而http://localhost:9001/用于网页访问控制台。

MinioClient minioClient = MinioClient.builder()
                    .endpoint("http://localhost:9000/")
                    .credentials("user", "password")
                    .build();

2. Unsupported OkHttp library found. Must use okhttp >= 4.8.1

Exception in thread "main" java.lang.ExceptionInInitializerError
	at com.glorypan.console.utils.FileUploader.main(FileUploader.java:18)
Caused by: java.lang.RuntimeException: Unsupported OkHttp library found. Must use okhttp >= 4.8.1
	at io.minio.S3Base.<clinit>(S3Base.java:107)
	... 1 more
Caused by: java.lang.NoSuchMethodError: okhttp3.RequestBody.create([BLokhttp3/MediaType;)Lokhttp3/RequestBody;
	at io.minio.S3Base.<clinit>(S3Base.java:105)
	... 1 more

 解决:

minio依赖于okhttp,所以pom文件还需要引入一下okhttp。

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.9.0</version>
</dependency>

3.Exception in thread "main" javax.net.ssl.SSLException: Unsupported or unrecognized SSL message

Exception in thread "main" javax.net.ssl.SSLException: Unsupported or unrecognized SSL message
	at java.base/sun.security.ssl.SSLSocketInputRecord.handleUnknownRecord(SSLSocketInputRecord.java:451)
	at java.base/sun.security.ssl.SSLSocketInputRecord.decode(SSLSocketInputRecord.java:175)
	at java.base/sun.security.ssl.SSLTransport.decode(SSLTransport.java:110)
	at java.base/sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1369)
	at java.base/sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1278)
	at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:401)
	at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:373)
	at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.kt:379)
	at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.kt:337)
	at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:209)
	at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:226)
	at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:106)
	at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:74)
	at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:255)
	at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201)
	at okhttp3.internal.connection.RealCall.execute(RealCall.kt:154)
	at io.minio.S3Base.execute(S3Base.java:532)
	at io.minio.S3Base.getRegion(S3Base.java:694)
	at io.minio.S3Base.execute(S3Base.java:470)
	at io.minio.S3Base.executeHead(S3Base.java:728)
	at io.minio.MinioClient.bucketExists(MinioClient.java:1135)
	at com.glorypan.console.utils.FileUploader.main(FileUploader.java:25)
	Suppressed: javax.net.ssl.SSLException: Unsupported or unrecognized SSL message
		... 30 more

解决:http别加s就行! 

MinioClient minioClient = MinioClient.builder()
                    .endpoint("http://localhost:9000/")
                    .credentials("user", "password")
                    .build();

minio guide:minio使用手册

Java使用MinIO前,需要先引入依赖: ```xml &lt;dependency&gt; &lt;groupId&gt;io.minio&lt;/groupId&gt; &lt;artifactId&gt;minio&lt;/artifactId&gt; &lt;version&gt;3.0.10&lt;/version&gt; &lt;/dependency&gt; ``` 此依赖可确保在Java项目中使用MinIO相关功能[^2]。 以下是一个简单的Java使用MinIO的示例代码,该示例包含创建Minio客户端、创建存储桶、上传文件等基本操作: ```java import io.minio.MinioClient; import io.minio.errors.MinioException; import java.io.IOException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; public class MinioJavaExample { public static void main(String[] args) { try { // 创建Minio客户端 MinioClient minioClient = new MinioClient(&quot;http://localhost:9000&quot;, &quot;minioadmin&quot;, &quot;miniostorage&quot;); // 检查存储桶是否存在 boolean isExist = minioClient.bucketExists(&quot;my-bucket&quot;); if (!isExist) { // 创建存储桶 minioClient.makeBucket(&quot;my-bucket&quot;); } // 上传文件 minioClient.putObject(&quot;my-bucket&quot;, &quot;test.txt&quot;, &quot;/path/to/local/file/test.txt&quot;); System.out.println(&quot;文件上传成功&quot;); } catch (MinioException | IOException | NoSuchAlgorithmException | InvalidKeyException e) { e.printStackTrace(); } } } ``` 在上述代码中,首先创建了一个Minio客户端,需要传入MinIO服务的地址、访问密钥和秘密密钥。然后检查指定的存储桶是否存在,如果不存在则创建该存储桶。最后,将本地的一个文件上传到指定存储桶中。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值