feat: ali获取文件目录信息
This commit is contained in:
@@ -123,6 +123,24 @@ public class SchisandraOssAliController {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 获取文件目录信息
|
||||
* @param: [target, userId, dirName]
|
||||
* @return: com.schisandra.oss.common.entity.Result<java.lang.String>
|
||||
* @date: 2024/7/5 13:55
|
||||
*/
|
||||
@GetMapping("listAliDir")
|
||||
public Result<String> listAliDir(@RequestParam String userId,@RequestParam String bucket,@RequestParam String prefix) throws Exception {
|
||||
Preconditions.checkNotNull(userId, "不能为空");
|
||||
AliOssClient bean = SpringUtil.getBean(userId);
|
||||
bean.getAliOssConfig().setBucketName(bucket);
|
||||
if(prefix==null)
|
||||
prefix="";
|
||||
return Result.ok(bean.listAliInfo(bucket,prefix));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 查找bucket
|
||||
* @param userId
|
||||
@@ -183,8 +201,8 @@ public class SchisandraOssAliController {
|
||||
* @return: void
|
||||
* @date: 2024/6/26 14:34
|
||||
*/
|
||||
@PostMapping("uploadMinioFile")
|
||||
public Result<OssInfo> uploadMinioFile(@RequestParam String userId, @RequestParam MultipartFile file, @RequestParam String fileName, @RequestParam String bucket) throws IOException {
|
||||
@PostMapping("uploadAliFile")
|
||||
public Result<OssInfo> uploadAliFile(@RequestParam String userId, @RequestParam MultipartFile file, @RequestParam String fileName, @RequestParam String bucket) throws IOException {
|
||||
Preconditions.checkNotNull(userId, "不能为空");
|
||||
Preconditions.checkNotNull(fileName, "不能为空");
|
||||
Preconditions.checkNotNull(bucket, "不能为空");
|
||||
|
@@ -5,6 +5,7 @@ import cn.hutool.core.date.DatePattern;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.io.file.FileNameUtil;
|
||||
import cn.hutool.core.io.unit.DataSizeUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
@@ -31,6 +32,7 @@ import org.springframework.stereotype.Component;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
@@ -52,7 +54,6 @@ public class AliOssClient implements StandardOssClient {
|
||||
private OSS oss;
|
||||
private AliOssConfig aliOssConfig;
|
||||
|
||||
|
||||
public List<String> selectAllBucket() {
|
||||
List<Bucket> buckets = oss.listBuckets();
|
||||
List<String> names = new ArrayList<>();
|
||||
@@ -62,6 +63,80 @@ public class AliOssClient implements StandardOssClient {
|
||||
return names;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 获取目录文件信息
|
||||
* @param: [bucket, dirName]
|
||||
* @return: java.util.List<com.schisandra.oss.application.oss.model.OssInfo>
|
||||
* @date: 2024/7/5 13:34
|
||||
*/
|
||||
public String transfer(String before){
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
|
||||
Date date = new Date();
|
||||
try{
|
||||
date = sdf.parse(before);
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
String formatStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
|
||||
|
||||
return formatStr;
|
||||
}
|
||||
|
||||
public List<OssInfo> listAliInfo(String bucket, String prefix){
|
||||
// 列举文件。如果不设置Prefix,则列举存储空间下的所有文件。如果设置Prefix,则列举包含指定前缀的文件。
|
||||
ObjectListing objectListing = oss.listObjects(bucket, prefix);
|
||||
List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
|
||||
List<OssInfo> infos = new ArrayList<>();
|
||||
if(prefix == ""){
|
||||
for(OSSObjectSummary objectSummary : sums){
|
||||
OssInfo info = new OssInfo();
|
||||
|
||||
String[] split_path = objectSummary.getKey().split("/");
|
||||
if(split_path.length>=2)
|
||||
continue;
|
||||
|
||||
String[] split_path_again = split_path[0].split("\\.");
|
||||
if(split_path_again.length>=2) {
|
||||
info.setIsDir(false);
|
||||
}else{
|
||||
info.setIsDir(true);
|
||||
}
|
||||
info.setName(split_path[0]);
|
||||
info.setPath(objectSummary.getKey());
|
||||
info.setLength(DataSizeUtil.format(objectSummary.getSize()));
|
||||
|
||||
String formatStr = transfer(String.valueOf(objectSummary.getLastModified()));
|
||||
info.setLastUpdateTime(formatStr);
|
||||
infos.add(info);
|
||||
}
|
||||
|
||||
}else{
|
||||
for(OSSObjectSummary objectSummary : sums){
|
||||
OssInfo info = new OssInfo();
|
||||
|
||||
int x = prefix.split("/").length;
|
||||
String[] split_path = objectSummary.getKey().split("/");
|
||||
if(split_path.length>=x+2)
|
||||
continue;
|
||||
String[] split_path_again = split_path[split_path.length-1].split("\\.");
|
||||
if(split_path_again.length>=2){
|
||||
info.setIsDir(false);
|
||||
}else{
|
||||
info.setIsDir(true);
|
||||
}
|
||||
info.setName(split_path[split_path.length-1]);
|
||||
info.setPath(objectSummary.getKey());
|
||||
info.setLength(DataSizeUtil.format(objectSummary.getSize()));
|
||||
|
||||
String formatStr = transfer(String.valueOf(objectSummary.getLastModified()));
|
||||
info.setLastUpdateTime(formatStr);
|
||||
infos.add(info);
|
||||
}
|
||||
}
|
||||
return infos;
|
||||
}
|
||||
|
||||
|
||||
public String createBucket(String bucketName) {
|
||||
|
||||
try {
|
||||
@@ -271,5 +346,4 @@ public class AliOssClient implements StandardOssClient {
|
||||
}
|
||||
return ossInfo;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -84,9 +84,27 @@ public class AliOssConfiguration {
|
||||
return new AliOssClient(ossClient(aliOssConfig), aliOssConfig);
|
||||
}
|
||||
|
||||
// public OSS ossClient(AliOssConfig aliOssConfig) {
|
||||
// return new OSSClientBuilder().build(aliOssConfig.getEndpoint(),
|
||||
// aliOssConfig.getAccessKeyId(),
|
||||
// aliOssConfig.getAccessKeySecret());
|
||||
// }
|
||||
public OSS ossClient(AliOssConfig aliOssConfig) {
|
||||
String securityToken = aliOssConfig.getSecurityToken();
|
||||
AliOssClientConfig clientConfiguration = aliOssConfig.getClientConfig();
|
||||
if (ObjectUtil.isEmpty(securityToken) && ObjectUtil.isNotEmpty(clientConfiguration)) {
|
||||
return new OSSClientBuilder().build(aliOssConfig.getEndpoint(),
|
||||
aliOssConfig.getAccessKeyId(),
|
||||
aliOssConfig.getAccessKeySecret(), clientConfiguration.toClientConfig());
|
||||
}
|
||||
if (ObjectUtil.isNotEmpty(securityToken) && ObjectUtil.isEmpty(clientConfiguration)) {
|
||||
return new OSSClientBuilder().build(aliOssConfig.getEndpoint(),
|
||||
aliOssConfig.getAccessKeyId(),
|
||||
aliOssConfig.getAccessKeySecret(), securityToken);
|
||||
}
|
||||
return new OSSClientBuilder().build(aliOssConfig.getEndpoint(),
|
||||
aliOssConfig.getAccessKeyId(),
|
||||
aliOssConfig.getAccessKeySecret());
|
||||
aliOssConfig.getAccessKeySecret(), securityToken,
|
||||
Optional.ofNullable(clientConfiguration).orElse(new AliOssClientConfig()).toClientConfig());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user