From 58f1e2ba25587ae4c0db0d009a401ef24ad65d8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Fri, 27 Mar 2026 16:02:56 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20oss=20=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E4=BB=A3=E7=A0=81=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../oss/client/AbstractOssClientImpl.java | 82 +++++++++++++------ .../dromara/common/oss/client/OssClient.java | 24 ++++-- .../common/oss/config/OssClientConfig.java | 1 + .../common/oss/factory/OssFactory.java | 33 ++++---- .../dromara/common/oss/util/S3ObjectUtil.java | 46 ----------- .../service/impl/SysOssServiceImpl.java | 9 +- 6 files changed, 96 insertions(+), 99 deletions(-) delete mode 100644 ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/util/S3ObjectUtil.java diff --git a/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/client/AbstractOssClientImpl.java b/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/client/AbstractOssClientImpl.java index b7c5f7a64..af4eddd61 100644 --- a/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/client/AbstractOssClientImpl.java +++ b/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/client/AbstractOssClientImpl.java @@ -2,6 +2,7 @@ package org.dromara.common.oss.client; import cn.hutool.core.lang.Assert; import cn.hutool.core.util.IdUtil; +import org.dromara.common.core.utils.DateUtils; import org.dromara.common.core.utils.StringUtils; import org.dromara.common.oss.config.OssClientConfig; import org.dromara.common.oss.exception.S3StorageException; @@ -127,27 +128,6 @@ public abstract class AbstractOssClientImpl implements OssClient { abstract void doInitialize(); - @Override - public void refresh(OssClientConfig config) { - if (Objects.equals(this.config, config)) { - return; - } - // 如果状态本来就是未初始化,直接则调用初始化 - if (!initialized.get()) { - this.initialize(); - } - // 将状态转为未初始化 - if (initialized.compareAndSet(false, true)) { - try { - this.close(); - } catch (Exception e) { - // 异常不影响刷新逻辑,此处屏蔽异常 - } - // 状态交换成功才进行刷新 - this.initialize(); - } - } - @Override public boolean verifyConfig(Function verifyConfigAction) { OssClientConfig config = config(); @@ -159,6 +139,23 @@ public abstract class AbstractOssClientImpl implements OssClient { return verifyConfig((config) -> Objects.equals(config, verifyConfig)); } + @Override + public String buildPathKey(String fileName) { + return buildPathKey(null, fileName); + } + + @Override + public String buildPathKey(String businessPrefix, String fileName) { + String defaultPrefix = config.prefix() + .orElse(""); + String mergedPrefix = mergePrefix(defaultPrefix, businessPrefix); + String suffix = suffix(fileName); + String datePath = DateUtils.datePath(); + String uuid = IdUtil.fastSimpleUUID(); + String path = mergedPrefix.isEmpty() ? datePath + StringUtils.SLASH + uuid : mergedPrefix + StringUtils.SLASH + datePath + StringUtils.SLASH + uuid; + return path + suffix; + } + @Override public T doCustomUpload(AsyncRequestBody body, Consumer putObjectRequestBuilderConsumer, Collection transferListeners, BiFunction handleAsyncAction) { try { @@ -230,7 +227,7 @@ public abstract class AbstractOssClientImpl implements OssClient { try { // 以文件的大小为准 options.setLength(file.length()); - return bucketUpload(bucket, key, file.getChannel(), -1L); + return bucketUpload(bucket, key, file.getChannel(), -1L, options); } catch (Exception e) { if (e instanceof S3StorageException ex) { throw ex; @@ -438,8 +435,8 @@ public abstract class AbstractOssClientImpl implements OssClient { @Override public boolean bucketDelete(String bucket, String key) { try { - DeleteObjectResponse response = s3AsyncClient.deleteObject(builder -> builder.bucket(bucket).key(key)).join(); - return Boolean.TRUE.equals(response.deleteMarker()); + s3AsyncClient.deleteObject(builder -> builder.bucket(bucket).key(key)).join(); + return true; } catch (Exception e) { throw S3StorageException.form(e); } @@ -589,6 +586,43 @@ public abstract class AbstractOssClientImpl implements OssClient { .orElseThrow(() -> S3StorageException.form("bucket is not configured.")); } + private String mergePrefix(String defaultPrefix, String businessPrefix) { + String left = normalizePrefix(defaultPrefix); + String right = normalizePrefix(businessPrefix); + if (left.isEmpty()) { + return right; + } + if (right.isEmpty()) { + return left; + } + return left + StringUtils.SLASH + right; + } + + private String normalizePrefix(String prefix) { + if (prefix == null) { + return ""; + } + String normalized = prefix.trim(); + while (normalized.startsWith(StringUtils.SLASH)) { + normalized = normalized.substring(1); + } + while (normalized.endsWith(StringUtils.SLASH)) { + normalized = normalized.substring(0, normalized.length() - 1); + } + return normalized; + } + + private String suffix(String fileName) { + if (fileName == null) { + return ""; + } + int index = fileName.lastIndexOf('.'); + if (index < 0) { + return ""; + } + return fileName.substring(index); + } + @Override public void close() throws Exception { if (s3TransferManager != null) { diff --git a/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/client/OssClient.java b/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/client/OssClient.java index 4281cfa3b..e8a038ab4 100644 --- a/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/client/OssClient.java +++ b/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/client/OssClient.java @@ -68,13 +68,6 @@ public interface OssClient extends AutoCloseable { */ void initialize(); - /** - * 刷新客户端配置 - * - * @param config 配置项 - */ - void refresh(OssClientConfig config); - /** * 校验客户端配置 * @@ -585,4 +578,21 @@ public interface OssClient extends AutoCloseable { * @return 预签名上传 URL */ String presignPutUrl(String key, Duration expiredTime, Map metadata); + + /** + * 根据客户端配置生成默认对象Key。 + * + * @param fileName 原始文件名 + * @return 对象Key + */ + String buildPathKey(String fileName); + + /** + * 根据业务前缀和客户端默认前缀生成对象Key。 + * + * @param businessPrefix 业务前缀 + * @param fileName 原始文件名 + * @return 对象Key + */ + String buildPathKey(String businessPrefix, String fileName); } diff --git a/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/config/OssClientConfig.java b/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/config/OssClientConfig.java index 2c46270f7..cbafc9de2 100644 --- a/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/config/OssClientConfig.java +++ b/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/config/OssClientConfig.java @@ -182,6 +182,7 @@ public class OssClientConfig implements Config