!865 基础设施:前端直连上传文件

Merge pull request !865 from 疯狂的世界/develop
This commit is contained in:
芋道源码
2024-02-17 11:21:06 +00:00
committed by Gitee
8 changed files with 176 additions and 18 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;
/**
* 文件客户端
*
@ -18,11 +20,11 @@ public interface FileClient {
* 上传文件
*
* @param content 文件流
* @param path 相对路径
* @param path 相对路径
* @return 完整路径,即 HTTP 访问地址
* @throws Exception 上传文件时,抛出 Exception 异常
*/
String upload(byte[] content, String path, String type) throws Exception;
String upload(byte[] content, String path, String type) throws Exception;
/**
* 删除文件
@ -40,4 +42,14 @@ public interface FileClient {
*/
byte[] getContent(String path) throws Exception;
/**
* 获得文件预签名地址
*
* @param fileName 文件名称
* @return 文件预签名地址
*/
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

@ -5,8 +5,10 @@ import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.iocoder.yudao.framework.file.core.client.AbstractFileClient;
import io.minio.*;
import io.minio.http.Method;
import java.io.ByteArrayInputStream;
import java.util.concurrent.TimeUnit;
import static cn.iocoder.yudao.framework.file.core.client.s3.S3FileClientConfig.ENDPOINT_ALIYUN;
import static cn.iocoder.yudao.framework.file.core.client.s3.S3FileClientConfig.ENDPOINT_TENCENT;
@ -117,4 +119,19 @@ public class S3FileClient extends AbstractFileClient<S3FileClientConfig> {
return IoUtil.readBytes(response);
}
@Override
public FilePresignedUrlBO getPresignedObjectUrl(String fileName) throws Exception {
String uploadUrl = client.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
.method(Method.PUT)
.bucket(config.getBucket())
.object(fileName)
/**
* 过期时间秒数取值范围1秒 ~ 7天
* {@link GetPresignedObjectUrlArgs.Builder#validateExpiry(int)}
*/
.expiry(10, TimeUnit.MINUTES)
.build()
);
return new FilePresignedUrlBO(uploadUrl, config.getDomain() + "/" + fileName);
}
}