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);
|
||||
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
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 org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class InternalAudioUploadControllerTest {
|
||||
|
||||
private AdminAuthSupport adminAuthSupport;
|
||||
private OssStorageService ossStorageService;
|
||||
private InternalAudioUploadController controller;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
adminAuthSupport = mock(AdminAuthSupport.class);
|
||||
ossStorageService = mock(OssStorageService.class);
|
||||
controller = new InternalAudioUploadController(adminAuthSupport, ossStorageService);
|
||||
}
|
||||
|
||||
private MockHttpServletRequest request(String contentType) {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setContentType(contentType);
|
||||
return request;
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsRequestWithoutTrustedInternalToken() {
|
||||
when(adminAuthSupport.isTrustedInternalToken(any())).thenReturn(false);
|
||||
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> controller.upload(request("audio/mpeg"), "tts.mp3", new byte[]{1}));
|
||||
|
||||
assertEquals(401, ex.getCode());
|
||||
verify(ossStorageService, never()).uploadInternalAudio(any(), anyString(), anyString(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void uploadsAudioAndReturnsPresignedUrl() {
|
||||
when(adminAuthSupport.isTrustedInternalToken(any())).thenReturn(true);
|
||||
when(ossStorageService.uploadInternalAudio(any(), eq("tts_1.mp3"), eq("audio/mpeg"), eq(86400)))
|
||||
.thenReturn(new OssStorageService.UploadedResult(
|
||||
"upload/audio/uuid/tts_1.mp3",
|
||||
"https://oss.aishufu.top/nanri-ai-images/upload/audio/uuid/tts_1.mp3?X-Amz-Signature=sig"));
|
||||
|
||||
MockHttpServletRequest request = request("audio/mpeg");
|
||||
request.addHeader("X-Internal-Token", "token");
|
||||
ApiResponse<InternalAudioUploadVo> response = controller.upload(request, "tts_1.mp3", new byte[]{1, 2, 3});
|
||||
|
||||
assertTrue(response.isSuccess());
|
||||
assertEquals("upload/audio/uuid/tts_1.mp3", response.getData().objectKey());
|
||||
assertEquals("https://oss.aishufu.top/nanri-ai-images/upload/audio/uuid/tts_1.mp3?X-Amz-Signature=sig",
|
||||
response.getData().audioUrl());
|
||||
assertEquals(3L, response.getData().sizeBytes());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsEmptyBody() {
|
||||
when(adminAuthSupport.isTrustedInternalToken(any())).thenReturn(true);
|
||||
|
||||
assertFalse(controller.upload(request("audio/mpeg"), null, new byte[0]).isSuccess());
|
||||
verify(ossStorageService, never()).uploadInternalAudio(any(), anyString(), anyString(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsOversizedBody() {
|
||||
when(adminAuthSupport.isTrustedInternalToken(any())).thenReturn(true);
|
||||
|
||||
byte[] oversized = new byte[50 * 1024 * 1024 + 1];
|
||||
assertFalse(controller.upload(request("audio/mpeg"), null, oversized).isSuccess());
|
||||
verify(ossStorageService, never()).uploadInternalAudio(any(), anyString(), anyString(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsNonAudioContentType() {
|
||||
when(adminAuthSupport.isTrustedInternalToken(any())).thenReturn(true);
|
||||
|
||||
assertFalse(controller.upload(request("application/octet-stream"), "a.bin", new byte[]{1}).isSuccess());
|
||||
assertFalse(controller.upload(request(null), "a.bin", new byte[]{1}).isSuccess());
|
||||
verify(ossStorageService, never()).uploadInternalAudio(any(), anyString(), anyString(), anyInt());
|
||||
}
|
||||
}
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
package com.nanri.aiimage.modules.file.service.oss;
|
||||
|
||||
import com.nanri.aiimage.config.OssProperties;
|
||||
import io.minio.GetPresignedObjectUrlArgs;
|
||||
import io.minio.MinioClient;
|
||||
import io.minio.PutObjectArgs;
|
||||
import io.minio.http.Method;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
class OssStorageServiceInternalAudioTest {
|
||||
|
||||
private OssProperties properties;
|
||||
private MinioClient client;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
properties = new OssProperties();
|
||||
properties.setEndpoint("https://oss.aishufu.top");
|
||||
properties.setPublicEndpoint("https://oss.aishufu.top");
|
||||
properties.setBucket("nanri-ai-images");
|
||||
properties.setAccessKeyId("test-access-key");
|
||||
properties.setAccessKeySecret("test-secret-key");
|
||||
client = mock(MinioClient.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void internalAudioRoutesToDefaultBucketAndPresignsGet() throws Exception {
|
||||
properties.setDownloadEndpoint("https://download.aishufu.top");
|
||||
doReturn(null).when(client).putObject(any(PutObjectArgs.class));
|
||||
whenPresignReturns("https://oss.aishufu.top/nanri-ai-images/upload/audio/uuid/a.mp3?X-Amz-Signature=sig");
|
||||
|
||||
OssStorageService oss = new OssStorageService(properties, client);
|
||||
OssStorageService.UploadedResult result = oss.uploadInternalAudio(
|
||||
new byte[]{1, 2, 3}, "tts_1.mp3", "audio/mpeg", 86400);
|
||||
|
||||
ArgumentCaptor<PutObjectArgs> putCaptor = ArgumentCaptor.forClass(PutObjectArgs.class);
|
||||
verify(client).putObject(putCaptor.capture());
|
||||
assertEquals("nanri-ai-images", putCaptor.getValue().bucket());
|
||||
assertTrue(putCaptor.getValue().object().matches("upload/audio/[0-9a-f-]{36}/tts_1\\.mp3"),
|
||||
"对象键应为 upload/audio/{uuid}/{filename},实际:" + putCaptor.getValue().object());
|
||||
assertEquals("audio/mpeg", putCaptor.getValue().contentType());
|
||||
|
||||
assertEquals(putCaptor.getValue().object(), result.objectKey());
|
||||
assertTrue(result.objectKey().startsWith("upload/audio/"), "对象键前缀应保持 upload/audio/");
|
||||
// 出链换到主机B 反代域名,路径与查询串(含签名)逐字保留
|
||||
assertEquals("https://download.aishufu.top/nanri-ai-images/upload/audio/uuid/a.mp3?X-Amz-Signature=sig",
|
||||
result.downloadUrl());
|
||||
|
||||
ArgumentCaptor<GetPresignedObjectUrlArgs> presignCaptor =
|
||||
ArgumentCaptor.forClass(GetPresignedObjectUrlArgs.class);
|
||||
verify(client).getPresignedObjectUrl(presignCaptor.capture());
|
||||
assertEquals(Method.GET, presignCaptor.getValue().method());
|
||||
}
|
||||
|
||||
@Test
|
||||
void internalAudioKeepsSignedHostWhenDownloadEndpointUnset() throws Exception {
|
||||
// download-endpoint 留空时不得凭空造域名:仍按签名 host 出链
|
||||
doReturn(null).when(client).putObject(any(PutObjectArgs.class));
|
||||
whenPresignReturns("https://oss.aishufu.top/nanri-ai-images/upload/audio/uuid/a.mp3?X-Amz-Signature=sig");
|
||||
|
||||
OssStorageService oss = new OssStorageService(properties, client);
|
||||
OssStorageService.UploadedResult result = oss.uploadInternalAudio(
|
||||
new byte[]{1}, "a.mp3", "audio/mpeg", 86400);
|
||||
|
||||
assertEquals("https://oss.aishufu.top/nanri-ai-images/upload/audio/uuid/a.mp3?X-Amz-Signature=sig",
|
||||
result.downloadUrl());
|
||||
}
|
||||
|
||||
@Test
|
||||
void internalAudioWithoutFilenameFallsBackToAudioMp3() throws Exception {
|
||||
doReturn(null).when(client).putObject(any(PutObjectArgs.class));
|
||||
whenPresignReturns("https://oss.aishufu.top/nanri-ai-images/upload/audio/uuid/audio.mp3?X-Amz-Signature=sig");
|
||||
|
||||
OssStorageService oss = new OssStorageService(properties, client);
|
||||
OssStorageService.UploadedResult result = oss.uploadInternalAudio(new byte[]{1}, null, "audio/mpeg", 86400);
|
||||
|
||||
assertTrue(result.objectKey().endsWith("/audio.mp3"));
|
||||
}
|
||||
|
||||
private void whenPresignReturns(String url) throws Exception {
|
||||
org.mockito.Mockito.when(client.getPresignedObjectUrl(any(GetPresignedObjectUrlArgs.class)))
|
||||
.thenReturn(url);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user