功能介绍
该模块为应用提供文件操作能力,包含上传、下载和删除三个核心方法,另外对多种场景进行适配。支持以下服务提供商:
- 华为OBS
- 阿里OSS
- FastDfs
- minio
- 腾讯COS
- 本地存储
- 移动EOS
其中上传支持以下场景:
- 根据文件路径上传
- 根据File文件上传
- 根据MultipartFile文件上传
- 根据文件输入流上传
模块引用说明
maven配置
<dependency>
<groupId>com.kingengine.kems.sdk</groupId>
<artifactId>kems-sdk-file-core</artifactId>
</dependency>
yml配置
file:
# 选择哪种文件服务,可以填 fdfs,oss,obs,minio, cos, local
provider: obs
# 限定文件最大大小
max-size: 100000000
# 正向代理路径(选填),如果填写了正向代理路径,则会通过正向代理服务器连接文件服务器(填写规则:用户名@密码:host:port)
forward-proxy-url: xxx:xxx@xxx:xxx
# 反向代理路径(选填),如果填写了反向代理路径,上传文件的全路径则为 反向代理路径+文件服务返回的半路径,实现路径代理功能
reverse-proxy-url: https://www.oss.com
# FAST_DFS配置
fdfs:
#访问url(选填),填写后fastdfs上传文件返回的全路径为:accessUrl+文件服务返回的半路径(如果同时反向代理路径也配置了,优先使用反向代理路径+半路径)
access-url: xxx:xxx
#分组名(选填),用来指定fastdfs选择哪个分组,分组需要保证是在fastdfs配置正确的,否则上传会失败。不填会选择默认的分组名
group-name: xxx
tracker-servers: xxx
tracker-http-port: xxx
http-anti-steal-token: true
http-secret-key: kingengine
charset: UTF-8
connect-timeout-in-seconds: 60
network-timeout-in-seconds: 60
pool:
max-total: 10
min-idle: 2
# OSS配置
oss:
endpoint: xxx
access-key-id:xxx
access-key-secret: xxx
bucket-name:xxx
# OBS配置
obs:
endpoint: xxx
access-key-id: xxx
access-key-secret: xxx
bucket-name: xxx
# minio配置
minio:
endpoint: http://10.202.61.123:9000
access-key: minioadmin
access-secret: minioadmin
bucket-name: kems
# cos配置
cos:
app-id: 1312290446
secret-id: AKID2uVptgCRKxJOCixvb08zYqqa3FBpCI6d
secret-key: HI2UgaDYcPu97oDLCm45pHSMzKPDbiZ9
region: ap-guangzhou
bucket-name: kssp #桶名称
dir-name: folder ##路径名称
pool:
max-total: 8 #线程池大小
# local配置
local:
dir-name: /kingdom/data #文件存储目录
url: http://139.159.200.182/data # nginx或tomcat 静态文件访问路径
# 移动eos配置
eos:
endpoint: http://eos-guangzhou-1.cmecloud.cn
access-key-id: 0GNGHRF8AYW1QDNYT9NT
access-key-secret: iC9zR0SFqqwKtu8FAaSwl76EI72eQUw9y1DYrkn2
bucket-name: kems1
配置说明参阅
kems-sdk-file-core/src/main/resources/META-INF/spring-configuration-metadata.json
简单示例
文件服务配置
file:
provider: oss
max-size: 100000000
forward-proxy-url: xxx:xxx@xxx:xxx
reverse-proxy-url: https://www.oss.com
oss:
endpoint: xxx
access-key-id: xxx
access-key-secret: xxx
bucket-name: xxx
业务操作
文件上传
@Resource
private FileCore fileCore;
@Test
public void uploadWithFilePath() throws Exception {
String filePath = "C:/Users/JZYQ/Desktop/test.txt";
String dirPath = "ccs/abc";
FileInfoVO fileInfoVO = fileCore.upload(filePath,dirPath,null);
System.out.println(fileInfoVO);
}
文件下载
@Resource
private FileCore fileCore;
@Test
public void downLoadByFastDfs() throws Exception {
String path = "KEDFSFile/M00/00/E0/Cso9QmAYqwuALxbXAAAADKoejAk834.txt";
DownLoadFileVO downLoadFileVO = fileCore.downloadFile(path);
}
文件删除
@Test
public void deleteFile() throws Exception {
String path1 = "ccs/abc/test.txt";
String path = "KEDFSFile/M00/00/D9/Cso9QmATapqACZE9AAAADKoejAk357.txt";
fileCore.deleteFile(path);
怎么扩展提供商?
在
sdk
基础上进行扩展,能更方便的获得配套插件能力
- 依赖引入
<dependency>
<groupId>com.kingengine.kems.sdk</groupId>
<artifactId>kems-sdk-file-core</artifactId>
</dependency>
实现接口
com.kingengine.kems.sdk.file.service.FileStrategyInterface
调整配置文件
file:
# 自己扩展的提供商名字
provider: xxx