|
@@ -0,0 +1,605 @@
|
|
|
+package com.fdkankan.fusion.common.util;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import com.aliyun.oss.OSSClient;
|
|
|
+import com.aliyun.oss.model.*;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.net.FileNameMap;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLConnection;
|
|
|
+import java.net.URLDecoder;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+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;
|
|
|
+
|
|
|
+ @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
|
|
|
+ * @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;
|
|
|
+ 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();
|
|
|
+ }finally {
|
|
|
+ if(ossClient != null){
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void uploadOss(byte[] data,String objectName){
|
|
|
+ OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
+ try {
|
|
|
+ ossClient.putObject(bucket, objectName, new ByteArrayInputStream(data));
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("oss上传文件失败", e);
|
|
|
+ }finally {
|
|
|
+ if(ossClient != null){
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void uploadOss(String filePath, String key1){
|
|
|
+ OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
+ try {
|
|
|
+ File file = new File(filePath);
|
|
|
+ if (!file.exists()) {
|
|
|
+ log.error("要上传的文件不存在:" + filePath);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ ObjectMetadata metadata = new ObjectMetadata();
|
|
|
+ if(filePath.contains(".jpg")){
|
|
|
+ metadata.setContentType("image/jpeg");
|
|
|
+ }
|
|
|
+ ossClient.putObject(bucket, key1, new File(filePath), metadata);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.toString() + filePath);
|
|
|
+ } finally {
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void uploadAws(String filePath, String key1){
|
|
|
+ try{
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public void uploadLocal(String filePath, String key1){
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得文件列表-阿里云
|
|
|
+ * @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
|
|
|
+ */
|
|
|
+ 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 objectName
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String getObjectContentFromAli(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();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取文件内容-阿里云
|
|
|
+ * @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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|