diff --git a/zt-module-infra/zt-module-infra-server/src/main/java/com/zt/plat/module/infra/framework/file/core/client/s3/S3FileClient.java b/zt-module-infra/zt-module-infra-server/src/main/java/com/zt/plat/module/infra/framework/file/core/client/s3/S3FileClient.java index 8cdce08b..7b716ba8 100644 --- a/zt-module-infra/zt-module-infra-server/src/main/java/com/zt/plat/module/infra/framework/file/core/client/s3/S3FileClient.java +++ b/zt-module-infra/zt-module-infra-server/src/main/java/com/zt/plat/module/infra/framework/file/core/client/s3/S3FileClient.java @@ -29,6 +29,9 @@ import java.time.Duration; * @author ZT */ public class S3FileClient extends AbstractFileClient { + + private static final Duration DEFAULT_PRESIGNED_EXPIRATION = Duration.ofHours(24); + private static final String PRESIGN_EXPIRE_SECONDS_PROPERTY = "zt.file.download-expire-seconds"; /** * 生成临时下载地址(预签名下载 URL) * @param path 文件路径 @@ -37,17 +40,7 @@ public class S3FileClient extends AbstractFileClient { */ @Override public String getPresignedDownloadUrl(String path, Duration expiration) { - Duration realExpiration = expiration; - if (realExpiration == null) { - long expireSeconds = 30; // 默认 30 秒 - try { - String val = SpringUtils.getProperty("zt.file.download-expire-seconds"); - if (val != null && !val.isEmpty()) { - expireSeconds = Long.parseLong(val); - } - } catch (Exception ignored) {} - realExpiration = Duration.ofSeconds(expireSeconds); - } + Duration realExpiration = expiration != null ? expiration : resolveDefaultExpiration(); if (path == null){ return StringUtils.EMPTY; } @@ -135,10 +128,25 @@ public class S3FileClient extends AbstractFileClient { @Override public FilePresignedUrlRespDTO getPresignedObjectUrl(String path) { - Duration expiration = Duration.ofHours(24); + Duration expiration = resolveDefaultExpiration(); return new FilePresignedUrlRespDTO(getPresignedUrl(path, expiration), config.getDomain() + "/" + path); } + private Duration resolveDefaultExpiration() { + String propertyValue = SpringUtils.getProperty(PRESIGN_EXPIRE_SECONDS_PROPERTY); + if (StringUtils.isNotEmpty(propertyValue)) { + try { + long seconds = Long.parseLong(propertyValue); + if (seconds > 0) { + return Duration.ofSeconds(seconds); + } + } catch (NumberFormatException ignored) { + // ignore invalid config values and fall back to default + } + } + return DEFAULT_PRESIGNED_EXPIRATION; + } + /** * 生成动态的预签名上传 URL * diff --git a/zt-module-infra/zt-module-infra-server/src/main/resources/application.yaml b/zt-module-infra/zt-module-infra-server/src/main/resources/application.yaml index 1d4dd83d..45f2bb3f 100644 --- a/zt-module-infra/zt-module-infra-server/src/main/resources/application.yaml +++ b/zt-module-infra/zt-module-infra-server/src/main/resources/application.yaml @@ -148,6 +148,8 @@ zt: key: "0123456789abcdef0123456789abcdef" # 附件预览 kkfile: "http://172.16.46.63:30012/onlinePreview?url=" + file: + download-expire-seconds: 86400 # 对象存储预签名地址默认有效期(秒) info: version: 1.0.0 base-package: com.zt.plat.module.infra diff --git a/zt-server/src/main/resources/application.yaml b/zt-server/src/main/resources/application.yaml index d502e1d9..fc8bd152 100644 --- a/zt-server/src/main/resources/application.yaml +++ b/zt-server/src/main/resources/application.yaml @@ -257,6 +257,8 @@ zt: info: version: 1.0.0 base-package: com.zt.plat + file: + download-expire-seconds: 86400 # 对象存储预签名地址默认有效期(秒) web: admin-ui: url: http://dashboard.zt.iocoder.cn # Admin 管理后台 UI 的地址