Skip to content

Commit

Permalink
🎉 优化oss封装
Browse files Browse the repository at this point in the history
  • Loading branch information
smallchill committed May 22, 2019
1 parent 56d1089 commit a82f84d
Show file tree
Hide file tree
Showing 10 changed files with 386 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import lombok.SneakyThrows;
import org.springblade.core.minio.enums.PolicyType;
import org.springblade.core.minio.model.MinioItem;
import org.springblade.core.minio.props.MinioProperties;
import org.springblade.core.minio.rule.IMinioRule;
import org.springblade.core.tool.utils.StringPool;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
Expand All @@ -51,6 +53,20 @@ public class MinioTemplate {
*/
private IMinioRule minioRule;

/**
* 配置类
*/
private MinioProperties minioProperties;

/**
* 根据规则生成存储桶名称规则
*
* @return String
*/
private String getBucketName() {
return getBucketName(minioProperties.getBucketName());
}

/**
* 根据规则生成存储桶名称规则
*
Expand All @@ -70,9 +86,18 @@ private String getBucketName(String bucketName) {
public void makeBucket(String bucketName) {
if (!client.bucketExists(getBucketName(bucketName))) {
client.makeBucket(getBucketName(bucketName));
client.setBucketPolicy(getBucketName(bucketName), getPolicyType(getBucketName(bucketName), PolicyType.READ));
}
}

/**
* 获取单个 存储桶
*/
@SneakyThrows
public Bucket getBucket() {
return getBucket(getBucketName());
}

/**
* 获取单个 存储桶
*
Expand Down Expand Up @@ -181,6 +206,17 @@ public void copyObject(String bucketName, String objectName, String destBucketNa
client.copyObject(getBucketName(bucketName), objectName, getBucketName(destBucketName), destObjectName);
}

/**
* 获取文件信息
*
* @param objectName 存储桶对象名称
* @return InputStream
*/
@SneakyThrows
public ObjectStat statObject(String objectName) {
return statObject(minioProperties.getBucketName(), objectName);
}

/**
* 获取文件信息
*
Expand All @@ -193,6 +229,17 @@ public ObjectStat statObject(String bucketName, String objectName) {
return client.statObject(getBucketName(bucketName), objectName);
}

/**
* 获取文件流
*
* @param objectName 存储桶对象名称
* @return InputStream
*/
@SneakyThrows
public InputStream getObject(String objectName) {
return getObject(minioProperties.getBucketName(), objectName);
}

/**
* 获取文件流
*
Expand All @@ -205,6 +252,16 @@ public InputStream getObject(String bucketName, String objectName) {
return client.getObject(getBucketName(bucketName), objectName);
}

/**
* 获取存储桶下的对象集合
*
* @return List<MinioItem>
*/
@SneakyThrows
public List<MinioItem> listObjects() {
return listObjects(minioProperties.getBucketName());
}

/**
* 获取存储桶下的对象集合
*
Expand Down Expand Up @@ -256,6 +313,17 @@ private List<MinioItem> buildItems(Iterable<Result<Item>> results) {
return items;
}

/**
* 获取文件外链
*
* @param objectName 存储桶对象名称
* @return String
*/
@SneakyThrows
public String getObjectUrl(String objectName) {
return getObjectUrl(minioProperties.getBucketName(), objectName);
}

/**
* 获取文件外链
*
Expand Down Expand Up @@ -283,14 +351,60 @@ public String getObjectUrl(String bucketName, String objectName, Integer expires
}

/**
* 上传文件
* 获取文件地址
*
* @param objectName 存储桶对象名称
* @return String
*/
@SneakyThrows
public String getObjectLink(String objectName) {
return minioProperties.getEndpoint().concat(StringPool.SLASH).concat(getBucketName()).concat(StringPool.SLASH).concat(objectName);
}

/**
* 获取文件地址
*
* @param bucketName 存储桶名称
* @param file 上传文件类
* @param objectName 存储桶对象名称
* @return String
*/
@SneakyThrows
public void putObject(String bucketName, MultipartFile file) {
putObject(getBucketName(bucketName), minioRule.fileName(file), file.getInputStream(), (long) file.getInputStream().available(), "application/octet-stream");
public String getObjectLink(String bucketName, String objectName) {
return minioProperties.getEndpoint().concat(StringPool.SLASH).concat(getBucketName(bucketName)).concat(StringPool.SLASH).concat(objectName);
}

/**
* 上传文件
*
* @param file 上传文件类
*/
@SneakyThrows
public void putObject(MultipartFile file) {
putObject(minioProperties.getBucketName(), file.getOriginalFilename(), file);
}

/**
* 上传文件,自动生成文件名
*
* @param file 上传文件类
* @return String 文件名
*/
@SneakyThrows
public String putObjectByRule(MultipartFile file) {
String fileName = minioRule.fileName(file.getOriginalFilename());
putObject(minioProperties.getBucketName(), fileName, file);
return fileName;
}

/**
* 上传文件
*
* @param file 上传文件类
* @param fileName 上传文件名
*/
@SneakyThrows
public void putObject(String fileName, MultipartFile file) {
putObject(minioProperties.getBucketName(), fileName, file);
}

/**
Expand All @@ -302,7 +416,7 @@ public void putObject(String bucketName, MultipartFile file) {
*/
@SneakyThrows
public void putObject(String bucketName, String fileName, MultipartFile file) {
putObject(getBucketName(bucketName), fileName, file.getInputStream(), (long) file.getInputStream().available(), "application/octet-stream");
putObject(bucketName, fileName, file.getInputStream(), (long) file.getInputStream().available(), "application/octet-stream");
}

/**
Expand All @@ -314,7 +428,7 @@ public void putObject(String bucketName, String fileName, MultipartFile file) {
*/
@SneakyThrows
public void putObject(String bucketName, String objectName, InputStream stream) {
putObject(getBucketName(bucketName), objectName, stream, (long) stream.available(), "application/octet-stream");
putObject(bucketName, objectName, stream, (long) stream.available(), "application/octet-stream");
}

/**
Expand All @@ -327,7 +441,7 @@ public void putObject(String bucketName, String objectName, InputStream stream)
*/
@SneakyThrows
public void putObject(String bucketName, String objectName, InputStream stream, long size) {
putObject(getBucketName(bucketName), objectName, stream, size, "application/octet-stream");
putObject(bucketName, objectName, stream, size, "application/octet-stream");
}

/**
Expand All @@ -345,6 +459,16 @@ public void putObject(String bucketName, String objectName, InputStream stream,
client.putObject(getBucketName(bucketName), objectName, stream, size, null, null, contextType);
}

/**
* 删除文件
*
* @param objectName 存储桶对象名称
*/
@SneakyThrows
public void removeObject(String objectName) {
removeObject(minioProperties.getBucketName(), objectName);
}

/**
* 删除文件
*
Expand All @@ -356,6 +480,16 @@ public void removeObject(String bucketName, String objectName) {
client.removeObject(getBucketName(bucketName), objectName);
}

/**
* 批量删除文件
*
* @param objectNames 存储桶对象名称集合
*/
@SneakyThrows
public void removeObjects(List<String> objectNames) {
removeObjects(minioProperties.getBucketName(), objectNames);
}

/**
* 批量删除文件
*
Expand All @@ -367,6 +501,16 @@ public void removeObjects(String bucketName, List<String> objectNames) {
client.removeObjects(getBucketName(bucketName), objectNames);
}

/**
* 获取存储桶策略
*
* @param policyType 策略枚举
* @return String
*/
public String getPolicyType(PolicyType policyType) {
return getPolicyType(getBucketName(), policyType);
}

/**
* 获取存储桶策略
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public MinioClient minioClient() {
@Bean
@ConditionalOnBean({MinioClient.class, IMinioRule.class})
@ConditionalOnMissingBean(MinioTemplate.class)
public MinioTemplate minioTemplate(MinioClient minioClient, IMinioRule minioRule) {
return new MinioTemplate(minioClient, minioRule);
public MinioTemplate minioTemplate(MinioClient minioClient, IMinioRule minioRule, MinioProperties minioProperties) {
return new MinioTemplate(minioClient, minioRule, minioProperties);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ public class MinioProperties {
/**
* 默认的存储桶名称
*/
private String bucketName = "blade";
private String bucketName = "bladex";

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.springblade.core.tool.utils.FileUtil;
import org.springblade.core.tool.utils.StringPool;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.web.multipart.MultipartFile;

/**
* 默认存储桶生成规则
Expand All @@ -43,8 +42,8 @@ public String bucketName(String bucketName) {
}

@Override
public String fileName(MultipartFile file) {
return StringUtil.randomUUID() + StringPool.DOT + FileUtil.getFileExtension(file.getOriginalFilename());
public String fileName(String originalFilename) {
return StringUtil.randomUUID() + StringPool.DOT + FileUtil.getFileExtension(originalFilename);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
package org.springblade.core.minio.rule;

import org.springframework.web.multipart.MultipartFile;

/**
* 生成存储桶命名规则
*
Expand All @@ -36,9 +34,9 @@ public interface IMinioRule {
/**
* 获取文件名规则
*
* @param file 文件
* @param originalFilename 文件名
* @return String
*/
String fileName(MultipartFile file);
String fileName(String originalFilename);

}
Loading

0 comments on commit a82f84d

Please sign in to comment.