Pre Merge pull request !97 from dazer007/ruoyi-fix-small-fix

This commit is contained in:
dazer007 2022-01-20 03:38:45 +00:00 committed by Gitee
commit bbc3d6df1c
11 changed files with 302 additions and 34 deletions

View File

@ -2,7 +2,9 @@ package com.ruoyi.system.api;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.core.constant.ServiceNameConstants; import com.ruoyi.common.core.constant.ServiceNameConstants;
@ -12,7 +14,7 @@ import com.ruoyi.system.api.factory.RemoteFileFallbackFactory;
/** /**
* 文件服务 * 文件服务
* *
* @author ruoyi * @author ruoyi
*/ */
@FeignClient(contextId = "remoteFileService", value = ServiceNameConstants.FILE_SERVICE, fallbackFactory = RemoteFileFallbackFactory.class) @FeignClient(contextId = "remoteFileService", value = ServiceNameConstants.FILE_SERVICE, fallbackFactory = RemoteFileFallbackFactory.class)
@ -26,4 +28,11 @@ public interface RemoteFileService
*/ */
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public R<SysFile> upload(@RequestPart(value = "file") MultipartFile file); public R<SysFile> upload(@RequestPart(value = "file") MultipartFile file);
/**
* 临时安全凭证获取加签的url; 根据输入的url,获取带有临时安全凭证的url
* @param fileUrl 待加签的url
*/
@GetMapping("getPresignedUrl")
R<String> getPresignedUrl(@RequestParam(value = "fileUrl") String fileUrl);
} }

View File

@ -11,7 +11,7 @@ import com.ruoyi.system.api.domain.SysFile;
/** /**
* 文件服务降级处理 * 文件服务降级处理
* *
* @author ruoyi * @author ruoyi
*/ */
@Component @Component
@ -30,6 +30,11 @@ public class RemoteFileFallbackFactory implements FallbackFactory<RemoteFileServ
{ {
return R.fail("上传文件失败:" + throwable.getMessage()); return R.fail("上传文件失败:" + throwable.getMessage());
} }
@Override
public R<String> getPresignedUrl(String fileUrl) {
return R.fail("文件加签失败:" + throwable.getMessage());
}
}; };
} }
} }

View File

@ -16,56 +16,62 @@
</description> </description>
<dependencies> <dependencies>
<!-- SpringCloud Alibaba Nacos --> <!-- SpringCloud Alibaba Nacos -->
<dependency> <dependency>
<groupId>com.alibaba.cloud</groupId> <groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency> </dependency>
<!-- SpringCloud Alibaba Nacos Config --> <!-- SpringCloud Alibaba Nacos Config -->
<dependency> <dependency>
<groupId>com.alibaba.cloud</groupId> <groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency> </dependency>
<!-- SpringCloud Alibaba Sentinel --> <!-- SpringCloud Alibaba Sentinel -->
<dependency> <dependency>
<groupId>com.alibaba.cloud</groupId> <groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency> </dependency>
<!-- SpringBoot Actuator --> <!-- SpringBoot Actuator -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId> <artifactId>spring-boot-starter-actuator</artifactId>
</dependency> </dependency>
<!-- FastDFS --> <!-- FastDFS -->
<dependency> <dependency>
<groupId>com.github.tobato</groupId> <groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId> <artifactId>fastdfs-client</artifactId>
</dependency> </dependency>
<!-- FastDFS 主要提供 【 ProtoCommon.getToken】 这个一个方法 -->
<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27.0.0</version>
</dependency>
<!-- Minio --> <!-- Minio -->
<dependency> <dependency>
<groupId>io.minio</groupId> <groupId>io.minio</groupId>
<artifactId>minio</artifactId> <artifactId>minio</artifactId>
<version>${minio.version}</version> <version>${minio.version}</version>
</dependency> </dependency>
<!-- RuoYi Api System --> <!-- RuoYi Api System -->
<dependency> <dependency>
<groupId>com.ruoyi</groupId> <groupId>com.ruoyi</groupId>
<artifactId>ruoyi-api-system</artifactId> <artifactId>ruoyi-api-system</artifactId>
</dependency> </dependency>
<!-- RuoYi Common Swagger --> <!-- RuoYi Common Swagger -->
<dependency> <dependency>
<groupId>com.ruoyi</groupId> <groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common-swagger</artifactId> <artifactId>ruoyi-common-swagger</artifactId>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
@ -84,5 +90,5 @@
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
</project> </project>

