|
|
|
@ -44,7 +44,11 @@ import java.io.IOException;
|
|
|
|
|
import java.io.InputStream; |
|
|
|
|
import java.io.InputStreamReader; |
|
|
|
|
import java.nio.file.Files; |
|
|
|
|
import java.nio.file.Path; |
|
|
|
|
import java.nio.file.Paths; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.Collections; |
|
|
|
|
import java.util.LinkedList; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
import java.util.stream.Stream; |
|
|
|
@ -56,8 +60,13 @@ import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import com.aliyun.oss.OSS; |
|
|
|
|
import com.aliyun.oss.OSSException; |
|
|
|
|
import com.aliyun.oss.ServiceException; |
|
|
|
|
import com.aliyun.oss.model.Bucket; |
|
|
|
|
import com.aliyun.oss.model.DeleteObjectsRequest; |
|
|
|
|
import com.aliyun.oss.model.ListObjectsV2Request; |
|
|
|
|
import com.aliyun.oss.model.ListObjectsV2Result; |
|
|
|
|
import com.aliyun.oss.model.OSSObject; |
|
|
|
|
import com.aliyun.oss.model.OSSObjectSummary; |
|
|
|
|
import com.aliyun.oss.model.ObjectMetadata; |
|
|
|
|
import com.aliyun.oss.model.PutObjectRequest; |
|
|
|
|
|
|
|
|
@ -190,8 +199,20 @@ public class OssStorageOperator implements Closeable, StorageOperate {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public boolean delete(String filePath, List<String> childrenPathArray, boolean recursive) throws IOException { |
|
|
|
|
return false; |
|
|
|
|
public boolean delete(String fullName, List<String> childrenPathList, boolean recursive) throws IOException { |
|
|
|
|
// append the resource fullName to the list for deletion.
|
|
|
|
|
childrenPathList.add(fullName); |
|
|
|
|
|
|
|
|
|
DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName) |
|
|
|
|
.withKeys(childrenPathList); |
|
|
|
|
try { |
|
|
|
|
ossClient.deleteObjects(deleteObjectsRequest); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
logger.error("delete objects error", e); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@ -288,19 +309,168 @@ public class OssStorageOperator implements Closeable, StorageOperate {
|
|
|
|
|
@Override |
|
|
|
|
public List<StorageEntity> listFilesStatusRecursively(String path, String defaultPath, String tenantCode, |
|
|
|
|
ResourceType type) { |
|
|
|
|
return null; |
|
|
|
|
List<StorageEntity> storageEntityList = new ArrayList<>(); |
|
|
|
|
LinkedList<StorageEntity> foldersToFetch = new LinkedList<>(); |
|
|
|
|
|
|
|
|
|
StorageEntity initialEntity = null; |
|
|
|
|
try { |
|
|
|
|
initialEntity = getFileStatus(path, defaultPath, tenantCode, type); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
logger.error("error while listing files status recursively, path: {}", path, e); |
|
|
|
|
return storageEntityList; |
|
|
|
|
} |
|
|
|
|
foldersToFetch.add(initialEntity); |
|
|
|
|
|
|
|
|
|
while (!foldersToFetch.isEmpty()) { |
|
|
|
|
String pathToExplore = foldersToFetch.pop().getFullName(); |
|
|
|
|
try { |
|
|
|
|
List<StorageEntity> tempList = listFilesStatus(pathToExplore, defaultPath, tenantCode, type); |
|
|
|
|
for (StorageEntity temp : tempList) { |
|
|
|
|
if (temp.isDirectory()) { |
|
|
|
|
foldersToFetch.add(temp); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
storageEntityList.addAll(tempList); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
logger.error("error while listing files status recursively, path: {}", pathToExplore, e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return storageEntityList; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public List<StorageEntity> listFilesStatus(String path, String defaultPath, String tenantCode, |
|
|
|
|
ResourceType type) throws Exception { |
|
|
|
|
return null; |
|
|
|
|
List<StorageEntity> storageEntityList = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
ListObjectsV2Result result = null; |
|
|
|
|
String nextContinuationToken = null; |
|
|
|
|
do { |
|
|
|
|
try { |
|
|
|
|
ListObjectsV2Request request = new ListObjectsV2Request(); |
|
|
|
|
request.setBucketName(bucketName); |
|
|
|
|
request.setPrefix(path); |
|
|
|
|
request.setDelimiter(FOLDER_SEPARATOR); |
|
|
|
|
request.setContinuationToken(nextContinuationToken); |
|
|
|
|
|
|
|
|
|
result = ossClient.listObjectsV2(request); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
throw new ServiceException("Get OSS file list exception", e); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
List<OSSObjectSummary> summaries = result.getObjectSummaries(); |
|
|
|
|
|
|
|
|
|
for (OSSObjectSummary summary : summaries) { |
|
|
|
|
if (!summary.getKey().endsWith(FOLDER_SEPARATOR)) { |
|
|
|
|
// the path is a file
|
|
|
|
|
String[] aliasArr = summary.getKey().split(FOLDER_SEPARATOR); |
|
|
|
|
String alias = aliasArr[aliasArr.length - 1]; |
|
|
|
|
String fileName = StringUtils.difference(defaultPath, summary.getKey()); |
|
|
|
|
|
|
|
|
|
StorageEntity entity = new StorageEntity(); |
|
|
|
|
entity.setAlias(alias); |
|
|
|
|
entity.setFileName(fileName); |
|
|
|
|
entity.setFullName(summary.getKey()); |
|
|
|
|
entity.setDirectory(false); |
|
|
|
|
entity.setDescription(""); |
|
|
|
|
entity.setUserName(tenantCode); |
|
|
|
|
entity.setType(type); |
|
|
|
|
entity.setSize(summary.getSize()); |
|
|
|
|
entity.setCreateTime(summary.getLastModified()); |
|
|
|
|
entity.setUpdateTime(summary.getLastModified()); |
|
|
|
|
entity.setPfullName(path); |
|
|
|
|
|
|
|
|
|
storageEntityList.add(entity); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for (String commonPrefix : result.getCommonPrefixes()) { |
|
|
|
|
// the paths in commonPrefix are directories
|
|
|
|
|
String suffix = StringUtils.difference(path, commonPrefix); |
|
|
|
|
String fileName = StringUtils.difference(defaultPath, commonPrefix); |
|
|
|
|
|
|
|
|
|
StorageEntity entity = new StorageEntity(); |
|
|
|
|
entity.setAlias(suffix); |
|
|
|
|
entity.setFileName(fileName); |
|
|
|
|
entity.setFullName(commonPrefix); |
|
|
|
|
entity.setDirectory(true); |
|
|
|
|
entity.setDescription(""); |
|
|
|
|
entity.setUserName(tenantCode); |
|
|
|
|
entity.setType(type); |
|
|
|
|
entity.setSize(0); |
|
|
|
|
entity.setCreateTime(null); |
|
|
|
|
entity.setUpdateTime(null); |
|
|
|
|
entity.setPfullName(path); |
|
|
|
|
|
|
|
|
|
storageEntityList.add(entity); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
nextContinuationToken = result.getNextContinuationToken(); |
|
|
|
|
} while (result.isTruncated()); |
|
|
|
|
|
|
|
|
|
return storageEntityList; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public StorageEntity getFileStatus(String path, String defaultPath, String tenantCode, |
|
|
|
|
ResourceType type) throws Exception { |
|
|
|
|
return null; |
|
|
|
|
ListObjectsV2Request request = new ListObjectsV2Request(); |
|
|
|
|
request.setBucketName(bucketName); |
|
|
|
|
request.setPrefix(path); |
|
|
|
|
request.setDelimiter(FOLDER_SEPARATOR); |
|
|
|
|
|
|
|
|
|
ListObjectsV2Result result; |
|
|
|
|
try { |
|
|
|
|
result = ossClient.listObjectsV2(request); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
throw new ServiceException("Get OSS file list exception", e); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
List<OSSObjectSummary> summaries = result.getObjectSummaries(); |
|
|
|
|
|
|
|
|
|
if (path.endsWith(FOLDER_SEPARATOR)) { |
|
|
|
|
// the path is a directory that may or may not exist in OSS
|
|
|
|
|
String alias = findDirAlias(path); |
|
|
|
|
String fileName = StringUtils.difference(defaultPath, path); |
|
|
|
|
|
|
|
|
|
StorageEntity entity = new StorageEntity(); |
|
|
|
|
entity.setAlias(alias); |
|
|
|
|
entity.setFileName(fileName); |
|
|
|
|
entity.setFullName(path); |
|
|
|
|
entity.setDirectory(true); |
|
|
|
|
entity.setDescription(""); |
|
|
|
|
entity.setUserName(tenantCode); |
|
|
|
|
entity.setType(type); |
|
|
|
|
entity.setSize(0); |
|
|
|
|
|
|
|
|
|
return entity; |
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
// the path is a file
|
|
|
|
|
if (summaries.size() > 0) { |
|
|
|
|
OSSObjectSummary summary = summaries.get(0); |
|
|
|
|
String[] aliasArr = summary.getKey().split(FOLDER_SEPARATOR); |
|
|
|
|
String alias = aliasArr[aliasArr.length - 1]; |
|
|
|
|
String fileName = StringUtils.difference(defaultPath, summary.getKey()); |
|
|
|
|
|
|
|
|
|
StorageEntity entity = new StorageEntity(); |
|
|
|
|
entity.setAlias(alias); |
|
|
|
|
entity.setFileName(fileName); |
|
|
|
|
entity.setFullName(summary.getKey()); |
|
|
|
|
entity.setDirectory(false); |
|
|
|
|
entity.setDescription(""); |
|
|
|
|
entity.setUserName(tenantCode); |
|
|
|
|
entity.setType(type); |
|
|
|
|
entity.setSize(summary.getSize()); |
|
|
|
|
entity.setCreateTime(summary.getLastModified()); |
|
|
|
|
entity.setUpdateTime(summary.getLastModified()); |
|
|
|
|
|
|
|
|
|
return entity; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
throw new FileNotFoundException("Object is not found in OSS Bucket: " + bucketName); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@ -360,4 +530,13 @@ public class OssStorageOperator implements Closeable, StorageOperate {
|
|
|
|
|
protected OSS buildOssClient() { |
|
|
|
|
return OssClientFactory.buildOssClient(ossConnection); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private String findDirAlias(String dirPath) { |
|
|
|
|
if (!dirPath.endsWith(FOLDER_SEPARATOR)) { |
|
|
|
|
return dirPath; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Path path = Paths.get(dirPath); |
|
|
|
|
return path.getName(path.getNameCount() - 1) + FOLDER_SEPARATOR; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|