88 lines
3.2 KiB
Java
88 lines
3.2 KiB
Java
|
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;
|
||
|
|
||
|
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) {
|
||
|
if (type.equals(TYPE_MUSIC) && !ConfigTools.load(ConfigTools.CONFIG, TYPE_MUSIC, boolean.class, false)) {
|
||
|
return;
|
||
|
}
|
||
|
if (type.equals(TYPE_PHOTO) && !ConfigTools.load(ConfigTools.CONFIG, TYPE_PHOTO, boolean.class, false)) {
|
||
|
return;
|
||
|
}
|
||
|
if (type.equals(TYPE_DEPOT) && !ConfigTools.load(ConfigTools.CONFIG, TYPE_DEPOT, boolean.class, false)) {
|
||
|
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);
|
||
|
}
|
||
|
client.putObject(type, uploadPath, file);
|
||
|
}
|
||
|
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;
|
||
|
}
|
||
|
}
|