疯狂的狮子Li
2025-01-17 e8acfac09190e29b4aea8cfc41d43dac099603a4
ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/core/OssClient.java
@@ -32,6 +32,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.function.Consumer;
/**
 * S3 存储协议 所有兼容S3协议的云厂商均支持
@@ -239,10 +240,11 @@
     *
     * @param key 文件在 Amazon S3 中的对象键
     * @param out 输出流
     * @param consumer 自定义处理逻辑
     * @return 输出流中写入的字节数(长度)
     * @throws OssException 如果下载失败,抛出自定义异常
     */
    public long download(String key, OutputStream out) {
    public void download(String key, OutputStream out, Consumer<Long> consumer) {
        try {
            // 构建下载请求
            DownloadRequest<ResponseInputStream<GetObjectResponse>> downloadRequest = DownloadRequest.builder()
@@ -258,7 +260,10 @@
            Download<ResponseInputStream<GetObjectResponse>> responseFuture = transferManager.download(downloadRequest);
            // 输出到流中
            try (ResponseInputStream<GetObjectResponse> responseStream = responseFuture.completionFuture().join().result()) { // auto-closeable stream
                return responseStream.transferTo(out); // 阻塞调用线程 blocks the calling thread
                if (consumer != null) {
                    consumer.accept(responseStream.response().contentLength());
                }
                responseStream.transferTo(out); // 阻塞调用线程 blocks the calling thread
            }
        } catch (Exception e) {
            throw new OssException("文件下载失败,错误信息:[" + e.getMessage() + "]");