View File

@ -0,0 +1,53 @@
package com.ruoyi.file.config;
import com.github.tobato.fastdfs.FdfsClientConstants;
import com.github.tobato.fastdfs.domain.conn.PooledConnectionFactory;
import com.ruoyi.file.service.FastDfsSysFileServiceImpl;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
/**
* 阿里 fastdfs 配置
* @author dazer
* @see FastDfsSysFileServiceImpl
* FastDFS配置 其他参数见{@link PooledConnectionFactory}
*
* 使用: Docker部署FastDFS附示例代码 https://www.cnblogs.com/cao-lei/p/13470695.html
* github 地址https://github.com/tobato/FastDFS_Client
*/
@RefreshScope
@Configuration
@ConfigurationProperties(
prefix = FdfsClientConstants.ROOT_CONFIG_PREFIX
)
public class FastDfsConfig {
/**
* 文件对外访问域名or ip
* FastDFS配置 其他参数见{@link PooledConnectionFactory}
* //@Value("${fdfs.domain}")
*/
private String domain;
/**
* 生成防盗链token的加密key注意保密
* fastdfs 内置的功能需要和 FastDFS etc/fdfs/http.conf http.anti_steal.secret_key保持一致
*/
private String tokenSecretKey;
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
public String getTokenSecretKey() {
return tokenSecretKey;
}
public void setTokenSecretKey(String tokenSecretKey) {
this.tokenSecretKey = tokenSecretKey;
}
}

View File

@ -1,6 +1,7 @@
package com.ruoyi.file.config; package com.ruoyi.file.config;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import io.minio.MinioClient; import io.minio.MinioClient;
@ -10,6 +11,7 @@ import io.minio.MinioClient;
* *
* @author ruoyi * @author ruoyi
*/ */
@RefreshScope
@Configuration @Configuration
@ConfigurationProperties(prefix = "minio") @ConfigurationProperties(prefix = "minio")
public class MinioConfig public class MinioConfig
@ -34,6 +36,30 @@ public class MinioConfig
*/ */
private String bucketName; private String bucketName;
/**
* 访问域名; url经常是内网地址外部访问用域名或者外网ip
* eg: https://yq666.bj.gov.cn/appt-file
*
* 注意minio要配置 Bucket Policy Bucket ==> Edit Bucket
* 1Read Only
* 2Write Only
* 3Read and Write
* 4不做任何分配 必须要加签之后才能访问
*/
private String domain;
/**
* 过期时间
* 文档MinIO STS快速入门指南 http://docs.minio.org.cn/docs/master/minio-sts-quickstart-guide
* 文档适用于与Amazon S3兼容的云存储的MinIO Java SDK: API文档: Presigned操作: presignedGetObject: http://docs.minio.org.cn/docs/master/java-client-quickstart-guide
* 默认7天单位秒
* 1小时3600 = 60 * 60 * 1
* 24小时1天86400 = 60 * 60 * 24
* 7天604800 = 86400 * 7
* -1 就永不过期原样返回url
*/
private Integer expiryDuration = 86400;
public String getUrl() public String getUrl()
{ {
return url; return url;
@ -74,6 +100,31 @@ public class MinioConfig
this.bucketName = bucketName; this.bucketName = bucketName;
} }
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
public Integer getExpiryDuration() {
if (expiryDuration == null) {
// 默认一个小时, 3600秒
expiryDuration = 86400;
}
if (expiryDuration < 1L && expiryDuration != -1) {
// 最小1秒
// 如果要永不过期就不要调用 -1 直接原样返回
expiryDuration = 1;
}
return expiryDuration;
}
public void setExpiryDuration(Integer expiryDuration) {
this.expiryDuration = expiryDuration;
}
@Bean @Bean
public MinioClient getMinioClient() public MinioClient getMinioClient()
{ {

View File

@ -2,6 +2,7 @@ package com.ruoyi.file.config;
import java.io.File; import java.io.File;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
@ -9,9 +10,10 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/** /**
* 通用映射配置 * 通用映射配置
* *
* @author ruoyi * @author ruoyi
*/ */
@RefreshScope
@Configuration @Configuration
public class ResourcesConfig implements WebMvcConfigurer public class ResourcesConfig implements WebMvcConfigurer
{ {
@ -34,7 +36,7 @@ public class ResourcesConfig implements WebMvcConfigurer
registry.addResourceHandler(localFilePrefix + "/**") registry.addResourceHandler(localFilePrefix + "/**")
.addResourceLocations("file:" + localFilePath + File.separator); .addResourceLocations("file:" + localFilePath + File.separator);
} }
/** /**
* 开启跨域 * 开启跨域
*/ */
@ -47,4 +49,4 @@ public class ResourcesConfig implements WebMvcConfigurer
// 设置允许的方法 // 设置允许的方法
.allowedMethods("GET"); .allowedMethods("GET");
} }
} }

