OSS 文件存储
# 介绍
主要功能 文件对象存储 支持选择使用不同存储方式,也可以同时使用多种存储方式。
源码地址 aizuda-oss (opens new window) 测试demo (opens new window)
# 安装
- Maven
<dependency>
<groupId>com.aizuda</groupId>
<artifactId>aizuda-oss</artifactId>
<version>1.0.1</version>
</dependency>
- Gradle
implementation 'com.aizuda:aizuda-oss:1.0.1'
# SpringBoot使用
- application.yml 配置
# 配置存储平台 ,第一位 test-minio 为默认存储平台
aizuda:
oss:
test-minio:
platform: minio
endpoint: http://xxxxxx
accessKey: xxx
secretKey: xxxxxxx
bucketName: test1
aliyun-oss:
platform: aliyun
endpoint: http://xxxxxx
accessKey: xxx
secretKey: xxxxxxx
bucketName: test
allowMediaType:
- image/gif
- application/
- Bean 方式注入,以下注入 minio3 为平台别名
@Bean
public IFileStorage minio3() {
// 注入一个自定义存储平台
OssProperty ossProperty = new OssProperty();
ossProperty.setPlatform(StoragePlatform.minio);
ossProperty.setBucketName("test3");
ossProperty.setEndpoint("http://xxxxx");
ossProperty.setAccessKey("q7RNi6elbvQ0j1ry");
ossProperty.setSecretKey("HMoKkeu0zGSvSdDGWlMDuytaRON12St9");
return new Minio(ossProperty);
}
- 测试上传 platform 存储平台(不设置使用默认)bucketName 存储桶(不设置使用默认)
// 静态方法方式调用
OSS.fileStorage(platform).bucket(bucketName).upload(fis, filename);
// 依赖注入方式调用
fileStorage.bucket(bucketName)
// 使用默认 yml 配置媒体类型
.allowMediaType(fis)
// 只允许gif图片上传,所有图片可以是 image/ 部分匹配
//.allowMediaType(fis, t -> t.startsWith("image/gif"))
.upload(fis, filename);
# IFileStorage 方法说明
属性 | 说明 |
---|---|
upload | 上传 |
delete | 删除 |
download | 下载 |
getUrl | 文件可预览地址 |
# 配置属性说明
属性 | 说明 |
---|---|
platform | 存储平台,目前支持 minio aliyun |
endpoint | 域名 |
accessKey | 访问 KEY |
secretKey | 密钥 |
bucketName | 存储空间桶名 |
connectionTimeout | 连接超时,阿里云OSS有效 |
localFilePath | 本地存储文件路径 |
allowMediaType | 允许媒体类型(集合 list 类型,支持部分匹配) |
上次更新: 2022/07/01, 09:29:12