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.util.ObjectUtils; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.commons.CommonsMultipartFile; import java.io.*; import java.net.*; 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; /** * 获取文件内容-阿里云 * @param objectName * @return */ public boolean existKey(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; } /** * 从阿里云oss下载文件到本地 * @param objectName 云端文件k地址 * @param localPath 本地文件地址 * @return */ public boolean downFormAli(String objectName, String localPath){ OSSClient ossClient = new OSSClient(point, key, secrey); try { com.aliyun.oss.model.GetObjectRequest request = new com.aliyun.oss.model.GetObjectRequest(bucket,objectName); File file = new File(localPath); if(!file.getParentFile().exists()){ file.getParentFile().mkdirs(); file = new File(localPath); } ossClient.getObject(request, file); return true; }catch (Exception e){ log.error("阿里云oss文件下载失败,key=" + objectName, e); }finally { if(ossClient != null){ ossClient.shutdown(); } } return false; } public void uploadOss(String filePath, String key1){ OSSClient ossClient = new OSSClient(point, key, secrey); try { log.info("upload-to-oss:file-path:{},oss-path:{}",filePath,key1); File file = new File(filePath); if (!file.exists()) { log.info("upload-to-oss:file-path:{},oss-path:{},filePath不存在!",filePath,key1); return; } ObjectMetadata metadata = new ObjectMetadata(); ossClient.putObject(bucket, key1, new File(filePath), metadata); } catch (Exception e) { log.info("upload-to-oss:error:file-path:{},key:{}",filePath,key1); } finally { ossClient.shutdown(); } } public void delete(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(); } } } /** * 获得文件列表-阿里云 * @return */ public List listKeysFromAli(String sourcePath) { List 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 objectSummaries = objectListing.getObjectSummaries(); List 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; } /** * 获取文件内容-阿里云 * @param bucketName * @param objectName * @return */ public String getObjectContent(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(); } public void uploadFileOss(File file) { if(file.isFile()){ String ossPath = file.getPath(); ossPath = ossPath.replace("/mnt/",""); ossPath = ossPath.replace("\\","/"); this.uploadOss(file.getPath(),ossPath); }else { File[] files = file.listFiles(); for (File file1 : files) { uploadFileOss(file1); } } } public Long getSize(String filePath){ OSSClient ossClient = new OSSClient(point, key, secrey); Long total = 0L; try { List files = listKeysFromAli( filePath); if (ObjectUtils.isEmpty(files)) { return 0L; } for (String file : files) { GetObjectRequest getObjectMetadataRequest = new GetObjectRequest(bucket, file); Long size = ossClient.getObjectMetadata(getObjectMetadataRequest).getContentLength(); total += size; } }catch (Exception e){ log.info("oss-getFileInfo-error:{}",e); }finally { if(ossClient != null){ ossClient.shutdown(); } } return total; } }