🐛 外网预签名固定 MinIO region,避免 GetBucketLocation 解析到 HTML

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-10-13 22:36:14 +08:00
parent 7c7fe2e1c1
commit 64b4ad99fc
2 changed files with 13 additions and 1 deletions
@@ -65,6 +65,8 @@ import java.util.UUID;
public class MinioFileServiceImpl extends AbstractFileService {
private static final long PART_SIZE = 10 * 1024 * 1024L;
/** MinIO 默认 region;固定后预签名无需再请求外网域名做 GetBucketLocation */
private static final String DEFAULT_REGION = "us-east-1";
private final FileStorageProperties.StoreProperties properties;
private final MinioClient minioClient;
@@ -82,6 +84,7 @@ public class MinioFileServiceImpl extends AbstractFileService {
.method(Method.PUT)
.bucket(properties.getBucketName())
.object(partObjectKey(objectKey, uploadId, partNumber))
.region(DEFAULT_REGION)
.expiry(Math.toIntExact(resolveExpires(expires)))
.build());
return applyTransformPathPrefix(presignedUrl);
@@ -186,6 +189,7 @@ public class MinioFileServiceImpl extends AbstractFileService {
.method(Method.GET)
.bucket(properties.getBucketName())
.object(resolveObjectKey(objectKey))
.region(DEFAULT_REGION)
.expiry(Math.toIntExact(resolveExpires(expires)))
.build());
return applyTransformPathPrefix(presignedUrl);
@@ -212,7 +216,10 @@ public class MinioFileServiceImpl extends AbstractFileService {
/**
* 浏览器侧预签名优先使用外网地址,避免 HTTPS 页面访问 HTTP MinIO 被 Mixed Content 拦截。
* <p>
* MinIO SDK 不允许 endpoint 带 path,因此外网地址中的 /oss 等前缀在签名后单独拼回。
* 外网域名通常是业务网关而非直连 MinIO,必须固定 region,否则 SDK 会请求 GetBucketLocation
* 并拿到 HTML 页面导致 XmlParserException。
*/
private MinioClient resolvePresignClient() {
String transformEndpoint = properties.getTransformEndpoint();
@@ -228,6 +235,7 @@ public class MinioFileServiceImpl extends AbstractFileService {
transformPresignClient = MinioClient.builder()
.endpoint(resolveTransformOrigin(transformEndpoint))
.credentials(properties.getAccessKeyId(), properties.getAccessKeySecret())
.region(DEFAULT_REGION)
.build();
}
return transformPresignClient;