View File

@ -1,9 +1,13 @@
package com.ruoyi.file.controller; package com.ruoyi.file.controller;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.core.domain.R; import com.ruoyi.common.core.domain.R;
@ -13,7 +17,7 @@ import com.ruoyi.system.api.domain.SysFile;
/** /**
* 文件请求处理 * 文件请求处理
* *
* @author ruoyi * @author ruoyi
*/ */
@RestController @RestController
@ -45,4 +49,19 @@ public class SysFileController
return R.fail(e.getMessage()); return R.fail(e.getMessage());
} }
} }
}
/**
* 对访问url进行签名加密 别名 临时安全凭证
* 兼容 AWS Security Token Service (STS) 的联合身份临时安全凭证 (federation token) 更多详细信息请查阅
* aliyun oss 实例: http://react-yuebaoxiao-pro.oss-cn-shanghai.aliyuncs.com/dev/upload/default/20210719-23d31398-4849-408d-8775-a5b668ccafc3.jpeg?Expires=1626736182&OSSAccessKeyId=LTAI4GDQSbwgmbsRxxbDXnKT&Signature=P3w3%2FIpEnZEUhYku6scOos4p54A%3D
* minio 示例: https://yq666.bj.gov.cn/appt-file/dev/default/2021/07/19/5fe1478b-969c-4b6e-9cc0-742412dc3128.jpeg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=D99KGE6ZTQXSATTJWU24%2F20210719%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20210719T112025Z&X-Amz-Expires=432000&X-Amz-SignedHeaders=host&X-Amz-Signature=e45171d0885f006ee1de43cec9d88963e2b55c6e671740ae5695410ba16770c5
* ---------------------------------------------------
* 说明文件服务器一般默认是不加延签参数就可以访问要让验签看到效果一般都需要在 对应文件服务器 bucket 上面做访问策略的配置
*/
@ApiOperation(value = "临时安全凭证、获取加签的url", notes = "根据输入的url,获取带有临时安全凭证的url")
@GetMapping("getPresignedUrl")
public R<String> getPresignedUrl(
@ApiParam("需要访问的url,字段名:fileUrl,必填;不要带有?后面的参数") @RequestParam(value = "fileUrl") String fileUrl) {
return R.ok(sysFileService.presignedUrl(fileUrl), "获取成功");
}
}

View File

