Commit 5804957149768fcf47f008eaebfa0456842e0e45
1 parent
478db1bd
基础设施:前端直连上传,优先使用自定义域名
Showing
5 changed files
with
39 additions
and
5 deletions
yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/FileClient.java
| 1 | package cn.iocoder.yudao.framework.file.core.client; | 1 | package cn.iocoder.yudao.framework.file.core.client; |
| 2 | 2 | ||
| 3 | +import cn.iocoder.yudao.framework.file.core.client.s3.FilePresignedUrlBO; | ||
| 4 | + | ||
| 3 | /** | 5 | /** |
| 4 | * 文件客户端 | 6 | * 文件客户端 |
| 5 | * | 7 | * |
| @@ -46,7 +48,7 @@ public interface FileClient { | @@ -46,7 +48,7 @@ public interface FileClient { | ||
| 46 | * @param fileName 文件名称 | 48 | * @param fileName 文件名称 |
| 47 | * @return 文件预签名地址 | 49 | * @return 文件预签名地址 |
| 48 | */ | 50 | */ |
| 49 | - default String getPresignedObjectUrl(String fileName) throws Exception { | 51 | + default FilePresignedUrlBO getPresignedObjectUrl(String fileName) throws Exception { |
| 50 | throw new UnsupportedOperationException("不支持的操作"); | 52 | throw new UnsupportedOperationException("不支持的操作"); |
| 51 | } | 53 | } |
| 52 | 54 |
yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/s3/FilePresignedUrlBO.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.file.core.client.s3; | ||
| 2 | + | ||
| 3 | +import lombok.AllArgsConstructor; | ||
| 4 | +import lombok.Data; | ||
| 5 | +import lombok.NoArgsConstructor; | ||
| 6 | + | ||
| 7 | +/** | ||
| 8 | + * 文件预签名地址 BO | ||
| 9 | + * | ||
| 10 | + * @author owen | ||
| 11 | + */ | ||
| 12 | +@AllArgsConstructor | ||
| 13 | +@NoArgsConstructor | ||
| 14 | +@Data | ||
| 15 | +public class FilePresignedUrlBO { | ||
| 16 | + | ||
| 17 | + /** | ||
| 18 | + * 文件上传 URL(用于上传) | ||
| 19 | + */ | ||
| 20 | + private String uploadUrl; | ||
| 21 | + | ||
| 22 | + /** | ||
| 23 | + * 文件 URL(用于读取、下载等) | ||
| 24 | + */ | ||
| 25 | + private String url; | ||
| 26 | + | ||
| 27 | +} |
yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/s3/S3FileClient.java
| @@ -120,8 +120,8 @@ public class S3FileClient extends AbstractFileClient<S3FileClientConfig> { | @@ -120,8 +120,8 @@ public class S3FileClient extends AbstractFileClient<S3FileClientConfig> { | ||
| 120 | } | 120 | } |
| 121 | 121 | ||
| 122 | @Override | 122 | @Override |
| 123 | - public String getPresignedObjectUrl(String fileName) throws Exception { | ||
| 124 | - return client.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder() | 123 | + public FilePresignedUrlBO getPresignedObjectUrl(String fileName) throws Exception { |
| 124 | + String uploadUrl = client.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder() | ||
| 125 | .method(Method.PUT) | 125 | .method(Method.PUT) |
| 126 | .bucket(config.getBucket()) | 126 | .bucket(config.getBucket()) |
| 127 | .object(fileName) | 127 | .object(fileName) |
| @@ -132,5 +132,6 @@ public class S3FileClient extends AbstractFileClient<S3FileClientConfig> { | @@ -132,5 +132,6 @@ public class S3FileClient extends AbstractFileClient<S3FileClientConfig> { | ||
| 132 | .expiry(10, TimeUnit.MINUTES) | 132 | .expiry(10, TimeUnit.MINUTES) |
| 133 | .build() | 133 | .build() |
| 134 | ); | 134 | ); |
| 135 | + return new FilePresignedUrlBO(uploadUrl, config.getDomain() + "/" + fileName); | ||
| 135 | } | 136 | } |
| 136 | } | 137 | } |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/FilePresignedUrlRespVO.java
| @@ -14,6 +14,9 @@ public class FilePresignedUrlRespVO { | @@ -14,6 +14,9 @@ public class FilePresignedUrlRespVO { | ||
| 14 | @Schema(description = "配置编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "11") | 14 | @Schema(description = "配置编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "11") |
| 15 | private Long configId; | 15 | private Long configId; |
| 16 | 16 | ||
| 17 | + @Schema(description = "文件上传 URL", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://www.iocoder.cn/yudao.jpg") | ||
| 18 | + private String uploadUrl; | ||
| 19 | + | ||
| 17 | @Schema(description = "文件 URL", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://www.iocoder.cn/yudao.jpg") | 20 | @Schema(description = "文件 URL", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://www.iocoder.cn/yudao.jpg") |
| 18 | private String url; | 21 | private String url; |
| 19 | 22 |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java
| @@ -6,6 +6,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; | @@ -6,6 +6,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||
| 6 | import cn.iocoder.yudao.framework.common.util.io.FileUtils; | 6 | import cn.iocoder.yudao.framework.common.util.io.FileUtils; |
| 7 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; | 7 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| 8 | import cn.iocoder.yudao.framework.file.core.client.FileClient; | 8 | import cn.iocoder.yudao.framework.file.core.client.FileClient; |
| 9 | +import cn.iocoder.yudao.framework.file.core.client.s3.FilePresignedUrlBO; | ||
| 9 | import cn.iocoder.yudao.framework.file.core.utils.FileTypeUtils; | 10 | import cn.iocoder.yudao.framework.file.core.utils.FileTypeUtils; |
| 10 | import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileCreateReqVO; | 11 | import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileCreateReqVO; |
| 11 | import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; | 12 | import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; |
| @@ -109,8 +110,8 @@ public class FileServiceImpl implements FileService { | @@ -109,8 +110,8 @@ public class FileServiceImpl implements FileService { | ||
| 109 | @Override | 110 | @Override |
| 110 | public FilePresignedUrlRespVO getFilePresignedUrl(String fileName) throws Exception { | 111 | public FilePresignedUrlRespVO getFilePresignedUrl(String fileName) throws Exception { |
| 111 | FileClient fileClient = fileConfigService.getMasterFileClient(); | 112 | FileClient fileClient = fileConfigService.getMasterFileClient(); |
| 112 | - String url = fileClient.getPresignedObjectUrl(fileName); | ||
| 113 | - return new FilePresignedUrlRespVO(fileClient.getId(), url); | 113 | + FilePresignedUrlBO bo = fileClient.getPresignedObjectUrl(fileName); |
| 114 | + return BeanUtils.toBean(bo, FilePresignedUrlRespVO.class, f -> f.setConfigId(fileClient.getId())); | ||
| 114 | } | 115 | } |
| 115 | 116 | ||
| 116 | } | 117 | } |