perf(OSS): 服务端对象读写与浏览器预签名拆成两张 client
Java 在自己机器上用 https://oss.aishufu.top 读写对象,等于走公网 EIP 回环 (ip route get 自己的公网 IP 是 via 192.168.0.253 dev eth0),实测只有 0.98 MB/s、 TLS 首字节 39ms;同一台走 VPC 内网是 900 MB/s、0.9ms。结果 Excel 出表与输入件下载都吃这条。 不能直接把 endpoint 改成内网地址:预签名 URL 的 host 必须是浏览器可达的反代入口, 否则用户侧直传当场全断。所以新增 aiimage.oss.internal-endpoint(默认空=沿用 endpoint, 行为与今天完全一致),只给服务端 I/O 的 client 用;presign 那两处改用 public-endpoint 的 client, 并把内网地址纳入 managedEndpoints,避免历史数据里出现内网 host 时不归一化。 启动打一行 [oss] 服务端读写端点=…(预签名端点=…),发版后直接回读生效值。
This commit is contained in:
@@ -9,6 +9,11 @@ public class OssProperties {
|
||||
private String region;
|
||||
private String endpoint;
|
||||
private String publicEndpoint;
|
||||
/**
|
||||
* 服务端自己读写对象时用的地址(VPC 内网),为空表示与 {@link #endpoint} 相同。
|
||||
* 拆开是因为 endpoint 必须保持公网域名:预签名 URL 的 host 要等于浏览器能到达的反代入口。
|
||||
*/
|
||||
private String internalEndpoint;
|
||||
private String downloadEndpoint;
|
||||
private String bucket;
|
||||
private String imageVideoBucket;
|
||||
|
||||
+43
-8
@@ -47,7 +47,10 @@ public class OssStorageService {
|
||||
private static final long DEFAULT_READ_MAX_BYTES = 20L * 1024 * 1024;
|
||||
|
||||
private final OssProperties ossProperties;
|
||||
/** 服务端读写对象用的 client:配置了 internal-endpoint 时走 VPC 内网,否则与 endpoint 一致。 */
|
||||
private final MinioClient minioClient;
|
||||
/** 只用于签发浏览器直传预签名 URL:host 必须是公网反代入口,内网地址签出来的 URL 用户侧不可达。 */
|
||||
private final MinioClient presignClient;
|
||||
|
||||
@Autowired
|
||||
public OssStorageService(OssProperties ossProperties) {
|
||||
@@ -56,7 +59,15 @@ public class OssStorageService {
|
||||
|
||||
OssStorageService(OssProperties ossProperties, MinioClient minioClient) {
|
||||
this.ossProperties = ossProperties;
|
||||
this.minioClient = minioClient == null ? createClient(ossProperties) : minioClient;
|
||||
if (minioClient != null) {
|
||||
this.minioClient = minioClient;
|
||||
this.presignClient = minioClient;
|
||||
return;
|
||||
}
|
||||
this.minioClient = createClient(ossProperties, objectIoEndpoint(ossProperties));
|
||||
this.presignClient = objectIoEndpoint(ossProperties).equals(signingEndpoint(ossProperties))
|
||||
? this.minioClient
|
||||
: createClient(ossProperties, signingEndpoint(ossProperties));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,6 +77,8 @@ public class OssStorageService {
|
||||
*/
|
||||
@PostConstruct
|
||||
void checkShopDataBucketConfigured() {
|
||||
log.info("[oss] 服务端读写端点={}(预签名端点={})",
|
||||
withScheme(objectIoEndpoint(ossProperties)), withScheme(signingEndpoint(ossProperties)));
|
||||
String configured = ossProperties.getShopDataBucket();
|
||||
if (configured == null || configured.isBlank()) {
|
||||
log.warn("[oss] aiimage.oss.shop-data-bucket 未配置,店铺数据采集累计档将落默认桶 {};"
|
||||
@@ -133,14 +146,15 @@ public class OssStorageService {
|
||||
|
||||
/**
|
||||
* 为软件版本安装包签发浏览器直传用的预签名 PUT URL(client 桶公开路径)。
|
||||
* 签名 host 与反代入口一致(endpoint 即 public-endpoint oss.aishufu.top),浏览器对返回的 URL 直接 PUT 即可落对象。
|
||||
* 签名 host 取 public-endpoint(oss.aishufu.top),浏览器对返回的 URL 直接 PUT 即可落对象;
|
||||
* 服务端自己的读写走 internal-endpoint 那张 client,两者不能合用一张。
|
||||
*/
|
||||
public String presignSoftwareVersionUpload(String objectKey, int expirySeconds) {
|
||||
if (objectKey == null || !objectKey.startsWith(SOFTWARE_VERSION_PREFIX)) {
|
||||
throw new IllegalArgumentException("software version objectKey must start with " + SOFTWARE_VERSION_PREFIX);
|
||||
}
|
||||
try {
|
||||
return buildClient().getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
|
||||
return presignClient().getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
|
||||
.method(Method.PUT)
|
||||
.bucket(publicClientBucket())
|
||||
.object(objectKey)
|
||||
@@ -197,12 +211,12 @@ public class OssStorageService {
|
||||
|
||||
/**
|
||||
* 为教程安装包签发浏览器直传用的预签名 PUT URL(公开 client 桶 tutorial/ 前缀)。
|
||||
* 签名 host 与反代入口一致,浏览器对返回的 URL 直接 PUT 即可落对象。
|
||||
* 签名 host 同 {@link #presignSoftwareVersionUpload}:必须是公网入口,不能用内网 client。
|
||||
*/
|
||||
public String presignTutorialUpload(String objectKey, int expirySeconds) {
|
||||
requireTutorialKey(objectKey);
|
||||
try {
|
||||
return buildClient().getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
|
||||
return presignClient().getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
|
||||
.method(Method.PUT)
|
||||
.bucket(publicClientBucket())
|
||||
.object(objectKey)
|
||||
@@ -664,7 +678,8 @@ public class OssStorageService {
|
||||
}
|
||||
|
||||
private List<URI> managedEndpoints() {
|
||||
return Stream.of(ossProperties.getEndpoint(), ossProperties.getPublicEndpoint(), LEGACY_MINIO_ENDPOINT)
|
||||
return Stream.of(ossProperties.getEndpoint(), ossProperties.getPublicEndpoint(),
|
||||
ossProperties.getInternalEndpoint(), LEGACY_MINIO_ENDPOINT)
|
||||
.filter(Objects::nonNull)
|
||||
.map(String::trim)
|
||||
.filter(endpoint -> !endpoint.isBlank())
|
||||
@@ -719,9 +734,29 @@ public class OssStorageService {
|
||||
return minioClient;
|
||||
}
|
||||
|
||||
private static MinioClient createClient(OssProperties properties) {
|
||||
private MinioClient presignClient() {
|
||||
return presignClient;
|
||||
}
|
||||
|
||||
/** 服务端读写用的地址:配了内网就走内网,否则维持 endpoint(旧行为)。 */
|
||||
static String objectIoEndpoint(OssProperties properties) {
|
||||
return hasInternalEndpoint(properties) ? properties.getInternalEndpoint().trim() : properties.getEndpoint();
|
||||
}
|
||||
|
||||
/** 预签名 URL 的 host:优先 public-endpoint,与 {@link #publicEndpoint()} 的取值口径一致。 */
|
||||
static String signingEndpoint(OssProperties properties) {
|
||||
String preferred = properties.getPublicEndpoint();
|
||||
return preferred == null || preferred.isBlank() ? properties.getEndpoint() : preferred;
|
||||
}
|
||||
|
||||
private static boolean hasInternalEndpoint(OssProperties properties) {
|
||||
String value = properties.getInternalEndpoint();
|
||||
return value != null && !value.isBlank();
|
||||
}
|
||||
|
||||
private static MinioClient createClient(OssProperties properties, String endpoint) {
|
||||
MinioClient client = MinioClient.builder()
|
||||
.endpoint(withScheme(properties.getEndpoint()))
|
||||
.endpoint(withScheme(endpoint))
|
||||
.credentials(properties.getAccessKeyId(), properties.getAccessKeySecret())
|
||||
.build();
|
||||
// MinIO 8.5.17 默认把 connect/write/read 三项全设成 5 分钟(S3Base.DEFAULT_CONNECTION_TIMEOUT)。
|
||||
|
||||
@@ -111,6 +111,9 @@ aiimage:
|
||||
region: ${AIIMAGE_OSS_REGION:us-east-1}
|
||||
endpoint: ${AIIMAGE_OSS_ENDPOINT:https://oss.aishufu.top}
|
||||
public-endpoint: ${AIIMAGE_OSS_PUBLIC_ENDPOINT:https://oss.aishufu.top}
|
||||
# 服务端读写对象用的内网地址;留空=沿用 endpoint。生产两台都在同一 VPC,公网入口只有 5–8 Mbps
|
||||
# 而内网 ~1 GB/s,且发往自己的 EIP 要出 VPC 网关绕一圈。预签名 URL 仍按 endpoint 签,不受影响。
|
||||
internal-endpoint: ${AIIMAGE_OSS_INTERNAL_ENDPOINT:}
|
||||
download-endpoint: ${AIIMAGE_OSS_DOWNLOAD_ENDPOINT:}
|
||||
bucket: ${AIIMAGE_OSS_BUCKET:nanri-ai-images}
|
||||
image-video-bucket: ${AIIMAGE_IMAGE_VIDEO_OSS_BUCKET:shufu-video}
|
||||
|
||||
Reference in New Issue
Block a user