feat: aliOssClient
This commit is contained in:
@@ -6,7 +6,6 @@ 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.lang.hash.Hash;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
@@ -14,8 +13,6 @@ import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.common.utils.HttpHeaders;
|
||||
import com.aliyun.oss.model.*;
|
||||
|
||||
import com.qiniu.storage.BucketManager;
|
||||
import com.qiniu.storage.model.FileInfo;
|
||||
import com.schisandra.oss.application.oss.constant.OssConstant;
|
||||
import com.schisandra.oss.application.oss.core.StandardOssClient;
|
||||
import com.schisandra.oss.application.oss.core.ali.model.AliOssConfig;
|
||||
@@ -35,6 +32,7 @@ import org.springframework.stereotype.Component;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URL;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
@@ -57,23 +55,29 @@ public class AliOssClient implements StandardOssClient {
|
||||
private OSS oss;
|
||||
private AliOssConfig aliOssConfig;
|
||||
|
||||
|
||||
public String getAliPreviewUrl(String fileName) {
|
||||
URL presignedUrl = oss.generatePresignedUrl(getBucketName(), fileName, new Date());
|
||||
return String.valueOf(presignedUrl);
|
||||
}
|
||||
|
||||
public String getAliBucketSize(String bucket) {
|
||||
ObjectListing objectListing = oss.listObjects(bucket, "");
|
||||
long x = 0L;
|
||||
List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
|
||||
for(OSSObjectSummary objectSummary : sums){
|
||||
x+=objectSummary.getSize();
|
||||
for (OSSObjectSummary objectSummary : sums) {
|
||||
x += objectSummary.getSize();
|
||||
}
|
||||
return DataSizeUtil.format(x);
|
||||
}
|
||||
|
||||
public List<HashMap<String,String>> selectAllBucket() {
|
||||
public List<HashMap<String, String>> selectAllBucket() {
|
||||
List<Bucket> buckets = oss.listBuckets();
|
||||
List<HashMap<String,String>> list1 = new ArrayList<>();
|
||||
List<HashMap<String, String>> list1 = new ArrayList<>();
|
||||
buckets.forEach(bucket -> {
|
||||
HashMap<String, String> names = new HashMap<>();
|
||||
names.put("name",bucket.getName());
|
||||
names.put("size",getAliBucketSize(bucket.getName()));
|
||||
names.put("name", bucket.getName());
|
||||
names.put("size", getAliBucketSize(bucket.getName()));
|
||||
list1.add(names);
|
||||
});
|
||||
return list1;
|
||||
@@ -85,12 +89,12 @@ public class AliOssClient implements StandardOssClient {
|
||||
* @return: java.util.List<com.schisandra.oss.application.oss.model.OssInfo>
|
||||
* @date: 2024/7/5 13:34
|
||||
*/
|
||||
public String transfer(String before){
|
||||
public String transfer(String before) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
|
||||
Date date = new Date();
|
||||
try{
|
||||
try {
|
||||
date = sdf.parse(before);
|
||||
}catch (Exception e){
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
String formatStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
|
||||
@@ -98,23 +102,23 @@ public class AliOssClient implements StandardOssClient {
|
||||
return formatStr;
|
||||
}
|
||||
|
||||
public List<OssInfo> listAliInfo(String bucket, String prefix){
|
||||
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){
|
||||
if (prefix == "") {
|
||||
for (OSSObjectSummary objectSummary : sums) {
|
||||
OssInfo info = new OssInfo();
|
||||
|
||||
String[] split_path = objectSummary.getKey().split("/");
|
||||
if(split_path.length>=2)
|
||||
if (split_path.length >= 2)
|
||||
continue;
|
||||
|
||||
String[] split_path_again = split_path[0].split("\\.");
|
||||
if(split_path_again.length>=2) {
|
||||
if (split_path_again.length >= 2) {
|
||||
info.setIsDir(false);
|
||||
}else{
|
||||
} else {
|
||||
info.setIsDir(true);
|
||||
}
|
||||
info.setName(split_path[0]);
|
||||
@@ -126,21 +130,21 @@ public class AliOssClient implements StandardOssClient {
|
||||
infos.add(info);
|
||||
}
|
||||
|
||||
}else{
|
||||
for(OSSObjectSummary objectSummary : sums){
|
||||
} 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)
|
||||
if (split_path.length >= x + 2)
|
||||
continue;
|
||||
String[] split_path_again = split_path[split_path.length-1].split("\\.");
|
||||
if(split_path_again.length>=2){
|
||||
String[] split_path_again = split_path[split_path.length - 1].split("\\.");
|
||||
if (split_path_again.length >= 2) {
|
||||
info.setIsDir(false);
|
||||
}else{
|
||||
} else {
|
||||
info.setIsDir(true);
|
||||
}
|
||||
info.setName(split_path[split_path.length-1]);
|
||||
info.setName(split_path[split_path.length - 1]);
|
||||
info.setPath(objectSummary.getKey());
|
||||
info.setLength(DataSizeUtil.format(objectSummary.getSize()));
|
||||
|
||||
@@ -159,7 +163,7 @@ public class AliOssClient implements StandardOssClient {
|
||||
if (!oss.doesBucketExist(bucketName)) {
|
||||
Bucket bucket = oss.createBucket(bucketName);
|
||||
bucket.setRegion("chengdu");
|
||||
}else {
|
||||
} else {
|
||||
return "bucket already exists";
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@@ -169,14 +173,13 @@ public class AliOssClient implements StandardOssClient {
|
||||
}
|
||||
|
||||
public String deleteBucket(String bucketName) {
|
||||
try{
|
||||
if (oss.doesBucketExist(bucketName)){
|
||||
try {
|
||||
if (oss.doesBucketExist(bucketName)) {
|
||||
VoidResult voidResult = oss.deleteBucket(bucketName);
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
return "bucket does not exist";
|
||||
}
|
||||
}catch(Exception e){
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "yes";
|
||||
|
Reference in New Issue
Block a user