|
@@ -40,258 +40,82 @@ public class UploadToOssUtil {
|
|
@Value("${oss.bucket:4dkankan}")
|
|
@Value("${oss.bucket:4dkankan}")
|
|
private String bucket;
|
|
private String bucket;
|
|
|
|
|
|
- @Value("${upload.type:oss}")
|
|
|
|
- private String type;
|
|
|
|
|
|
|
|
- @Value("${local.path:/home/4dkankan}")
|
|
|
|
- private String localPath;
|
|
|
|
|
|
|
|
- /**
|
|
|
|
- * oss文件上传命令
|
|
|
|
- * 第一个参数是oss路径,要包含bucket名称
|
|
|
|
- * 第二个参数是本地文件路径
|
|
|
|
- */
|
|
|
|
- private static final String UPLOAD_SH = "bash /opt/ossutil/upload.sh %s %s";
|
|
|
|
-
|
|
|
|
- //上传的数据是byte[],key是上传后的文件名
|
|
|
|
- public void upload(byte[] data,String key1) throws IOException{
|
|
|
|
- log.info("开始上传文件 源路径:{},目标路径:{},type:{}" , new String(data, "UTF-8"),key1,type);
|
|
|
|
- StorageType storageType = StorageType.get(type);
|
|
|
|
- switch (storageType){
|
|
|
|
- case OSS:
|
|
|
|
- uploadOss(data,key1);
|
|
|
|
- break;
|
|
|
|
- case AWS:
|
|
|
|
- break;
|
|
|
|
- case LOCAL:
|
|
|
|
- uploadLocal(data,key1);
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- public void upload(String filePath, String key1) {
|
|
|
|
- log.info("开始上传文件 源路径:{},目标路径:{},type:{}" , filePath,key1,type);
|
|
|
|
- StorageType storageType = StorageType.get(type);
|
|
|
|
- switch (storageType){
|
|
|
|
- case OSS:
|
|
|
|
- uploadOss(filePath,key1);
|
|
|
|
- break;
|
|
|
|
- case AWS:
|
|
|
|
- uploadAws(filePath,key1);
|
|
|
|
- break;
|
|
|
|
- case LOCAL:
|
|
|
|
- uploadLocal(filePath,key1);
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- public void delete(String key1) throws IOException{
|
|
|
|
- switch (type){
|
|
|
|
- case "oss":deleteOss(key1); break;
|
|
|
|
- case "local":FileUtil.del(key1); break;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 删除目录或者文件
|
|
|
|
- * @param prefix
|
|
|
|
|
|
+ * 获取文件内容-阿里云
|
|
|
|
+ * @param objectName
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public int deleteFile(String prefix){
|
|
|
|
- switch (type){
|
|
|
|
- case "oss":deleteOssFile(prefix); break;
|
|
|
|
- case "local":FileUtil.del(prefix); break;
|
|
|
|
- }
|
|
|
|
- return 1;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public void deleteOss(String objectName){
|
|
|
|
- OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
|
- try {
|
|
|
|
- ossClient.deleteObject(bucket, objectName);
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- log.error("OSS删除文件失败,key=" + objectName);
|
|
|
|
- }finally {
|
|
|
|
- if(ossClient != null){
|
|
|
|
- ossClient.shutdown();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public void deleteOssFile(String prefix){
|
|
|
|
- int maxKeys = 200;
|
|
|
|
|
|
+ public boolean existKey(String objectName){
|
|
|
|
+ //创建oss客户端
|
|
OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
- try {
|
|
|
|
-
|
|
|
|
- String nextMarker = null;
|
|
|
|
- ObjectListing objectListing;
|
|
|
|
-
|
|
|
|
- do {
|
|
|
|
- objectListing = ossClient.listObjects(new ListObjectsRequest(this.bucket).withPrefix(prefix).withMarker(nextMarker).withMaxKeys(maxKeys));
|
|
|
|
- List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
|
|
|
|
- if (CollUtil.isEmpty(sums)) {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- List<String> keys = new ArrayList<>();
|
|
|
|
- for (OSSObjectSummary sum : sums) {
|
|
|
|
- keys.add(sum.getKey());
|
|
|
|
- }
|
|
|
|
- DeleteObjectsRequest deleteObjectsRequest =
|
|
|
|
- new DeleteObjectsRequest(bucket).withKeys(keys).withEncodingType("url");
|
|
|
|
- DeleteObjectsResult deleteObjectsResult = ossClient
|
|
|
|
- .deleteObjects(deleteObjectsRequest);
|
|
|
|
- List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
|
|
|
|
- try {
|
|
|
|
- for (String deletedObject : deletedObjects) {
|
|
|
|
- String decode = URLDecoder.decode(deletedObject, "UTF-8");
|
|
|
|
- log.info("删除oss文件:{}", decode);
|
|
|
|
- }
|
|
|
|
- } catch (UnsupportedEncodingException e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-// ListUtil.page(keys, 100, subKeys -> {
|
|
|
|
-// DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucket)
|
|
|
|
-// .withKeys(
|
|
|
|
-// subKeys).withEncodingType("url");
|
|
|
|
-// DeleteObjectsResult deleteObjectsResult = ossClient
|
|
|
|
-// .deleteObjects(deleteObjectsRequest);
|
|
|
|
-// List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
|
|
|
|
-// try {
|
|
|
|
-// for (String deletedObject : deletedObjects) {
|
|
|
|
-// String decode = URLDecoder.decode(deletedObject, "UTF-8");
|
|
|
|
-// log.info("删除oss文件:{}", decode);
|
|
|
|
-// }
|
|
|
|
-// } catch (UnsupportedEncodingException e) {
|
|
|
|
-// e.printStackTrace();
|
|
|
|
-// }
|
|
|
|
-// });
|
|
|
|
- }while (objectListing.isTruncated());
|
|
|
|
- }catch (Exception e){
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
|
+ // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
|
|
|
|
+ try{
|
|
|
|
+ boolean exist = ossClient.doesObjectExist(bucket, objectName);
|
|
|
|
+ return exist;
|
|
|
|
+ }catch (Exception e){
|
|
|
|
+ log.error("s4判断是否存在key异常,key=" + objectName, e);
|
|
}finally {
|
|
}finally {
|
|
if(ossClient != null){
|
|
if(ossClient != null){
|
|
ossClient.shutdown();
|
|
ossClient.shutdown();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ return false;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
- public void uploadOss(byte[] data,String objectName){
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 从阿里云oss下载文件到本地
|
|
|
|
+ * @param objectName 云端文件k地址
|
|
|
|
+ * @param localPath 本地文件地址
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public boolean downFormAli(String objectName, String localPath){
|
|
OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
try {
|
|
try {
|
|
- ossClient.putObject(bucket, objectName, new ByteArrayInputStream(data));
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- log.error("oss上传文件失败", e);
|
|
|
|
|
|
+ com.aliyun.oss.model.GetObjectRequest request = new com.aliyun.oss.model.GetObjectRequest(bucket,objectName);
|
|
|
|
+ ossClient.getObject(request, new File(localPath));
|
|
|
|
+ return true;
|
|
|
|
+ }catch (Exception e){
|
|
|
|
+ log.error("阿里云oss文件下载失败,key=" + objectName, e);
|
|
}finally {
|
|
}finally {
|
|
if(ossClient != null){
|
|
if(ossClient != null){
|
|
ossClient.shutdown();
|
|
ossClient.shutdown();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- }
|
|
|
|
- public void uploadLocal(byte[] data,String key1){
|
|
|
|
- InputStream in = new ByteArrayInputStream(data);
|
|
|
|
- File file = new File(key1);
|
|
|
|
- String path = key1.substring(0, key1.lastIndexOf("/"));
|
|
|
|
- if (!file.exists()) {
|
|
|
|
- new File(path).mkdir();
|
|
|
|
- }
|
|
|
|
- FileOutputStream fos = null;
|
|
|
|
- try {
|
|
|
|
- fos = new FileOutputStream(file);
|
|
|
|
- int len = 0;
|
|
|
|
- byte[] buf = new byte[1024];
|
|
|
|
- while ((len = in.read(buf)) != -1) {
|
|
|
|
- fos.write(buf, 0, len);
|
|
|
|
- }
|
|
|
|
- fos.flush();
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- } finally {
|
|
|
|
- if (null != fos) {
|
|
|
|
- try {
|
|
|
|
- fos.close();
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ return false;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- public void uploadOss(String filePath, String key1){
|
|
|
|
|
|
+ public void uploadOss(String filePath, String key1){
|
|
OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
try {
|
|
try {
|
|
|
|
+ System.out.println("oss-path:"+key1);
|
|
File file = new File(filePath);
|
|
File file = new File(filePath);
|
|
if (!file.exists()) {
|
|
if (!file.exists()) {
|
|
- log.error("要上传的文件不存在:" + filePath);
|
|
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
ObjectMetadata metadata = new ObjectMetadata();
|
|
ObjectMetadata metadata = new ObjectMetadata();
|
|
- if(filePath.contains(".jpg")){
|
|
|
|
- metadata.setContentType("image/jpeg");
|
|
|
|
- }
|
|
|
|
ossClient.putObject(bucket, key1, new File(filePath), metadata);
|
|
ossClient.putObject(bucket, key1, new File(filePath), metadata);
|
|
|
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
- log.error(e.toString() + filePath);
|
|
|
|
|
|
+ e.printStackTrace();
|
|
} finally {
|
|
} finally {
|
|
ossClient.shutdown();
|
|
ossClient.shutdown();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
- public void uploadAws(String filePath, String key1){
|
|
|
|
- try{
|
|
|
|
- }catch (Exception e){
|
|
|
|
- e.printStackTrace();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- public void uploadLocal(String filePath, String key1){
|
|
|
|
|
|
+ public void delete(String objectName){
|
|
|
|
+ OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
try {
|
|
try {
|
|
- File srcFile = new File(filePath);
|
|
|
|
- File file = new File(localPath + key1);
|
|
|
|
- FileUtils.copyFile(srcFile,file);
|
|
|
|
- }catch (Exception e){
|
|
|
|
- e.printStackTrace();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 获取文件类型
|
|
|
|
- */
|
|
|
|
- public static String getContentType(String filePath){
|
|
|
|
- FileNameMap fileNameMap = URLConnection.getFileNameMap();
|
|
|
|
- String contentType = fileNameMap.getContentTypeFor(filePath);
|
|
|
|
- System.out.println(contentType);
|
|
|
|
- return contentType;
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 检查文件是否为空
|
|
|
|
- *
|
|
|
|
- * @param imageFile
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- private static boolean isEmpty(MultipartFile imageFile) {
|
|
|
|
- if (imageFile == null || imageFile.getSize() <= 0) {
|
|
|
|
- return true;
|
|
|
|
- }
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public List<String> listKeys(String sourcePath){
|
|
|
|
- StorageType storageType = StorageType.get(type);
|
|
|
|
- switch (storageType){
|
|
|
|
- case OSS:
|
|
|
|
- return this.listKeysFromAli(sourcePath);
|
|
|
|
- case AWS:
|
|
|
|
- return this.listKeysFromAws(sourcePath);
|
|
|
|
- case LOCAL:
|
|
|
|
- return this.listKeysFromLocal(sourcePath);
|
|
|
|
|
|
+ ossClient.deleteObject(bucket, objectName);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("OSS删除文件失败,key=" + objectName);
|
|
|
|
+ }finally {
|
|
|
|
+ if(ossClient != null){
|
|
|
|
+ ossClient.shutdown();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- return null;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -340,153 +164,12 @@ public class UploadToOssUtil {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 获得文件列表-亚马逊
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public List<String> listKeysFromAws(String sourcePath) {
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 获得文件列表-阿里云
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public List<String> listKeysFromLocal(String sourcePath) {
|
|
|
|
- List<String> keyList = new ArrayList<>();
|
|
|
|
- return keyList;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * <p>
|
|
|
|
- 拷贝目录
|
|
|
|
- * </p>
|
|
|
|
- * @author dengsixing
|
|
|
|
- * @date 2022/1/18
|
|
|
|
- * @param sourcePath
|
|
|
|
- * @param targetPath
|
|
|
|
- **/
|
|
|
|
- public void copyFiles(String sourcePath, String targetPath) throws IOException {
|
|
|
|
- StorageType storageType = StorageType.get(type);
|
|
|
|
- switch (storageType){
|
|
|
|
- case OSS:
|
|
|
|
- this.copyFilesFromAli(sourcePath, targetPath);
|
|
|
|
- break;
|
|
|
|
- case AWS:
|
|
|
|
- break;
|
|
|
|
- case LOCAL: this.copyFilesFromLocal(sourcePath, targetPath);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * <p>
|
|
|
|
- 拷贝文件
|
|
|
|
- * </p>
|
|
|
|
- * @author dengsixing
|
|
|
|
- * @date 2022/1/18
|
|
|
|
- * @param sourceKey
|
|
|
|
- * @param targetKey
|
|
|
|
- **/
|
|
|
|
- public void copyObject(String sourceKey, String targetKey) throws IOException {
|
|
|
|
- StorageType storageType = StorageType.get(type);
|
|
|
|
- switch (storageType){
|
|
|
|
- case OSS:
|
|
|
|
- this.copyObjectFromAli(sourceKey, targetKey);
|
|
|
|
- break;
|
|
|
|
- case AWS:
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * <p>
|
|
|
|
- 拷贝-阿里云
|
|
|
|
- * </p>
|
|
|
|
- * @author dengsixing
|
|
|
|
- * @date 2022/1/18
|
|
|
|
- * @param sourcePath
|
|
|
|
- * @param targetPath
|
|
|
|
- **/
|
|
|
|
- public void copyObjectFromAli(String sourcePath, String targetPath) throws IOException {
|
|
|
|
-
|
|
|
|
- // 创建OSSClient实例。
|
|
|
|
- OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
|
- // 复制文件
|
|
|
|
- log.info("开始复制:" + sourcePath);
|
|
|
|
- ossClient.copyObject(this.bucket, sourcePath, this.bucket, targetPath);
|
|
|
|
- log.info("复制成功:" + sourcePath);
|
|
|
|
- ossClient.shutdown();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * <p>
|
|
|
|
- 拷贝-阿里云
|
|
|
|
- * </p>
|
|
|
|
- * @author dengsixing
|
|
|
|
- * @date 2022/1/18
|
|
|
|
- * @param sourcePath
|
|
|
|
- * @param targetPath
|
|
|
|
- **/
|
|
|
|
- public void copyFilesFromAli(String sourcePath, String targetPath) throws IOException {
|
|
|
|
-
|
|
|
|
- //获取源文件列表
|
|
|
|
- List<String> sourceKeyList = this.listKeysFromAli(sourcePath);
|
|
|
|
- if(CollUtil.isEmpty(sourceKeyList)){
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- // 创建OSSClient实例。
|
|
|
|
- OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
|
- // 复制文件
|
|
|
|
- sourceKeyList.parallelStream().forEach(key -> {
|
|
|
|
- log.info("开始复制:" + key);
|
|
|
|
- ossClient.copyObject(this.bucket, key, this.bucket, key.replace(sourcePath, targetPath));
|
|
|
|
- log.info("复制成功:" + key);
|
|
|
|
- });
|
|
|
|
-
|
|
|
|
- ossClient.shutdown();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * <p>
|
|
|
|
- 拷贝-本地
|
|
|
|
- * </p>
|
|
|
|
- * @author dengsixing
|
|
|
|
- * @date 2022/1/18
|
|
|
|
- * @param sourcePath
|
|
|
|
- * @param targetPath
|
|
|
|
- **/
|
|
|
|
- public void copyFilesFromLocal(String sourcePath, String targetPath) throws IOException {
|
|
|
|
- // TODO: 2022/1/21
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 获取文件内容
|
|
|
|
- * @param bucketName
|
|
|
|
- * @param objectName
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public String getObjectContent(String bucketName, String objectName){
|
|
|
|
- StorageType storageType = StorageType.get(type);
|
|
|
|
- switch (storageType){
|
|
|
|
- case OSS:
|
|
|
|
- return this.getObjectContentFromAli(bucketName, objectName);
|
|
|
|
- case AWS:
|
|
|
|
- case LOCAL:
|
|
|
|
- return this.getObjectContentFromLocal(objectName);
|
|
|
|
- }
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
* 获取文件内容-阿里云
|
|
* 获取文件内容-阿里云
|
|
* @param bucketName
|
|
* @param bucketName
|
|
* @param objectName
|
|
* @param objectName
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public String getObjectContentFromAli(String bucketName, String objectName){
|
|
|
|
|
|
+ public String getObjectContent(String bucketName, String objectName){
|
|
//创建oss客户端
|
|
//创建oss客户端
|
|
OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
InputStream objectContent = null;
|
|
InputStream objectContent = null;
|
|
@@ -515,91 +198,16 @@ public class UploadToOssUtil {
|
|
return contentJson.toString();
|
|
return contentJson.toString();
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * 获取文件内容-阿里云
|
|
|
|
- * @param objectName
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public boolean existOnAli(String objectName){
|
|
|
|
- //创建oss客户端
|
|
|
|
- OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
|
- // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
|
|
|
|
- try{
|
|
|
|
- boolean exist = ossClient.doesObjectExist(bucket, objectName);
|
|
|
|
- return exist;
|
|
|
|
- }catch (Exception e){
|
|
|
|
- log.error("s4判断是否存在key异常,key=" + objectName, e);
|
|
|
|
- }finally {
|
|
|
|
- if(ossClient != null){
|
|
|
|
- ossClient.shutdown();
|
|
|
|
|
|
+ public void uploadFileOss(File file,String filePath) {
|
|
|
|
+ if(file.isFile()){
|
|
|
|
+ String ossPath = file.getPath().replace(filePath,"");
|
|
|
|
+ ossPath = ossPath.replace("\\","/");
|
|
|
|
+ this.uploadOss(file.getPath(),ossPath);
|
|
|
|
+ }else {
|
|
|
|
+ File[] files = file.listFiles();
|
|
|
|
+ for (File file1 : files) {
|
|
|
|
+ uploadFileOss(file1,filePath);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- return false;
|
|
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 判断key是否存在
|
|
|
|
- * @param key
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public boolean existKey(String key){
|
|
|
|
- StorageType storageType = StorageType.get(type);
|
|
|
|
- switch (storageType){
|
|
|
|
- case OSS:
|
|
|
|
- return this.existOnAli(key);
|
|
|
|
- case AWS:
|
|
|
|
- default:
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 获取文件内容-本地
|
|
|
|
- * @param objectName
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public String getObjectContentFromLocal(String objectName){
|
|
|
|
- // TODO: 2022/1/21
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * oss下载文件到本地
|
|
|
|
- * @param objectName
|
|
|
|
- * @param localPath
|
|
|
|
- */
|
|
|
|
- public boolean download(String objectName, String localPath){
|
|
|
|
- StorageType storageType = StorageType.get(this.type);
|
|
|
|
- switch (storageType){
|
|
|
|
- case OSS:
|
|
|
|
- return this.downFormAli(objectName, localPath);
|
|
|
|
- case AWS:
|
|
|
|
- }
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 从阿里云oss下载文件到本地
|
|
|
|
- * @param objectName 云端文件k地址
|
|
|
|
- * @param localPath 本地文件地址
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public boolean downFormAli(String objectName, String localPath){
|
|
|
|
- OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
|
- try {
|
|
|
|
- GetObjectRequest request = new GetObjectRequest(bucket,objectName);
|
|
|
|
- ossClient.getObject(request, new File(localPath));
|
|
|
|
- return true;
|
|
|
|
- }catch (Exception e){
|
|
|
|
- log.error("阿里云oss文件下载失败,key=" + objectName, e);
|
|
|
|
- }finally {
|
|
|
|
- if(ossClient != null){
|
|
|
|
- ossClient.shutdown();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
}
|
|
}
|