|
@@ -3,8 +3,7 @@ package com.fdkankan.fyun.oss;
|
|
import com.aliyun.oss.OSS;
|
|
import com.aliyun.oss.OSS;
|
|
import com.aliyun.oss.model.*;
|
|
import com.aliyun.oss.model.*;
|
|
import com.fdkankan.common.util.CreateObjUtil;
|
|
import com.fdkankan.common.util.CreateObjUtil;
|
|
-import com.fdkankan.fyun.config.FYunFileConfig;
|
|
|
|
-import com.fdkankan.fyun.face.FYunFileService;
|
|
|
|
|
|
+import com.fdkankan.fyun.face.AbstractFYunFileService;
|
|
import com.fdkankan.fyun.oss.constant.OssConstants;
|
|
import com.fdkankan.fyun.oss.constant.OssConstants;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
@@ -14,24 +13,19 @@ import org.springframework.util.CollectionUtils;
|
|
import org.springframework.util.ObjectUtils;
|
|
import org.springframework.util.ObjectUtils;
|
|
|
|
|
|
import java.io.*;
|
|
import java.io.*;
|
|
-import java.net.FileNameMap;
|
|
|
|
-import java.net.URLConnection;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Component
|
|
@Component
|
|
-public class OssFileService implements FYunFileService {
|
|
|
|
|
|
+public class OssFileService extends AbstractFYunFileService {
|
|
|
|
|
|
private Logger log = LoggerFactory.getLogger(this.getClass().getName());
|
|
private Logger log = LoggerFactory.getLogger(this.getClass().getName());
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
private OSS ossClient;
|
|
private OSS ossClient;
|
|
|
|
|
|
- @Autowired
|
|
|
|
- private FYunFileConfig fYunFileConfig;
|
|
|
|
-
|
|
|
|
@Override
|
|
@Override
|
|
public void uploadFile(String bucket, byte[] data, String remoteFilePath) throws Exception {
|
|
public void uploadFile(String bucket, byte[] data, String remoteFilePath) throws Exception {
|
|
try {
|
|
try {
|
|
@@ -47,20 +41,10 @@ public class OssFileService implements FYunFileService {
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public void uploadFile(byte[] data, String remoteFilePath) throws Exception {
|
|
|
|
- uploadFile(fYunFileConfig.getBucket(), data, remoteFilePath);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
public void uploadFile(String bucket, String filePath, String remoteFilePath) throws Exception {
|
|
public void uploadFile(String bucket, String filePath, String remoteFilePath) throws Exception {
|
|
uploadFile(bucket, filePath, remoteFilePath, true);
|
|
uploadFile(bucket, filePath, remoteFilePath, true);
|
|
}
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
|
- public void uploadFile(String filePath, String remoteFilePath) throws Exception {
|
|
|
|
- uploadFile(fYunFileConfig.getBucket(), filePath, remoteFilePath, true);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
private void uploadFile(String bucket, String filePath, String remoteFilePath, Boolean shutdown) throws Exception {
|
|
private void uploadFile(String bucket, String filePath, String remoteFilePath, Boolean shutdown) throws Exception {
|
|
try {
|
|
try {
|
|
File file = new File(filePath);
|
|
File file = new File(filePath);
|
|
@@ -103,11 +87,6 @@ public class OssFileService implements FYunFileService {
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public void uploadFileByCommand(String filePath, String remoteFilePath) throws Exception {
|
|
|
|
- uploadFileByCommand(fYunFileConfig.getBucket(), filePath, remoteFilePath);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
public void deleteFile(String bucket, String remoteFilePath) throws IOException {
|
|
public void deleteFile(String bucket, String remoteFilePath) throws IOException {
|
|
try {
|
|
try {
|
|
ossClient.deleteObject(bucket, remoteFilePath);
|
|
ossClient.deleteObject(bucket, remoteFilePath);
|
|
@@ -122,15 +101,10 @@ public class OssFileService implements FYunFileService {
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public void deleteFile(String remoteFilePath) throws IOException {
|
|
|
|
- deleteFile(fYunFileConfig.getBucket(), remoteFilePath);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
public void deleteFolder(String bucket, String remoteFolderPath) throws Exception {
|
|
public void deleteFolder(String bucket, String remoteFolderPath) throws Exception {
|
|
try {
|
|
try {
|
|
List<String> remoteFiles = listRemoteFiles(bucket, remoteFolderPath, false);
|
|
List<String> remoteFiles = listRemoteFiles(bucket, remoteFolderPath, false);
|
|
- if(CollectionUtils.isEmpty(remoteFiles)){
|
|
|
|
|
|
+ if (CollectionUtils.isEmpty(remoteFiles)) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
DeleteObjectsRequest request = new DeleteObjectsRequest(bucket);
|
|
DeleteObjectsRequest request = new DeleteObjectsRequest(bucket);
|
|
@@ -147,11 +121,6 @@ public class OssFileService implements FYunFileService {
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public void deleteFolder(String remoteFolderPath) throws Exception {
|
|
|
|
- deleteFolder(fYunFileConfig.getBucket(), remoteFolderPath);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
public void uploadMulFiles(String bucket, Map<String, String> filepaths) throws Exception {
|
|
public void uploadMulFiles(String bucket, Map<String, String> filepaths) throws Exception {
|
|
try {
|
|
try {
|
|
for (Map.Entry<String, String> entry : filepaths.entrySet()) {
|
|
for (Map.Entry<String, String> entry : filepaths.entrySet()) {
|
|
@@ -166,21 +135,12 @@ public class OssFileService implements FYunFileService {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
|
- public void uploadMulFiles(Map<String, String> filepaths) throws Exception {
|
|
|
|
- uploadMulFiles(fYunFileConfig.getBucket(), filepaths);
|
|
|
|
- }
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public List<String> listRemoteFiles(String bucket, String sourcePath) throws Exception {
|
|
public List<String> listRemoteFiles(String bucket, String sourcePath) throws Exception {
|
|
return listRemoteFiles(bucket, sourcePath, true);
|
|
return listRemoteFiles(bucket, sourcePath, true);
|
|
}
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
|
- public List<String> listRemoteFiles(String sourcePath) throws Exception {
|
|
|
|
- return listRemoteFiles(fYunFileConfig.getBucket(), sourcePath);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
private List<String> listRemoteFiles(String bucket, String sourcePath, Boolean shutdown) throws Exception {
|
|
private List<String> listRemoteFiles(String bucket, String sourcePath, Boolean shutdown) throws Exception {
|
|
List<String> keyList = new ArrayList<>();
|
|
List<String> keyList = new ArrayList<>();
|
|
try {
|
|
try {
|
|
@@ -217,10 +177,10 @@ public class OssFileService implements FYunFileService {
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void copyFileBetweenFyun(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) throws Exception {
|
|
public void copyFileBetweenFyun(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) throws Exception {
|
|
- copyFileBetweenFyun(sourceBucketName,sourcePath,targetBucketName,targetPath,true);
|
|
|
|
|
|
+ copyFileBetweenFyun(sourceBucketName, sourcePath, targetBucketName, targetPath, true);
|
|
}
|
|
}
|
|
|
|
|
|
- private void copyFileBetweenFyun(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath,Boolean shutdown) throws Exception {
|
|
|
|
|
|
+ private void copyFileBetweenFyun(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath, Boolean shutdown) throws Exception {
|
|
try {
|
|
try {
|
|
List<String> files = listRemoteFiles(sourceBucketName, sourcePath, false);
|
|
List<String> files = listRemoteFiles(sourceBucketName, sourcePath, false);
|
|
if (ObjectUtils.isEmpty(files)) {
|
|
if (ObjectUtils.isEmpty(files)) {
|
|
@@ -239,11 +199,6 @@ public class OssFileService implements FYunFileService {
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public void copyFileBetweenFyun(String sourcePath, String targetBucketName, String targetPath) throws Exception {
|
|
|
|
- copyFileBetweenFyun(fYunFileConfig.getBucket(),sourcePath, targetBucketName, targetPath);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
public void copyFilesBetweenFyun(String sourceBucketName, String targetBucketName, Map<String, String> pathMap) throws Exception {
|
|
public void copyFilesBetweenFyun(String sourceBucketName, String targetBucketName, Map<String, String> pathMap) throws Exception {
|
|
if (ObjectUtils.isEmpty(pathMap)) {
|
|
if (ObjectUtils.isEmpty(pathMap)) {
|
|
return;
|
|
return;
|
|
@@ -262,11 +217,6 @@ public class OssFileService implements FYunFileService {
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public void copyFilesBetweenFyun(String targetBucketName, Map<String, String> pathMap) throws Exception {
|
|
|
|
- copyFilesBetweenFyun(fYunFileConfig.getBucket(),targetBucketName,pathMap);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
public String getFileContent(String bucketName, String remoteFilePath) throws Exception {
|
|
public String getFileContent(String bucketName, String remoteFilePath) throws Exception {
|
|
try {
|
|
try {
|
|
OSSObject ossObject = ossClient.getObject(bucketName, remoteFilePath);
|
|
OSSObject ossObject = ossClient.getObject(bucketName, remoteFilePath);
|
|
@@ -293,11 +243,6 @@ public class OssFileService implements FYunFileService {
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public String getFileContent(String remoteFilePath) throws Exception {
|
|
|
|
- return getFileContent(fYunFileConfig.getBucket(), remoteFilePath);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
public boolean fileExist(String bucket, String objectName) throws Exception {
|
|
public boolean fileExist(String bucket, String objectName) throws Exception {
|
|
try {
|
|
try {
|
|
return ossClient.doesObjectExist(bucket, objectName);
|
|
return ossClient.doesObjectExist(bucket, objectName);
|
|
@@ -312,15 +257,10 @@ public class OssFileService implements FYunFileService {
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public boolean fileExist(String objectName) throws Exception {
|
|
|
|
- return fileExist(fYunFileConfig.getBucket(), objectName);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
public void downloadFile(String bucket, String remoteFilePath, String localPath) throws Exception {
|
|
public void downloadFile(String bucket, String remoteFilePath, String localPath) throws Exception {
|
|
try {
|
|
try {
|
|
File localFile = new File(localPath);
|
|
File localFile = new File(localPath);
|
|
- if(!localFile.getParentFile().exists()){
|
|
|
|
|
|
+ if (!localFile.getParentFile().exists()) {
|
|
localFile.getParentFile().mkdirs();
|
|
localFile.getParentFile().mkdirs();
|
|
}
|
|
}
|
|
DownloadFileRequest request = new DownloadFileRequest(bucket, remoteFilePath);
|
|
DownloadFileRequest request = new DownloadFileRequest(bucket, remoteFilePath);
|
|
@@ -333,15 +273,12 @@ public class OssFileService implements FYunFileService {
|
|
} catch (Throwable throwable) {
|
|
} catch (Throwable throwable) {
|
|
log.error("文件下载失败:{}", remoteFilePath);
|
|
log.error("文件下载失败:{}", remoteFilePath);
|
|
throwable.printStackTrace();
|
|
throwable.printStackTrace();
|
|
- }finally {
|
|
|
|
|
|
+ } finally {
|
|
if (!ObjectUtils.isEmpty(ossClient)) {
|
|
if (!ObjectUtils.isEmpty(ossClient)) {
|
|
ossClient.shutdown();
|
|
ossClient.shutdown();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
|
- public void downloadFile(String remoteFilePath, String localPath) throws Exception {
|
|
|
|
- downloadFile(fYunFileConfig.getBucket(),remoteFilePath,localPath);
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
}
|
|
}
|