bug修复

This commit is contained in:
张锐 committed 2026-11-24 21:32:53 +08:00
1 parent ab0245b9a3
commit 7823f67b04
14 files changed
+317 -88

No files matched your search

@@ -0,0 +1,8 @@
package com.shoplix.modules.media.enums;
/** 媒体资源类型。 */
public enum MediaTypeEnum {
IMAGE,
VIDEO,
DOCUMENT
}
@@ -0,0 +1,52 @@
package com.shoplix.modules.media.storage;
import com.shoplix.common.exception.BusinessException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.S3Configuration;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
/** S3 兼容对象存储服务。 */
@Service
public class ObjectStorageService {
private final String bucket;
private final String publicBaseUrl;
private final S3Client client;
/** 根据环境配置初始化对象存储客户端。 */
public ObjectStorageService(@Value("${shoplix.storage.endpoint}") String endpoint,
@Value("${shoplix.storage.bucket}") String bucket,
@Value("${shoplix.storage.region}") String region,
@Value("${shoplix.storage.access-key:}") String accessKey,
@Value("${shoplix.storage.secret-key:}") String secretKey,
@Value("${shoplix.storage.path-style-access:true}") boolean pathStyleAccess,
@Value("${shoplix.storage.public-base-url}") String publicBaseUrl) {
this.bucket = bucket;
this.publicBaseUrl = trimTrailingSlash(publicBaseUrl);
this.client = S3Client.builder().endpointOverride(URI.create(endpoint)).region(Region.of(region))
.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) {
try (inputStream) {
client.putObject(PutObjectRequest.builder().bucket(bucket).key(objectKey).contentType(contentType)
.contentLength(contentLength).build(), RequestBody.fromInputStream(inputStream, contentLength));
return publicBaseUrl + "/" + objectKey;
} catch (IOException | RuntimeException exception) {
throw new BusinessException("OBJECT_STORAGE_UPLOAD_FAILED", "文件上传到对象存储失败");
}
}
/** 去除公开地址末尾的路径分隔符。 */ private String trimTrailingSlash(String value) { return value.replaceFirst("/+$", ""); }
}