基础设施:增加前端直连上传文件到S3服务的功能

This commit is contained in:
owen
2024-02-04 19:44:25 +08:00
parent 7bb171512a
commit 478db1bd39
7 changed files with 142 additions and 18 deletions

View File

@ -18,11 +18,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 +40,14 @@ public interface FileClient {
*/
byte[] getContent(String path) throws Exception;
/**
* 获得文件预签名地址
*
* @param fileName 文件名称
* @return 文件预签名地址
*/
default String getPresignedObjectUrl(String fileName) throws Exception {
throw new UnsupportedOperationException("不支持的操作");
}
}

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,18 @@ public class S3FileClient extends AbstractFileClient<S3FileClientConfig> {
return IoUtil.readBytes(response);
}
@Override
public String getPresignedObjectUrl(String fileName) throws Exception {
return client.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
.method(Method.PUT)
.bucket(config.getBucket())
.object(fileName)
/**
* 过期时间秒数取值范围1秒 ~ 7天
* {@link GetPresignedObjectUrlArgs.Builder#validateExpiry(int)}
*/
.expiry(10, TimeUnit.MINUTES)
.build()
);
}
}