MINIO 添加基路径支持

This commit is contained in:
zhaojun1998
2019-12-21 11:00:47 +08:00
parent 4d9357b11f
commit 12bae1ef53

View File

@@ -1,5 +1,6 @@
package im.zhaojun.minio;
import im.zhaojun.common.config.ZFileCacheConfiguration;
import im.zhaojun.common.model.StorageConfig;
import im.zhaojun.common.model.dto.FileItemDTO;
import im.zhaojun.common.model.enums.FileTypeEnum;
@@ -13,6 +14,8 @@ import io.minio.messages.Item;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@@ -21,9 +24,9 @@ import java.util.List;
import java.util.Map;
@Service
@CacheConfig(cacheNames = ZFileCacheConfiguration.CACHE_NAME, keyGenerator = "keyGenerator")
public class MinIOServiceImpl implements FileService {
private static final Logger log = LoggerFactory.getLogger(MinIOServiceImpl.class);
private String bucketName;
@@ -39,6 +42,8 @@ public class MinIOServiceImpl implements FileService {
private static final String ENDPOINT_KEY = "endPoint";
private static final String BASE_PATH = "base-path";
@Resource
private StorageConfigService storageConfigService;
@@ -46,6 +51,8 @@ public class MinIOServiceImpl implements FileService {
private boolean isInitialized;
private String basePath;
@Override
public void init() {
try {
@@ -56,35 +63,38 @@ public class MinIOServiceImpl implements FileService {
String endPoint = stringStorageConfigMap.get(ENDPOINT_KEY).getValue();
bucketName = stringStorageConfigMap.get(BUCKET_NAME_KEY).getValue();
minioClient = new MinioClient(endPoint, accessKey, secretKey);
basePath = stringStorageConfigMap.get(BASE_PATH).getValue();
basePath = basePath == null ? "" : basePath;
isInitialized = true;
} catch (Exception e) {
log.debug(StorageTypeEnum.MINIO.getDescription() + "初始化异常, 已跳过");
}
}
@Cacheable
@Override
public List<FileItemDTO> fileList(String path) throws Exception {
path = StringUtils.removeFirstSeparator(path);
String fullPath = StringUtils.removeFirstSeparator(StringUtils.removeDuplicateSeparator(basePath + "/" + path + "/"));
List<FileItemDTO> fileItemList = new ArrayList<>();
Iterable<Result<Item>> iterable = minioClient.listObjects(bucketName, path, false);
Iterable<Result<Item>> iterable = minioClient.listObjects(bucketName, fullPath, false);
for (Result<Item> itemResult : iterable) {
Item item = itemResult.get();
FileItemDTO fileItemDTO = new FileItemDTO();
if (item.isDir()) {
fileItemDTO.setName(StringUtils.removeLastSeparator(item.objectName().replaceFirst(path, "")));
fileItemDTO.setName(StringUtils.removeLastSeparator(item.objectName().replace(fullPath, "")));
fileItemDTO.setType(FileTypeEnum.FOLDER);
fileItemDTO.setPath(path);
} else {
fileItemDTO.setName(item.objectName().replaceFirst(path, ""));
fileItemDTO.setName(item.objectName().replace(fullPath, ""));
fileItemDTO.setSize(item.objectSize());
fileItemDTO.setTime(item.lastModified());
fileItemDTO.setType(FileTypeEnum.FILE);
fileItemDTO.setPath(path);
fileItemDTO.setUrl(getDownloadUrl(StringUtils.concatUrl(path, fileItemDTO.getName())));
fileItemDTO.setUrl(getDownloadUrl(StringUtils.concatUrl(fullPath, fileItemDTO.getName())));
}
fileItemList.add(fileItemDTO);
}
@@ -92,6 +102,7 @@ public class MinIOServiceImpl implements FileService {
return fileItemList;
}
@Cacheable
@Override
public String getDownloadUrl(String path) throws Exception {
return minioClient.presignedGetObject(bucketName, path, timeout.intValue());