io.minio.errors.ErrorResponseException: The authorization header is malformed; the region is wrong; expecting ‘us-east-1’
1. 现象
在使用MINIO上传文件到MinServer时候,后端java报错
io.minio.errors.ErrorResponseException: The authorization header is malformed; the region is wrong; expecting 'us-east-1'
2. 环境
- minio-3.0.10
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>3.0.10</version>
</dependency>
- linux
3. 解决
将 minio升级到5.0.4
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>5.0.4</version>
</dependency>
4. 解决历程
4.1 完整errorlog
首先自己调用MINIO的putObject方法,minioClient.putObject(minIOUtil.getBucketName(), zipFileName, inputStream, "application/json");
比较完整err如下:
[qtp852143641-27] com.wx.x.x.x.RestfulCatchAOP 83 apply - operation failed(操作失败)s io.minio.errors.ErrorResponseException: The authorization header is malformed; the region is wrong; expecting 'us-east-1'.
at io.minio.MinioClient.execute(MinioClient.java:1201) ~[minio-3.0.10.jar:3.0.10]
at io.minio.MinioClient.executeGet(MinioClient.java:1294) ~[minio-3.0.10.jar:3.0.10]
at io.minio.MinioClient.listIncompleteUploads(MinioClient.java:4294) ~[minio-3.0.10.jar:3.0.10]
at io.minio.MinioClient.access$300(MinioClient.java:158) ~[minio-3.0.10.jar:3.0.10]
at io.minio.MinioClient$7$1.populate(MinioClient.java:4159) ~[minio-3.0.10.jar:3.0.10]
at io.minio.MinioClient$7$1.hasNext(MinioClient.java:4194) ~[minio-3.0.10.jar:3.0.10]
at io.minio.MinioClient.getLatestIncompleteUploadId(MinioClient.java:4062) ~[minio-3.0.10.jar:3.0.10]
at io.minio.MinioClient.putObject(MinioClient.java:3740) ~[minio-3.0.10.jar:3.0.10]
at io.minio.MinioClient.putObject(MinioClient.java:3383) ~[minio-3.0.10.jar:3.0.10]
at com.x.x.x.TdpServiceImpl.downLoad(TdpServiceImpl.java:142) ~[linkis-metadata-0.9.3.jar:?]
4.2 阅读errorLog
自己的TdpServiceImpl.java:142行 minioClient.putObject(minIOUtil.getBucketName(), zipFileName, inputStream, "application/json")
↓
MinioClient.java:3383 putObject(bucketName, objectName, null, new BufferedInputStream(stream), headerMap);
↓
MinioClient.java:3740 检查是否存在不完整的分段上传 String uploadId = getLatestIncompleteUploadId(bucketName, objectName);
↓
MinioClient.java:4062 返回给定存储桶名称和对象名称的不完整分段上传的最新上传ID。for (Result<Upload> result : listIncompleteUploads(bucketName, objectName, true, false)) {
↓
MinioClient.java:4194 填充populate();
↓
MinioClient.java:4159 this.listMultipartUploadsResult = listIncompleteUploads(bucketName, nextKeyMarker, nextUploadIdMarker,
prefix, delimiter, 1000);
↓
MinioClient.java:158 初始化 public class MinioClient {
↓
MinioClient.java:4294 HttpResponse response = executeGet(bucketName, null, null, queryParamMap);
↓
MinioClient.java:1294 return execute(Method.GET, getRegion(bucketName), bucketName, objectName, headerMap, queryParamMap, null, 0);
↓
MinioClient.java:1201 throw new ErrorResponseException(errorResponse, response);
整体看下来,感觉contentType设置的不对,跟踪源码,发现contentType如果null,系统会自动设置一个。
于是minioClient.putObject(minIOUtil.getBucketName(), zipFileName, inputStream, null)运行后还是同样的错误the region is wrong; expecting 'us-east-1'.
4.3 谷歌
于是整体谷歌 io.minio.errors.ErrorResponseException: The authorization header is malformed; the region is wrong; expecting ‘us-east-1’
找到 https://github.com/minio/minio-java/issues/732 发现缺失是minIO的bug,然后已近修复 https://github.com/minio/minio-java/commit/22c7b122f770cbf4a2cb7d4b491e7d63fa6b5a94
在版本大于5.0.4以上就修复。
修复后源码如下
private static final String US_EAST_1 = "us-east-1";
public void makeBucket(String bucketName, String region)
throws InvalidBucketNameException, RegionConflictException, NoSuchAlgorithmException, InsufficientDataException,
IOException, InvalidKeyException, NoResponseException, XmlPullParserException, ErrorResponseException,
InternalException {
// If region param is not provided, set it with the one provided by constructor
if (region == null) {
region = this.region;
}
// If constructor already sets a region, check if it is equal to region param if provided
if (this.region != null && !this.region.equals(region)) {
throw new RegionConflictException("passed region conflicts with the one previously specified");
}
String configString;
if (region == null || US_EAST_1.equals(region)) {
// for 'us-east-1', location constraint is not required. for more info
// http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region
configString = "";
} else {
CreateBucketConfiguration config = new CreateBucketConfiguration(region);
configString = config.toString();
}
HttpResponse response = executePut(bucketName, null, null, null, US_EAST_1, configString, 0);
response.body().close();
}
升级后果然问题没有再出现;