104 lines
4.1 KiB
Java
Raw Normal View History

2021-06-09 18:31:48 +08:00
package com.yutou.nas.utils;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.StorageClass;
import com.yutou.nas.interfaces.DownloadInterface;
import java.io.File;
import java.io.FileNotFoundException;
2021-06-18 17:39:02 +08:00
import java.util.Arrays;
2021-06-09 18:31:48 +08:00
public class OSSManager {
public static final String TYPE_MUSIC = "oss-name-music";
public static final String TYPE_PHOTO = "oss-name-photo";
public static final String TYPE_DEPOT = "oss-name-depot";
private static OSS getOssClient() {
return new OSSClientBuilder().build(ConfigTools.load(ConfigTools.CONFIG, "oss-url", String.class),
ConfigTools.load(ConfigTools.CONFIG, "oss-id", String.class),
ConfigTools.load(ConfigTools.CONFIG, "oss-key", String.class));
}
private static void closeClient(OSS oss) {
oss.shutdown();
}
public static void upload(String type, String path, File... files) {
2021-06-18 17:39:02 +08:00
boolean music=false,photo=false,depot=false;
if (type.equals(TYPE_MUSIC) && !ConfigTools.load(ConfigTools.CONFIG, "oss-upload-music", boolean.class, false)) {
music=true;
2021-06-09 18:31:48 +08:00
}
2021-06-18 17:39:02 +08:00
if (type.equals(TYPE_PHOTO) && !ConfigTools.load(ConfigTools.CONFIG, "oss-upload-photo", boolean.class, false)) {
photo=true;
}
if (type.equals(TYPE_DEPOT) && !ConfigTools.load(ConfigTools.CONFIG, "oss-upload-depot", boolean.class, false)) {
depot=true;
2021-06-09 18:31:48 +08:00
}
2021-06-18 17:39:02 +08:00
if(music||photo||depot){
System.out.println("music = " + music);
System.out.println("photo = " + photo);
System.out.println("depot = " + depot);
System.out.println("type = " + type + ", path = " + path + ", files = " + Arrays.deepToString(files));
System.out.println("------------------------------");
2021-06-09 18:31:48 +08:00
return;
}
new Thread(() -> {
OSS client = getOssClient();
for (File file : files) {
if (file.isDirectory()) {
continue;
}
String uploadPath = file.getAbsolutePath().replace(path, "").replace(File.separator, "/");
if (uploadPath.startsWith("/")) {
uploadPath = uploadPath.substring(1);
}
2021-06-18 17:39:02 +08:00
if(isExist(ConfigTools.load(ConfigTools.CONFIG,type,String.class),uploadPath)){
System.out.println("文件已存在:"+file.getName());
return;
}
client.putObject(ConfigTools.load(ConfigTools.CONFIG,type,String.class), uploadPath, file);
System.out.println(file.getName()+"已上传");
2021-06-09 18:31:48 +08:00
}
closeClient(client);
}).start();
}
public static void download(String type, String path, DownloadInterface downloadInterface) {
new Thread(() -> {
OSS oss = getOssClient();
ObjectMetadata metadata = oss.getObjectMetadata(type, path);
if (metadata.getObjectStorageClass() == StorageClass.Archive) {
oss.restoreObject(type, path);
do {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
metadata = oss.getObjectMetadata(type, path);
} while (!metadata.isRestoreCompleted());
}
OSSObject obj = oss.getObject(type, path);
File file = StreamTools.streamSave(obj.getObjectContent());
if (file != null) {
downloadInterface.onDownload(file);
} else {
downloadInterface.onError(new FileNotFoundException("没有该文件"));
}
closeClient(oss);
}).start();
}
private static boolean isExist(String type,String path){
OSS oss=getOssClient();
boolean flag=oss.doesObjectExist(type,path,true);
closeClient(oss);
return flag;
}
}