@ -1,8 +1,12 @@
package com.ruoyi.file.service; package com.ruoyi.file.service;
import com.ruoyi.common.core.constant.HttpStatus;
import com.ruoyi.common.core.exception.file.FileException;
import com.ruoyi.file.config.FastDfsConfig;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.csource.fastdfs.ProtoCommon;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import com.github.tobato.fastdfs.domain.fdfs.StorePath; import com.github.tobato.fastdfs.domain.fdfs.StorePath;
@ -10,24 +14,21 @@ import com.github.tobato.fastdfs.service.FastFileStorageClient;
/** /**
* FastDFS 文件存储 * FastDFS 文件存储
* *
* @author ruoyi * @author ruoyi
*/ */
@Service @Service
public class FastDfsSysFileServiceImpl implements ISysFileService public class FastDfsSysFileServiceImpl implements ISysFileService
{ {
/** @Autowired
* 域名或本机访问地址 private FastDfsConfig fastDfsConfig;
*/
@Value("${fdfs.domain}")
public String domain;
@Autowired @Autowired
private FastFileStorageClient storageClient; private FastFileStorageClient storageClient;
/** /**
* FastDfs文件上传接口 * FastDfs文件上传接口
* *
* @param file 上传的文件 * @param file 上传的文件
* @return 访问地址 * @return 访问地址
* @throws Exception * @throws Exception
@ -37,6 +38,33 @@ public class FastDfsSysFileServiceImpl implements ISysFileService
{ {
StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
FilenameUtils.getExtension(file.getOriginalFilename()), null); FilenameUtils.getExtension(file.getOriginalFilename()), null);
return domain + "/" + storePath.getFullPath(); // 形如 http://47.99.175.191:8888/group1/M00/00/00/rBzzjWD-ec2ADLS9AAJiu1rRenk51.jpeg
return fastDfsConfig.getDomain() + "/" + storePath.getFullPath();
}
@Override
public String presignedUrl(String fileUrl) {
if (StringUtils.isBlank(fastDfsConfig.getTokenSecretKey())) {
throw new FileException(HttpStatus.ERROR + "", new String[] {"防盗链生成token的密钥为空请检查tokenSecretKey"});
}
String signKey = "?token=";
if (fileUrl.contains(signKey)) {
return fileUrl;
}
String tokenSecretKey = fastDfsConfig.getTokenSecretKey();
StorePath storePath = StorePath.parseFromUrl(fileUrl);
String keyPath = storePath.getPath();
//时间戳 单位为秒
int ts = (int) (System.currentTimeMillis() / 1000);
String token;
try {
token = ProtoCommon.getToken(keyPath, ts, tokenSecretKey);
} catch (Exception e) {
throw new FileException(HttpStatus.ERROR + "", new String[] {"FastDFS获取token异常"});
}
// 形如 http://47.99.175.191:8888/group1/M00/00/00/rBzzjWD-ec2ADLS9AAJiu1rRenk51.jpeg
return fastDfsConfig.getDomain() + "/" + storePath.getFullPath() + "?token=" + token + "&ts=" + ts;
} }
} }

View File

@ -4,17 +4,34 @@ import org.springframework.web.multipart.MultipartFile;
/** /**
* 文件上传接口 * 文件上传接口
* *
* @author ruoyi * @author ruoyi
*/ */
public interface ISysFileService public interface ISysFileService
{ {
/** /**
* 文件上传接口 * 文件上传接口
* *
* @param file 上传的文件 * @param file 上传的文件
* @return 访问地址 * @return 访问地址
* @throws Exception * @throws Exception
*/ */
public String uploadFile(MultipartFile file) throws Exception; public String uploadFile(MultipartFile file) throws Exception;
/**
* 对访问url进行签名加密 别名 临时安全凭证
* 兼容 AWS Security Token Service (STS) 的联合身份临时安全凭证 (federation token) 更多详细信息请查阅
*
* 1aliyun oss 叫做STS服务 临时安全令牌Security Token ServiceSTS 说明https://help.aliyun.com/document_detail/28761.html?spm=a2c4g.11186623.6.880.22bd2fe5pL1d39
* 2minio 叫做resignedGetObject 临时安全令牌Security Token ServiceSTS; Presigned presignedGetObject 预签
* http://docs.minio.org.cn/docs/master/minio-sts-quickstart-guide
* minio SDKS Java Client API参考文档 http://docs.minio.org.cn/docs/master/java-client-api-reference
* 3qiniu 七牛云存储 下载凭证(如果Bucket设置成私有必须要有 下载凭证)路径对象存储==使用指南===安全机制=== 下载凭证 https://developer.qiniu.com/kodo/1202/download-token
* https://developer.qiniu.com/kodo/5914/s3-compatible-sts
* 4腾讯 临时密钥临时访问凭证 GetFederationToken 临时密钥生成及使用指引 https://cloud.tencent.com/document/product/436/14048?from=10680
* 5fastdfs 防掉链 前提需要在 fastdfs上面配置 https://www.cnblogs.com/xiaolinstudy/p/9341779.html
* @param fileUrl 文件访问地址,全路径或者不是全路径都可以
* @return 返回签名后的url
*/
String presignedUrl(String fileUrl);
} }

View File

@ -8,7 +8,7 @@ import com.ruoyi.file.utils.FileUploadUtils;
/** /**
* 本地文件存储 * 本地文件存储
* *
* @author ruoyi * @author ruoyi
*/ */
@Primary @Primary
@ -26,7 +26,7 @@ public class LocalSysFileServiceImpl implements ISysFileService
*/ */
@Value("${file.domain}") @Value("${file.domain}")
public String domain; public String domain;
/** /**
* 上传文件存储在本地的根路径 * 上传文件存储在本地的根路径
*/ */
@ -35,7 +35,7 @@ public class LocalSysFileServiceImpl implements ISysFileService
/** /**
* 本地文件上传接口 * 本地文件上传接口
* *
* @param file 上传的文件 * @param file 上传的文件
* @return 访问地址 * @return 访问地址
* @throws Exception * @throws Exception
@ -47,4 +47,9 @@ public class LocalSysFileServiceImpl implements ISysFileService
String url = domain + localFilePrefix + name; String url = domain + localFilePrefix + name;
return url; return url;
} }
@Override
public String presignedUrl(String fileUrl) {
return fileUrl;
}
} }

View File

@ -1,5 +1,8 @@
package com.ruoyi.file.service; package com.ruoyi.file.service;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.errors.*;
import io.minio.http.Method;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@ -8,10 +11,24 @@ import com.ruoyi.file.utils.FileUploadUtils;
import io.minio.MinioClient; import io.minio.MinioClient;
import io.minio.PutObjectArgs; import io.minio.PutObjectArgs;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeUnit;
/** /**
* Minio 文件存储 * Minio 文件存储
* *
* @author ruoyi * @author ruoyi
* 官网博客 http://docs.minio.org.cn/docs/master/java-client-quickstart-guidec
* 在springboot中使用Minio8 https://springboot.io/t/topic/3109
* 1上传操作putObject
* 2删除操作removeObject
* ==================
* 3预签(Presigned)操作设置访问过期时间支持对url进行鉴权sts临时授权presignedGetObject
* presignedGetObject(String bucketName, String objectName, Integer expires)
* http://docs.minio.org.cn/docs/master/java-client-api-reference#presignedGetObject
* MinIO STS快速入门指南 http://docs.minio.org.cn/docs/master/minio-sts-quickstart-guide
*/ */
@Service @Service
public class MinioSysFileServiceImpl implements ISysFileService public class MinioSysFileServiceImpl implements ISysFileService
@ -24,7 +41,7 @@ public class MinioSysFileServiceImpl implements ISysFileService
/** /**
* 本地文件上传接口 * 本地文件上传接口
* *
* @param file 上传的文件 * @param file 上传的文件
* @return 访问地址 * @return 访问地址
* @throws Exception * @throws Exception
@ -42,4 +59,60 @@ public class MinioSysFileServiceImpl implements ISysFileService
client.putObject(args); client.putObject(args);
return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName; return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
} }
/**
* 给原始的URL add 过期时间
* [STS 临时授权]
* http://docs.minio.org.cn/docs/master/minio-sts-quickstart-guide
* minio SDKS Java Client API参考文档 http://docs.minio.org.cn/docs/master/java-client-api-reference
* Presigned presignedGetObject 预签
* 示例https://xxxx.xxx.gov.cn/file/2021/08/06/cd9dfbaa-8563-423a-bc3d-d0b15e781931.pdf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=D99KGE6ZTQXSATTJWU24%2F20210809%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20210809T075702Z&X-Amz-Expires=86400&X-Amz-SignedHeaders=host&X-Amz-Signature=198c76edc57998f4dde72124952b43f0066c762356e485dd44d21df9cc7dad78
*/
@Override
public String presignedUrl(String fileUrl) {
if (minioConfig.getExpiryDuration() == -1) {
return fileUrl;
}
String signKey = "?X-Amz-Algorithm=";
if (fileUrl.contains(signKey)) {
return fileUrl;
}
String objectName = this.getStorePath(fileUrl);
GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder().
bucket(minioConfig.getBucketName()).
method(Method.GET).
object(objectName).
expiry(minioConfig.getExpiryDuration(), TimeUnit.SECONDS).build();
String presignedObjectUrl = null;
try {
presignedObjectUrl = client.getPresignedObjectUrl(args);
String basePrivateUrl = minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/";
presignedObjectUrl = presignedObjectUrl.replace(basePrivateUrl, "");
} catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidKeyException | InvalidResponseException | IOException | NoSuchAlgorithmException | XmlParserException | ServerException e) {
e.printStackTrace();
presignedObjectUrl = fileUrl;
}
return minioConfig.getDomain() + "/" + minioConfig.getBucketName() + "/" + presignedObjectUrl;
}
/**
* 转换url为原始的key
*
* @param filePath https://yq666.bj.gov.cn/appt-file/dev/default/2021/07/18/f4243eb2-06a1-4304-bdfc-e2964b8721bb.jpeg
* @return dev/default/2021/07/18/f4243eb2-06a1-4304-bdfc-e2964b8721bb.jpeg
*/
private String getStorePath(String filePath) {
String oldPath = filePath;
// 处理方式1
String publicPath3 = minioConfig.getDomain() + "/" + minioConfig.getBucketName() + "/";
filePath = filePath.replace(publicPath3, "");
if (oldPath.equals(filePath)) {
// 处理方式2
String publicPath4 = minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/";
filePath = filePath.replace(publicPath4, "");
}
return filePath;
}
} }