基础设施:前端直连上传,优先使用自定义域名

This commit is contained in:
owen 2024-02-04 20:57:06 +08:00
parent 478db1bd39
commit 5804957149
5 changed files with 39 additions and 5 deletions

View File

@ -1,5 +1,7 @@
package cn.iocoder.yudao.framework.file.core.client;
import cn.iocoder.yudao.framework.file.core.client.s3.FilePresignedUrlBO;
/**
* 文件客户端
*
@ -46,7 +48,7 @@ public interface FileClient {
* @param fileName 文件名称
* @return 文件预签名地址
*/
default String getPresignedObjectUrl(String fileName) throws Exception {
default FilePresignedUrlBO getPresignedObjectUrl(String fileName) throws Exception {
throw new UnsupportedOperationException("不支持的操作");
}

View File

@ -0,0 +1,27 @@
package cn.iocoder.yudao.framework.file.core.client.s3;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 文件预签名地址 BO
*
* @author owen
*/
@AllArgsConstructor
@NoArgsConstructor
@Data
public class FilePresignedUrlBO {
/**
* 文件上传 URL用于上传
*/
private String uploadUrl;
/**
* 文件 URL用于读取下载等
*/
private String url;
}

View File

@ -120,8 +120,8 @@ public class S3FileClient extends AbstractFileClient<S3FileClientConfig> {
}
@Override
public String getPresignedObjectUrl(String fileName) throws Exception {
return client.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
public FilePresignedUrlBO getPresignedObjectUrl(String fileName) throws Exception {
String uploadUrl = client.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
.method(Method.PUT)
.bucket(config.getBucket())
.object(fileName)
@ -132,5 +132,6 @@ public class S3FileClient extends AbstractFileClient<S3FileClientConfig> {
.expiry(10, TimeUnit.MINUTES)
.build()
);
return new FilePresignedUrlBO(uploadUrl, config.getDomain() + "/" + fileName);
}
}

View File

@ -14,6 +14,9 @@ public class FilePresignedUrlRespVO {
@Schema(description = "配置编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "11")
private Long configId;
@Schema(description = "文件上传 URL", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://www.iocoder.cn/yudao.jpg")
private String uploadUrl;
@Schema(description = "文件 URL", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://www.iocoder.cn/yudao.jpg")
private String url;

View File

@ -6,6 +6,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.io.FileUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.file.core.client.FileClient;
import cn.iocoder.yudao.framework.file.core.client.s3.FilePresignedUrlBO;
import cn.iocoder.yudao.framework.file.core.utils.FileTypeUtils;
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileCreateReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO;
@ -109,8 +110,8 @@ public class FileServiceImpl implements FileService {
@Override
public FilePresignedUrlRespVO getFilePresignedUrl(String fileName) throws Exception {
FileClient fileClient = fileConfigService.getMasterFileClient();
String url = fileClient.getPresignedObjectUrl(fileName);
return new FilePresignedUrlRespVO(fileClient.getId(), url);
FilePresignedUrlBO bo = fileClient.getPresignedObjectUrl(fileName);
return BeanUtils.toBean(bo, FilePresignedUrlRespVO.class, f -> f.setConfigId(fileClient.getId()));
}
}