x-file-storage.dromara.org | x-file-storage.xuyanwu.cn | spring-file-storage.xuyanwu.cn
One line of code stores files locally, FTP, SFTP, WebDAV, Alibaba Cloud OSS, Huawei Cloud OBS, Qiniu Cloud Kodo, Tencent Cloud COS, Baidu Cloud BOS, Youpai Cloud USS, MinIO, Amazon S3, Google Cloud Storage, FastDFS, Azure Blob Storage, Cloudflare R2, Kingsoft Cloud KS3, Meituan Cloud MSS, JD Cloud OSS, Tianyi Cloud OOS, Mobile Cloud EOS, Woyun OSS, NetEase Shufan NOS, Ucloud US3, Qingyun QingStor, Ping An Cloud OBS, Shouyun OSS, IBM COS, and other storage platforms compatible with the S3 protocol. View all supported storage platforms
After connecting to Alist through WebDAV, you can use common storage services such as Baidu Cloud Disk, Tianyi Cloud Disk, Alibaba Cloud Disk, and Thunder Cloud Disk to view the storage platforms supported by Alist.
? Supports migrating files between different storage platforms, see migration files for details
GitHub: https://github.com/dromara/x-file-storage
Gitee: https://gitee.com/dromara/x-file-storage
Here is a brief update record, view detailed update record
2.2.1
Fixed the problem of hash calculation errors in some cases and the problem of Qiniu Cloud Kodo pre-signed URL being unusable. Update record
2.2.0
fixes a large number of problems, adds new files, lists files, reconstructs pre-signed URLs, supports client upload, download, delete and other operations, adds Solon plug-in, optimizes functions such as manual multi-part upload, check the update record for details
2.1.0
fixes a large number of issues, adds storage platforms FastDFS and Azure Blob Storage, and adds functions such as copying and moving (renaming) files, manual multipart upload (resumable upload), and calculation of hashes. Please check the update record for details.
2.0.0
is donated to the dromara open source community, changes the project name, package name, optimizes the project structure, supports Metadata metadata, etc. You need to pay attention to upgrading from the old version. Please check the update record for details.
Click Quick Start to view how to use all storage platforms!
Here we take Alibaba Cloud OSS as an example. pom.xml
is introduced into this project. The default here is the SpringBoot
environment. Solon
environment reference is used in Solon. The other environment references are used separately from SpringBoot.
<!-- 引入本项目 -->
< dependency >
< groupId >org.dromara.x-file-storage</ groupId >
< artifactId >x-file-storage-spring</ artifactId >
< version >2.2.1</ version >
</ dependency >
<!-- 引入 阿里云 OSS SDK,如果使用其它存储平台,就引入对应的 SDK -->
< dependency >
< groupId >com.aliyun.oss</ groupId >
< artifactId >aliyun-sdk-oss</ artifactId >
< version >3.16.1</ version >
</ dependency >
Add the following basic configuration to the application.yml
configuration file
Regarding the differences between configuration files and various paths (paths) in FileInfo, please refer to the Frequently Asked Questions
dromara :
x-file-storage : #文件存储配置
default-platform : aliyun-oss-1 #默认使用的存储平台
aliyun-oss :
- platform : aliyun-oss-1 # 存储平台标识
enable-storage : true # 启用存储
access-key : ??
secret-key : ??
end-point : ??
bucket-name : ??
domain : ?? # 访问域名,注意“/”结尾,例如:https://abc.oss-cn-shanghai.aliyuncs.com/
base-path : test/ # 基础路径
Add the @EnableFileStorage
annotation to the startup class
@ EnableFileStorage
@ SpringBootApplication
public class SpringFileStorageTestApplication {
public static void main ( String [] args ) {
SpringApplication . run ( SpringFileStorageTestApplication . class , args );
}
}
Supports File, MultipartFile, UploadedFile, byte[], InputStream, URL, URI, String, HttpServletRequest, and large files will be automatically uploaded in pieces. If you want to support more methods, please read the file adapter chapter
@ RestController
public class FileDetailController {
@ Autowired
private FileStorageService fileStorageService ; //注入实列
/**
* 上传文件
*/
@ PostMapping ( "/upload" )
public FileInfo upload ( MultipartFile file ) {
//只需要这一行代码即可上传成功
return fileStorageService . of ( file ). upload ();
}
/**
* 上传文件,成功返回文件 url
*/
@ PostMapping ( "/upload2" )
public String upload2 ( MultipartFile file ) {
FileInfo fileInfo = fileStorageService . of ( file )
. setPath ( "upload/" ) //保存到相对路径下,为了方便管理,不需要可以不写
. setSaveFilename ( "image.jpg" ) //设置保存的文件名,不需要可以不写,会随机生成
. setObjectId ( "0" ) //关联对象id,为了方便管理,不需要可以不写
. setObjectType ( "0" ) //关联对象类型,为了方便管理,不需要可以不写
. putAttr ( "role" , "admin" ) //保存一些属性,可以在切面、保存上传记录、自定义存储平台等地方获取使用,不需要可以不写
. upload (); //将文件上传到对应地方
return fileInfo == null ? "上传失败!" : fileInfo . getUrl ();
}
/**
* 上传图片,成功返回文件信息
* 图片处理使用的是 https://github.com/coobird/thumbnailator
*/
@ PostMapping ( "/upload-image" )
public FileInfo uploadImage ( MultipartFile file ) {
return fileStorageService . of ( file )
. image ( img -> img . size ( 1000 , 1000 )) //将图片大小调整到 1000*1000
. thumbnail ( th -> th . size ( 200 , 200 )) //再生成一张 200*200 的缩略图
. upload ();
}
/**
* 上传文件到指定存储平台,成功返回文件信息
*/
@ PostMapping ( "/upload-platform" )
public FileInfo uploadPlatform ( MultipartFile file ) {
return fileStorageService . of ( file )
. setPlatform ( "aliyun-oss-1" ) //使用指定的存储平台
. upload ();
}
/**
* 直接读取 HttpServletRequest 中的文件进行上传,成功返回文件信息
* 使用这种方式有些注意事项,请查看文档 基础功能-上传 章节
*/
@ PostMapping ( "/upload-request" )
public FileInfo uploadPlatform ( HttpServletRequest request ) {
return fileStorageService . of ( request ). upload ();
}
}
//手动构造文件信息,可用于其它操作
FileInfo fileInfo = new FileInfo ()
. setPlatform ( "huawei-obs-1" )
. setBasePath ( "test/" )
. setPath ( "aa/" )
. setFilename ( "image.png" )
. setThFilename ( "image.png.min.jpg" );
//文件是否存在
boolean exists = fileStorageService . exists ( fileInfo );
//下载
byte [] bytes = fileStorageService . download ( fileInfo ). bytes ();
//删除
fileStorageService . delete ( fileInfo );
//其它更多操作
If you save the file record to the database, you can also operate based on the URL more conveniently. For details, please read the chapter on saving upload records.
//直接从数据库中获取 FileInfo 对象,更加方便执行其它操作
FileInfo fileInfo = fileStorageService . getFileInfoByUrl ( "https://abc.def.com/test/aa/image.png" );
//文件是否存在
boolean exists = fileStorageService . exists ( "https://abc.def.com/test/aa/image.png" );
//下载
byte [] bytes = fileStorageService . download ( "https://abc.def.com/test/aa/image.png" ). bytes ();
//删除
fileStorageService . delete ( "https://abc.def.com/test/aa/image.png" );
//其它更多操作
Click Quick Start to view how to use all storage platforms!
The source code of X File Storage is divided into two branches, with the following functions:
branch | effect |
---|---|
main | The main branch, the branch used by the release version, is consistent with the jar submitted by the central library and does not receive any PR or modification. |
dev | Development branch, accept modifications or PR |
When submitting problem feedback, please indicate the X File Storage version, related dependent library versions, configuration parameters and problem code.
Gitee issue
GitHub issue
Anyone is welcome to contribute to X File Storage and contribute code. For ease of use and maintainability, the PR (pull request) that needs to be submitted must comply with some specifications. The specifications are as follows:
X File Storage thanks everyone for your trust and support. If you have used X File Storage in your project, I hope you can leave your company or organization information (company or organization name, official website address, display logo picture)
Your company information will be displayed on the project official website:
x-file-storage.dromara.org
x-file-storage.xuyanwu.cn
spring-file-storage.xuyanwu.cn
Register on Gitee Register on GitHub
If you think this project is good, you can click a star or donate to treat the author to eat spicy strips~. If you don’t want to reward, you can scan the last code with Alipay to receive a red envelope. I would like to express my gratitude ^_^
Or click the link below, scroll to the bottom of the page and click "Donate"
Donate on Gitee
Scan the QR code above to donate 99 yuan, and send the screenshot to me QQ1171736840
to join the VIP communication group (if it exceeds one year, you need to donate again, otherwise it will be cleared)
You can also click to add a free communication group to communicate together