🐛 外网预签名固定 MinIO region,避免 GetBucketLocation 解析到 HTML
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+8
@@ -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;
|
||||
|
||||
+5
-1
@@ -1501,7 +1501,9 @@ public class VoucherManageServiceImpl extends BaseServiceImpl<VoucherManageMappe
|
||||
private String buildVoucherFileUrl(Long fileId, String objectKey) {
|
||||
try {
|
||||
String presignedUrl = resolvePresignClient().getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
|
||||
.method(Method.GET).bucket(minioBucketName).object(objectKey).expiry(1, java.util.concurrent.TimeUnit.HOURS).build());
|
||||
.method(Method.GET).bucket(minioBucketName).object(objectKey)
|
||||
.region("us-east-1")
|
||||
.expiry(1, java.util.concurrent.TimeUnit.HOURS).build());
|
||||
return applyTransformPathPrefix(presignedUrl);
|
||||
} catch (Exception exception) {
|
||||
log.warn("生成凭证文件预览地址失败 voucherFileId={}, objectKey={}", fileId, objectKey, exception);
|
||||
@@ -1511,6 +1513,7 @@ public class VoucherManageServiceImpl extends BaseServiceImpl<VoucherManageMappe
|
||||
|
||||
/**
|
||||
* 浏览器预签名优先使用外网地址,避免 HTTPS 页面访问 HTTP MinIO 被 Mixed Content 拦截。
|
||||
* 外网域名通常是业务网关,必须固定 region,否则 SDK 会请求 GetBucketLocation 并解析到 HTML。
|
||||
*/
|
||||
private MinioClient resolvePresignClient() {
|
||||
if (Func.isEmpty(minioTransformEndpoint)) {
|
||||
@@ -1525,6 +1528,7 @@ public class VoucherManageServiceImpl extends BaseServiceImpl<VoucherManageMappe
|
||||
transformPresignClient = MinioClient.builder()
|
||||
.endpoint(org.springblade.transport.config.VoucherMinioConfig.resolveClientEndpoint(minioTransformEndpoint))
|
||||
.credentials(minioAccessKey, minioSecretKey)
|
||||
.region("us-east-1")
|
||||
.build();
|
||||
}
|
||||
return transformPresignClient;
|
||||
|
||||
Reference in New Issue
Block a user