From d634c2a292250c422997e1c1055e7a6b3870d207 Mon Sep 17 00:00:00 2001 From: AprilWind <2100166581@qq.com> Date: Mon, 5 Jan 2026 14:39:40 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96oss=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E4=BE=A6=E5=90=AC=E5=99=A8=E6=89=93=E5=8D=B0=E7=BA=A7?= =?UTF-8?q?=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dromara/common/oss/core/OssClient.java | 67 +++++++++++-------- 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/core/OssClient.java b/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/core/OssClient.java index 089d1926b..14fc4dcfe 100644 --- a/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/core/OssClient.java +++ b/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/core/OssClient.java @@ -141,7 +141,8 @@ public class OssClient { try { // 构建上传请求对象 FileUpload fileUpload = transferManager.uploadFile( - x -> x.putObjectRequest( + x -> { + x.source(filePath).putObjectRequest( y -> y.bucket(properties.getBucketName()) .key(key) .contentMD5(StringUtils.isNotEmpty(md5Digest) ? md5Digest : null) @@ -149,10 +150,13 @@ public class OssClient { // 用于设置对象的访问控制列表(ACL)。不同云厂商对ACL的支持和实现方式有所不同, // 因此根据具体的云服务提供商,你可能需要进行不同的配置(自行开启,阿里云有acl权限配置,腾讯云没有acl权限配置) //.acl(getAccessPolicy().getObjectCannedACL()) - .build()) - .addTransferListener(LoggingTransferListener.create()) - .source(filePath).build()); - + .build() + ); + if (log.isDebugEnabled()) { + x.addTransferListener(LoggingTransferListener.create()); + } + } + ); // 等待上传完成并获取上传结果 CompletedFileUpload uploadResult = fileUpload.completionFuture().join(); String eTag = uploadResult.response().eTag(); @@ -192,16 +196,21 @@ public class OssClient { // 使用 transferManager 进行上传 Upload upload = transferManager.upload( - x -> x.requestBody(body).addTransferListener(LoggingTransferListener.create()) - .putObjectRequest( + x -> { + x.requestBody(body).putObjectRequest( y -> y.bucket(properties.getBucketName()) .key(key) .contentType(contentType) // 用于设置对象的访问控制列表(ACL)。不同云厂商对ACL的支持和实现方式有所不同, // 因此根据具体的云服务提供商,你可能需要进行不同的配置(自行开启,阿里云有acl权限配置,腾讯云没有acl权限配置) //.acl(getAccessPolicy().getObjectCannedACL()) - .build()) - .build()); + .build() + ); + if (log.isDebugEnabled()) { + x.addTransferListener(LoggingTransferListener.create()); + } + } + ); // 将输入流写入请求体 body.writeInputStream(inputStream); @@ -229,13 +238,17 @@ public class OssClient { Path tempFilePath = FileUtils.createTempFile().toPath(); // 使用 S3TransferManager 下载文件 FileDownload downloadFile = transferManager.downloadFile( - x -> x.getObjectRequest( + x -> { + x.destination(tempFilePath).getObjectRequest( y -> y.bucket(properties.getBucketName()) .key(removeBaseUrl(path)) - .build()) - .addTransferListener(LoggingTransferListener.create()) - .destination(tempFilePath) - .build()); + .build() + ); + if (log.isDebugEnabled()) { + x.addTransferListener(LoggingTransferListener.create()); + } + } + ); // 等待文件下载操作完成 downloadFile.completionFuture().join(); return tempFilePath; @@ -244,8 +257,8 @@ public class OssClient { /** * 下载文件从 Amazon S3 到 输出流 * - * @param key 文件在 Amazon S3 中的对象键 - * @param out 输出流 + * @param key 文件在 Amazon S3 中的对象键 + * @param out 输出流 * @param consumer 自定义处理逻辑 * @throws OssException 如果下载失败,抛出自定义异常 */ @@ -260,26 +273,24 @@ public class OssClient { /** * 下载文件从 Amazon S3 到 输出流 * - * @param key 文件在 Amazon S3 中的对象键 + * @param key 文件在 Amazon S3 中的对象键 * @param contentLengthConsumer 文件大小消费者函数 * @return 写出订阅器 * @throws OssException 如果下载失败,抛出自定义异常 */ public WriteOutSubscriber download(String key, Consumer contentLengthConsumer) { try { - // 构建下载请求 - DownloadRequest> publisherDownloadRequest = DownloadRequest.builder() - // 文件对象 - .getObjectRequest(y -> y.bucket(properties.getBucketName()) - .key(key) - .build()) - .addTransferListener(LoggingTransferListener.create()) + DownloadRequest.TypedBuilder> typedBuilder = DownloadRequest.builder() // 使用发布订阅转换器 .responseTransformer(AsyncResponseTransformer.toPublisher()) - .build(); + // 文件对象 + .getObjectRequest(y -> y.bucket(properties.getBucketName()).key(key).build()); + if (log.isDebugEnabled()) { + typedBuilder.addTransferListener(LoggingTransferListener.create()); + } // 使用 S3TransferManager 下载文件 - Download> publisherDownload = transferManager.download(publisherDownloadRequest); + Download> publisherDownload = transferManager.download(typedBuilder.build()); // 获取下载发布订阅转换器 ResponsePublisher publisher = publisherDownload.completionFuture().join().result(); // 执行文件大小消费者函数 @@ -289,7 +300,7 @@ public class OssClient { // 构建写出订阅器对象 return out -> { // 创建可写入的字节通道 - try(WritableByteChannel channel = Channels.newChannel(out)){ + try (WritableByteChannel channel = Channels.newChannel(out)) { // 订阅数据 publisher.subscribe(byteBuffer -> { while (byteBuffer.hasRemaining()) { @@ -347,7 +358,7 @@ public class OssClient { * * @param objectKey 对象KEY * @param expiredTime 链接授权到期时间 - * @param metadata 元数据 + * @param metadata 元数据 */ public String createPresignedPutUrl(String objectKey, Duration expiredTime, Map metadata) { // 使用 AWS S3 预签名 URL 的生成器 获取上传文件对象的预签名 URL