feat(internal-audio): 内部音频上传口收口落桶与出链,外部工作流免持对象存储凭据
主机 A 事件隔离期间 oss.aishufu.top:443 对公网不可达,外部平台自签 SigV4 直连
MinIO 的链路当场失效;那把自签凭据又正是生产 root,随微信文件明文外流。
新增 POST /api/internal/audio(X-Internal-Token,只收 audio/*、50MB 上限),
服务端走 VPC 内网落桶 upload/audio/{uuid}/{filename},出 24h 预签名链并把 host
换成 download.aishufu.top——反代按 Host: oss.aishufu.top 回源,SigV4 只对收到的
host 验签,故签名不变、主机 A 无需对外开放公网入口。
This commit is contained in:
+64
@@ -0,0 +1,64 @@
|
||||
package com.nanri.aiimage.modules.file.controller;
|
||||
|
||||
import com.nanri.aiimage.common.api.ApiResponse;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.security.AdminAuthSupport;
|
||||
import com.nanri.aiimage.modules.file.model.vo.InternalAudioUploadVo;
|
||||
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 内部音频上传(工作流/服务间调用):X-Internal-Token 鉴权,原始音频字节直传。
|
||||
* 调用方不持有任何对象存储凭据,落桶与预签名下载直链都收口在服务端。
|
||||
*/
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
@RequestMapping("/api/internal/audio")
|
||||
@Tag(name = "内部接口", description = "服务间调用(X-Internal-Token 鉴权)。")
|
||||
public class InternalAudioUploadController {
|
||||
|
||||
/** 音频体量上限:TTS 成品 mp3 通常几 MB,该上限已远超业务需要,防此口被误用为大文件通道。 */
|
||||
private static final long MAX_AUDIO_BYTES = 50L * 1024 * 1024;
|
||||
|
||||
/** 预签名直链时效:与原工作流自签 presigned GET 的 24h 口径一致。 */
|
||||
private static final int AUDIO_URL_EXPIRY_SECONDS = 86400;
|
||||
|
||||
private final AdminAuthSupport adminAuthSupport;
|
||||
private final OssStorageService ossStorageService;
|
||||
|
||||
@PostMapping
|
||||
@Operation(summary = "上传音频对象",
|
||||
description = "请求体为原始音频字节(Content-Type: audio/*),返回 24h 预签名下载直链。")
|
||||
public ApiResponse<InternalAudioUploadVo> upload(
|
||||
HttpServletRequest request,
|
||||
@RequestParam(value = "filename", required = false) String filename,
|
||||
@RequestBody byte[] body) {
|
||||
if (!adminAuthSupport.isTrustedInternalToken(request)) {
|
||||
log.warn("[internal-audio] 拒绝未携带可信内部令牌的上传请求 remoteAddr={}", request.getRemoteAddr());
|
||||
throw new BusinessException(401, "未授权");
|
||||
}
|
||||
if (body == null || body.length == 0) {
|
||||
return ApiResponse.fail("请求体为空");
|
||||
}
|
||||
if (body.length > MAX_AUDIO_BYTES) {
|
||||
return ApiResponse.fail("音频超过大小上限 " + MAX_AUDIO_BYTES + " 字节");
|
||||
}
|
||||
String contentType = request.getContentType();
|
||||
if (contentType == null || !contentType.toLowerCase().startsWith("audio/")) {
|
||||
return ApiResponse.fail("Content-Type 必须为 audio/*");
|
||||
}
|
||||
OssStorageService.UploadedResult result = ossStorageService.uploadInternalAudio(
|
||||
body, filename, contentType, AUDIO_URL_EXPIRY_SECONDS);
|
||||
return ApiResponse.success(new InternalAudioUploadVo(result.objectKey(), result.downloadUrl(), body.length));
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.nanri.aiimage.modules.file.model.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
@Schema(description = "内部音频上传返回信息")
|
||||
public record InternalAudioUploadVo(
|
||||
@Schema(description = "OSS 对象 key") String objectKey,
|
||||
@Schema(description = "预签名下载直链(有时效)") String audioUrl,
|
||||
@Schema(description = "音频字节数") long sizeBytes) {
|
||||
}
|
||||
+46
@@ -115,6 +115,52 @@ public class OssStorageService {
|
||||
return new UploadedResult(objectKey, getPublicUrl(objectKey, bucket));
|
||||
}
|
||||
|
||||
/**
|
||||
* 内部音频上传(工作流 TTS 等服务间调用):upload/audio/{uuid}/{filename},默认桶。
|
||||
* 调用方只持有内部令牌,不持有任何对象存储凭据;落桶与预签名都收口在服务端。
|
||||
*/
|
||||
public UploadedResult uploadInternalAudio(byte[] content, String originalFilename, String contentType, int urlExpirySeconds) {
|
||||
String objectName = sanitizeObjectName(originalFilename);
|
||||
if (objectName.isBlank()) {
|
||||
objectName = "audio.mp3";
|
||||
}
|
||||
String objectKey = String.format("upload/audio/%s/%s", UUID.randomUUID(), objectName);
|
||||
String bucket = bucketForObjectKey(objectKey);
|
||||
uploadBytes(bucket, objectKey, content, contentType);
|
||||
return new UploadedResult(objectKey,
|
||||
applyDownloadHost(presignDownloadUrl(objectKey, bucket, urlExpirySeconds)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 把预签名直链的 host 换成 download-endpoint(主机B 反代)。
|
||||
* 反代按 {@code Host: oss.aishufu.top} 回源,而 SigV4 只对服务端收到的 host 验签,
|
||||
* 所以签名保持不变、换域名后链路仍可用;对象存储所在主机不需要对外开放公网入口。
|
||||
*/
|
||||
private String applyDownloadHost(String presignedUrl) {
|
||||
String download = downloadEndpoint();
|
||||
if (download == null) {
|
||||
return presignedUrl;
|
||||
}
|
||||
URI signed = URI.create(presignedUrl);
|
||||
URI base = URI.create(download);
|
||||
return base.getScheme() + "://" + base.getAuthority() + signed.getRawPath()
|
||||
+ (signed.getRawQuery() == null ? "" : "?" + signed.getRawQuery());
|
||||
}
|
||||
|
||||
/** 内网写入、公网签名:直链 host 取 public-endpoint,内网地址签出来的 URL 用户侧不可达。 */
|
||||
private String presignDownloadUrl(String objectKey, String bucket, int expirySeconds) {
|
||||
try {
|
||||
return presignClient().getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
|
||||
.method(Method.GET)
|
||||
.bucket(bucket)
|
||||
.object(objectKey)
|
||||
.expiry(Math.max(60, expirySeconds), TimeUnit.SECONDS)
|
||||
.build());
|
||||
} catch (Exception ex) {
|
||||
throw storageFailure("presign", objectKey, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public String uploadDigitalHumanVersion(File file, String objectKey) {
|
||||
if (objectKey == null || !objectKey.startsWith(DIGITAL_HUMAN_PREFIX)) {
|
||||
throw new IllegalArgumentException("digital human objectKey must start with " + DIGITAL_HUMAN_PREFIX);
|
||||
|
||||
Reference in New Issue
Block a user