|
@@ -1,35 +1,20 @@
|
|
|
package com.fdkankan.site.common.util;
|
|
|
|
|
|
-import cn.hutool.core.collection.CollUtil;
|
|
|
-import com.aliyun.oss.OSSClient;
|
|
|
-import com.aliyun.oss.model.*;
|
|
|
+import com.fdkankan.filestorage.FileStorageTemplate;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import java.io.*;
|
|
|
-import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
|
@Slf4j
|
|
|
@Component
|
|
|
public class UploadToOssUtil {
|
|
|
|
|
|
|
|
|
- @Value("${oss.point:http://oss-cn-shenzhen-internal.aliyuncs.com}")
|
|
|
- private String point;
|
|
|
-
|
|
|
- @Value("${oss.key:LTAIUrvuHqj8pvry}")
|
|
|
- private String key;
|
|
|
-
|
|
|
- @Value("${oss.secrey:JLOVl0k8Ke0aaM8nLMMiUAZ3EiiqI4}")
|
|
|
- private String secrey;
|
|
|
-
|
|
|
- @Value("${oss.bucket:4dkankan}")
|
|
|
- private String bucket;
|
|
|
-
|
|
|
-
|
|
|
+ @Resource()
|
|
|
+ FileStorageTemplate fileStorageTemplate;
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -38,18 +23,12 @@ public class UploadToOssUtil {
|
|
|
* @return
|
|
|
*/
|
|
|
public boolean existKey(String objectName){
|
|
|
- //创建oss客户端
|
|
|
- OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
- // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
|
|
|
+
|
|
|
try{
|
|
|
- boolean exist = ossClient.doesObjectExist(bucket, objectName);
|
|
|
+ boolean exist = fileStorageTemplate.doesObjectExist(objectName);
|
|
|
return exist;
|
|
|
}catch (Exception e){
|
|
|
log.error("s4判断是否存在key异常,key=" + objectName, e);
|
|
|
- }finally {
|
|
|
- if(ossClient != null){
|
|
|
- ossClient.shutdown();
|
|
|
- }
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
@@ -61,29 +40,17 @@ public class UploadToOssUtil {
|
|
|
* @return
|
|
|
*/
|
|
|
public boolean downFormAli(String objectName, String localPath){
|
|
|
- OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
try {
|
|
|
- GetObjectRequest request = new GetObjectRequest(bucket,objectName);
|
|
|
- File file = new File(localPath);
|
|
|
- if(!file.getParentFile().exists()){
|
|
|
- file.getParentFile().mkdirs();
|
|
|
- file = new File(localPath);
|
|
|
- }
|
|
|
- ossClient.getObject(request, file);
|
|
|
+ fileStorageTemplate.downloadFile(objectName,localPath);
|
|
|
return true;
|
|
|
}catch (Exception e){
|
|
|
log.error("阿里云oss文件下载失败,key=" + objectName, e);
|
|
|
- }finally {
|
|
|
- if(ossClient != null){
|
|
|
- ossClient.shutdown();
|
|
|
- }
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
|
|
|
public void uploadOss(String filePath, String key1){
|
|
|
- OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
try {
|
|
|
log.info("upload-to-oss:file-path:{},oss-path:{}",filePath,key1);
|
|
|
File file = new File(filePath);
|
|
@@ -91,24 +58,16 @@ public class UploadToOssUtil {
|
|
|
log.info("upload-to-oss:file-path:{},oss-path:{},filePath不存在!",filePath,key1);
|
|
|
return;
|
|
|
}
|
|
|
- ObjectMetadata metadata = new ObjectMetadata();
|
|
|
- ossClient.putObject(bucket, key1, new File(filePath), metadata);
|
|
|
+ fileStorageTemplate.uploadFile(filePath,key1);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
- } finally {
|
|
|
- ossClient.shutdown();
|
|
|
}
|
|
|
}
|
|
|
public void delete(String objectName){
|
|
|
- OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
- try {
|
|
|
- ossClient.deleteObject(bucket, objectName);
|
|
|
+ try {
|
|
|
+ fileStorageTemplate.deleteObject( objectName);
|
|
|
} catch (Exception e) {
|
|
|
log.error("OSS删除文件失败,key=" + objectName);
|
|
|
- }finally {
|
|
|
- if(ossClient != null){
|
|
|
- ossClient.shutdown();
|
|
|
- }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -117,44 +76,7 @@ public class UploadToOssUtil {
|
|
|
* @return
|
|
|
*/
|
|
|
public List<String> listKeysFromAli(String sourcePath) {
|
|
|
- List<String> keyList = new ArrayList<>();
|
|
|
- OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
- try {
|
|
|
- boolean flag = true;
|
|
|
- String nextMaker = null;
|
|
|
- ListObjectsRequest listObjectsRequest = new ListObjectsRequest(this.bucket);
|
|
|
- //指定下一级文件
|
|
|
- listObjectsRequest.setPrefix(sourcePath);
|
|
|
- //设置分页的页容量
|
|
|
- listObjectsRequest.setMaxKeys(200);
|
|
|
- do
|
|
|
- {
|
|
|
- //获取下一页的起始点,它的下一项
|
|
|
- listObjectsRequest.setMarker(nextMaker);
|
|
|
- ObjectListing objectListing = ossClient.listObjects(listObjectsRequest);
|
|
|
- List<OSSObjectSummary> objectSummaries = objectListing.getObjectSummaries();
|
|
|
- List<String> collect = objectSummaries.stream().map(summary -> {
|
|
|
- return summary.getKey();
|
|
|
- }).collect(Collectors.toList());
|
|
|
- if(CollUtil.isNotEmpty(collect)){
|
|
|
- keyList.addAll(collect);
|
|
|
- }
|
|
|
- nextMaker = objectListing.getNextMarker();
|
|
|
- //全部执行完后,为false
|
|
|
- flag = objectListing.isTruncated();
|
|
|
- } while (flag);
|
|
|
- }catch (Exception e){
|
|
|
- log.error("获取文件列表失败,path="+sourcePath, e);
|
|
|
- }finally {
|
|
|
- if(ossClient != null){
|
|
|
- ossClient.shutdown();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- ossClient.shutdown();
|
|
|
-
|
|
|
- return keyList;
|
|
|
+ return fileStorageTemplate.getFileFolder(sourcePath);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -164,33 +86,12 @@ public class UploadToOssUtil {
|
|
|
* @return
|
|
|
*/
|
|
|
public String getObjectContent(String bucketName, String objectName){
|
|
|
- //创建oss客户端
|
|
|
- OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
- InputStream objectContent = null;
|
|
|
- StringBuilder contentJson = new StringBuilder();
|
|
|
- try {
|
|
|
- // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
|
|
|
- OSSObject ossObject = ossClient.getObject(bucketName, objectName);
|
|
|
- objectContent = ossObject.getObjectContent();
|
|
|
- try(BufferedReader reader = new BufferedReader(new InputStreamReader(objectContent))){
|
|
|
- while (true) {
|
|
|
- String line = reader.readLine();
|
|
|
- if (line == null) break;
|
|
|
- contentJson.append(line);
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("读取scene.json文件流失败", e);
|
|
|
- }
|
|
|
- }catch (Exception e){
|
|
|
- log.error("s3获取文件内容失败,key="+objectName, e);
|
|
|
- }finally {
|
|
|
- if(ossClient != null){
|
|
|
- ossClient.shutdown();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return contentJson.toString();
|
|
|
- }
|
|
|
+ try {
|
|
|
+ return fileStorageTemplate.getFileContent(bucketName,objectName);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
public void uploadFileOss(File file) {
|
|
|
if(file.isFile()){
|