bug修复

This commit is contained in:
张锐 committed 2026-11-24 21:40:43 +08:00
1 parent 7823f67b04
commit fbd3692036
2 files changed
+50 -3

No files matched your search

@@ -3,6 +3,7 @@ package com.shoplix.modules.media.storage;
import com.shoplix.common.exception.BusinessException; import com.shoplix.common.exception.BusinessException;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.sync.RequestBody; import software.amazon.awssdk.core.sync.RequestBody;
@@ -32,13 +33,15 @@ public class ObjectStorageService {
@Value("${shoplix.storage.public-base-url}") String publicBaseUrl) { @Value("${shoplix.storage.public-base-url}") String publicBaseUrl) {
this.bucket = bucket; this.bucket = bucket;
this.publicBaseUrl = trimTrailingSlash(publicBaseUrl); this.publicBaseUrl = trimTrailingSlash(publicBaseUrl);
this.client = S3Client.builder().endpointOverride(URI.create(endpoint)).region(Region.of(region)) this.client = createClient(endpoint, region, accessKey, secretKey, pathStyleAccess);
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKey, secretKey)))
.serviceConfiguration(S3Configuration.builder().pathStyleAccessEnabled(pathStyleAccess).build()).build();
} }
/** 上传对象并返回面向访客的公开地址。 */ /** 上传对象并返回面向访客的公开地址。 */
public String upload(String objectKey, String contentType, long contentLength, InputStream inputStream) { public String upload(String objectKey, String contentType, long contentLength, InputStream inputStream) {
if (client == null) {
closeQuietly(inputStream);
throw new BusinessException("OBJECT_STORAGE_NOT_CONFIGURED", "对象存储访问密钥未配置");
}
try (inputStream) { try (inputStream) {
client.putObject(PutObjectRequest.builder().bucket(bucket).key(objectKey).contentType(contentType) client.putObject(PutObjectRequest.builder().bucket(bucket).key(objectKey).contentType(contentType)
.contentLength(contentLength).build(), RequestBody.fromInputStream(inputStream, contentLength)); .contentLength(contentLength).build(), RequestBody.fromInputStream(inputStream, contentLength));
@@ -48,5 +51,23 @@ public class ObjectStorageService {
} }
} }
/** 密钥完整时创建客户端,缺少密钥时延迟到上传阶段提示。 */
private S3Client createClient(String endpoint, String region, String accessKey, String secretKey,
boolean pathStyleAccess) {
if (!StringUtils.hasText(accessKey) || !StringUtils.hasText(secretKey)) return null;
return S3Client.builder().endpointOverride(URI.create(endpoint)).region(Region.of(region))
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKey, secretKey)))
.serviceConfiguration(S3Configuration.builder().pathStyleAccessEnabled(pathStyleAccess).build()).build();
}
/** 配置缺失时关闭上传流并忽略关闭异常。 */
private void closeQuietly(InputStream inputStream) {
try {
inputStream.close();
} catch (IOException ignored) {
// 上传尚未开始,关闭失败不覆盖明确的配置错误。
}
}
/** 去除公开地址末尾的路径分隔符。 */ private String trimTrailingSlash(String value) { return value.replaceFirst("/+$", ""); } /** 去除公开地址末尾的路径分隔符。 */ private String trimTrailingSlash(String value) { return value.replaceFirst("/+$", ""); }
} }
@@ -0,0 +1,26 @@
package com.shoplix.modules.media.storage;
import com.shoplix.common.exception.BusinessException;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
/** 对象存储配置容错测试。 */
class ObjectStorageServiceTest {
/** 验证缺少密钥不会阻止应用启动,并在上传时返回明确错误。 */
@Test
void shouldDelayMissingCredentialErrorUntilUpload() {
ObjectStorageService service = new ObjectStorageService("http://127.0.0.1:19000", "bucket",
"us-east-1", "", "", true, "https://oss.example.com/bucket");
BusinessException exception = assertThrows(BusinessException.class,
() -> service.upload("media/test.png", "image/png", 1,
new ByteArrayInputStream(new byte[]{1})));
assertEquals("OBJECT_STORAGE_NOT_CONFIGURED", exception.getCode());
}
}