feat: 资源列表批量下载优化
This commit is contained in:
+64
-3
@@ -11,14 +11,18 @@ import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* cos 配置
|
||||
@@ -168,6 +172,63 @@ public class OssTemplate implements InitializingBean {
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下载地址
|
||||
* @param objectName 对象名
|
||||
* @return
|
||||
*/
|
||||
public String getDownloadUrl(String objectName) {
|
||||
// 1. 入参校验:避免空指针和无效路径
|
||||
if (!StringUtils.hasText(objectName)) {
|
||||
throw new IllegalArgumentException("OSS ObjectKey 不能为空!");
|
||||
}
|
||||
String bucketName = fileProperties.getOss().getBucketName();
|
||||
if (!StringUtils.hasText(bucketName)) {
|
||||
throw new IllegalArgumentException("OSS Bucket 名称未配置!");
|
||||
}
|
||||
|
||||
try {
|
||||
// 2. 设置过期时间(线程安全,替代原 Calendar)
|
||||
Instant expireInstant = Instant.now().plus(Duration.ofMinutes(DEFAULT_EXPIRE_TYPE));
|
||||
Date expiration = Date.from(expireInstant);
|
||||
|
||||
// 3. 构建签名请求(核心:GET 方法用于下载)
|
||||
GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(
|
||||
bucketName,
|
||||
objectName,
|
||||
HttpMethod.GET // 明确 HTTP 方法,避免冲突
|
||||
);
|
||||
req.setExpiration(expiration);
|
||||
|
||||
// 4. 处理文件名:兼容中文/空格,避免下载乱码
|
||||
String filename = Paths.get(objectName).getFileName().toString();
|
||||
if (!StringUtils.hasText(filename)) {
|
||||
throw new IllegalArgumentException("ObjectKey 格式错误,无法提取文件名:" + objectName);
|
||||
}
|
||||
// UTF-8 编码 + 替换空格为 %20(URL 标准编码)
|
||||
String encodedFilename = URLEncoder.encode(filename, StandardCharsets.UTF_8.name())
|
||||
.replace("+", "%20");
|
||||
|
||||
// 5. 配置响应头:强制下载(核心需求)
|
||||
ResponseHeaderOverrides headers = new ResponseHeaderOverrides();
|
||||
// RFC 5987 标准格式:兼容所有浏览器(包括 IE)的中文文件名下载
|
||||
headers.setContentDisposition("attachment;filename*=UTF-8''" + encodedFilename);
|
||||
// 固定为二进制流:确保所有文件都触发下载(而非在线预览,符合你的需求)
|
||||
// headers.setContentType("application/octet-stream");
|
||||
req.setResponseHeaders(headers);
|
||||
|
||||
// 6. 生成并返回下载 URL
|
||||
return ossClient.generatePresignedUrl(req).toString();
|
||||
|
||||
} catch (IllegalArgumentException e) {
|
||||
// 入参错误直接抛出,上层可快速处理
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
// 捕获 OSS 签名/网络/权限异常,包装后抛出(便于上层排查)
|
||||
throw new RuntimeException("生成 OSS 下载 URL 失败!objectName=" + objectName, e);
|
||||
}
|
||||
}
|
||||
|
||||
public void out(String objectName, OutputStream os) {
|
||||
out(fileProperties.getOss().getBucketName(), objectName, os);
|
||||
}
|
||||
|
||||
+64
-4
@@ -1,6 +1,7 @@
|
||||
package com.central.oss.template;
|
||||
|
||||
import com.amazonaws.ClientConfiguration;
|
||||
import com.amazonaws.HttpMethod;
|
||||
import com.amazonaws.auth.AWSCredentials;
|
||||
import com.amazonaws.auth.AWSCredentialsProvider;
|
||||
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
||||
@@ -8,10 +9,7 @@ import com.amazonaws.auth.BasicAWSCredentials;
|
||||
import com.amazonaws.client.builder.AwsClientBuilder;
|
||||
import com.amazonaws.services.s3.AmazonS3;
|
||||
import com.amazonaws.services.s3.AmazonS3Client;
|
||||
import com.amazonaws.services.s3.model.ObjectMetadata;
|
||||
import com.amazonaws.services.s3.model.PutObjectRequest;
|
||||
import com.amazonaws.services.s3.model.S3Object;
|
||||
import com.amazonaws.services.s3.model.S3ObjectInputStream;
|
||||
import com.amazonaws.services.s3.model.*;
|
||||
import com.amazonaws.util.IOUtils;
|
||||
import com.central.oss.model.ObjectInfo;
|
||||
import com.central.oss.properties.FileServerProperties;
|
||||
@@ -21,13 +19,20 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* aws s3配置
|
||||
@@ -125,6 +130,61 @@ public class S3Template implements InitializingBean {
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下载地址
|
||||
* @param objectName 对象名
|
||||
* @return
|
||||
*/
|
||||
public String getDownloadUrl(String objectName) {
|
||||
// 1. 入参校验:避免空指针和无效路径(关键:防止后续 NPE)
|
||||
if (!StringUtils.hasText(objectName)) {
|
||||
throw new IllegalArgumentException("S3 ObjectKey 不能为空!");
|
||||
}
|
||||
String bucketName = fileProperties.getS3().getBucketName();
|
||||
if (!StringUtils.hasText(bucketName)) {
|
||||
throw new IllegalArgumentException("S3 Bucket 名称未配置!");
|
||||
}
|
||||
try {
|
||||
// 2. 过期时间:24小时(线程安全,替代 Calendar)
|
||||
Instant expireInstant = Instant.now().plus(Duration.ofHours(24));
|
||||
Date expiration = Date.from(expireInstant);
|
||||
|
||||
// 3. 构建 S3 签名请求
|
||||
GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(
|
||||
bucketName,
|
||||
objectName,
|
||||
HttpMethod.GET
|
||||
);
|
||||
req.setExpiration(expiration);
|
||||
|
||||
// 4. 处理文件名:兼容中文/空格/特殊字符
|
||||
String filename = Paths.get(objectName).getFileName().toString();
|
||||
if (!StringUtils.hasText(filename)) {
|
||||
throw new IllegalArgumentException("ObjectKey 格式错误,无法提取文件名:" + objectName);
|
||||
}
|
||||
String encodedFilename = URLEncoder.encode(filename, StandardCharsets.UTF_8.name())
|
||||
.replace("+", "%20") // 空格转义为 %20(URL 标准)
|
||||
.replace("*", "%2A"); // 星号转义(避免 URL 解析错误)
|
||||
|
||||
// 5. 核心:配置强制下载的响应头
|
||||
ResponseHeaderOverrides presetHeaders = new ResponseHeaderOverrides();
|
||||
presetHeaders.setContentDisposition("attachment;filename*=UTF-8''" + encodedFilename);
|
||||
presetHeaders.setContentType("application/octet-stream");
|
||||
presetHeaders.setCacheControl("no-cache, no-store, must-revalidate");
|
||||
req.setResponseHeaders(presetHeaders);
|
||||
|
||||
// 6. 生成并返回下载 URL
|
||||
return amazonS3.generatePresignedUrl(req).toString();
|
||||
|
||||
} catch (IllegalArgumentException e) {
|
||||
// 入参错误直接抛出,上层快速处理
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
// 捕获 S3 签名/网络/权限异常,包装后抛出(便于排查)
|
||||
throw new RuntimeException("生成 S3 强制下载 URL 失败!objectName=" + objectName, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取预览地址
|
||||
* @param path
|
||||
|
||||
Reference in New Issue
Block a user