|
@@ -1,6 +1,7 @@
|
|
package com.fdkankan.filestorage.aliyun;
|
|
package com.fdkankan.filestorage.aliyun;
|
|
|
|
|
|
|
|
|
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import com.alibaba.fastjson2.JSON;
|
|
import com.alibaba.fastjson2.JSON;
|
|
import com.aliyun.oss.OSS;
|
|
import com.aliyun.oss.OSS;
|
|
@@ -17,6 +18,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.util.Base64Utils;
|
|
import org.springframework.util.Base64Utils;
|
|
import org.springframework.util.CollectionUtils;
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
+import org.springframework.util.ObjectUtils;
|
|
import org.springframework.util.StringUtils;
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
import java.io.*;
|
|
import java.io.*;
|
|
@@ -138,7 +140,11 @@ public class AliyunOssTemplate implements FileStorageTemplate {
|
|
InnerUtils.checkArgument(file != null && file.length() > 0, "file can't be empty");
|
|
InnerUtils.checkArgument(file != null && file.length() > 0, "file can't be empty");
|
|
GetObjectRequest request = new GetObjectRequest(bucket, pathKey);
|
|
GetObjectRequest request = new GetObjectRequest(bucket, pathKey);
|
|
log.info("下载开始:下载bucket={},下载pathKey={},下载filePath={}", bucket, pathKey, file);
|
|
log.info("下载开始:下载bucket={},下载pathKey={},下载filePath={}", bucket, pathKey, file);
|
|
- return ossClient.getObject(request, new File(file));
|
|
|
|
|
|
+ File downloadFile = new File(file);
|
|
|
|
+ if (!FileUtil.exist(downloadFile.getParent())){
|
|
|
|
+ FileUtil.mkdir(downloadFile.getParent());
|
|
|
|
+ }
|
|
|
|
+ return ossClient.getObject(request, downloadFile);
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -278,14 +284,9 @@ public class AliyunOssTemplate implements FileStorageTemplate {
|
|
try {
|
|
try {
|
|
boolean flag = true;
|
|
boolean flag = true;
|
|
String nextMaker = null;
|
|
String nextMaker = null;
|
|
- //指定下一级文件
|
|
|
|
ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucket);
|
|
ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucket);
|
|
- // 设置正斜线(/)为文件夹的分隔符。
|
|
|
|
- listObjectsRequest.setDelimiter("/");
|
|
|
|
- // 设置prefix参数来获取fun目录下的所有文件。
|
|
|
|
- if (!StringUtils.isEmpty(keyName)) {
|
|
|
|
- listObjectsRequest.setPrefix(keyName + "/");
|
|
|
|
- }
|
|
|
|
|
|
+ //指定下一级文件
|
|
|
|
+ listObjectsRequest.setPrefix(keyName);
|
|
//设置分页的页容量
|
|
//设置分页的页容量
|
|
listObjectsRequest.setMaxKeys(200);
|
|
listObjectsRequest.setMaxKeys(200);
|
|
do {
|
|
do {
|
|
@@ -413,4 +414,23 @@ public class AliyunOssTemplate implements FileStorageTemplate {
|
|
host=this.getBucket()+"."+host;
|
|
host=this.getBucket()+"."+host;
|
|
return new URI(uri.getScheme(), host, uri.getPath()+"/"+key, uri.getFragment()).toString();
|
|
return new URI(uri.getScheme(), host, uri.getPath()+"/"+key, uri.getFragment()).toString();
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void copyFolder(String sourcePath, String targetPath) {
|
|
|
|
+ copyFolder(ossProperties.getBucket(), sourcePath, ossProperties.getBucket(), targetPath);
|
|
|
|
+ }
|
|
|
|
+ @Override
|
|
|
|
+ public void copyFolder(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) {
|
|
|
|
+ try {
|
|
|
|
+ List<String> files = this.getFileFolder(sourceBucketName, sourcePath);
|
|
|
|
+ if (ObjectUtils.isEmpty(files)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ files.stream().forEach(file -> {
|
|
|
|
+ this.copyObject(sourceBucketName, file, targetBucketName ,file.replace(sourcePath, targetPath));
|
|
|
|
+ });
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("列举文件目录失败,key:" + sourcePath, e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|