package com.fdkankan.fyun.oss; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; import com.aliyun.oss.OSSClient; import com.aliyun.oss.model.*; import com.amazonaws.HttpMethod; import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.CannedAccessControlList; import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest; import com.amazonaws.services.s3.model.GetObjectRequest; import com.amazonaws.services.s3.model.PutObjectRequest; import com.amazonaws.services.s3.model.S3Object; import com.amazonaws.services.s3.model.S3ObjectInputStream; import com.amazonaws.services.s3.model.S3ObjectSummary; import com.fdkankan.fyun.constant.StorageType; import com.qiniu.common.Zone; import com.qiniu.storage.Configuration; import com.qiniu.storage.UploadManager; import java.util.stream.Collectors; import lombok.extern.slf4j.Slf4j; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.disk.DiskFileItemFactory; 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.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @Slf4j @Component public class UploadToOssUtil { Zone zone = Zone.autoZone(); Configuration config = new Configuration(zone); UploadManager uploadManager = new UploadManager(config); @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("${oss.sdk:4dscene}") private String bucketSdk; @Value("${upload.type:oss}") private String type; @Value("${aws.s3key:AKIAWCV5QFZ3ZNELKYUY}") private String s3key; @Value("${aws.s3secrey:epS5ghyR4LJ7rxk/qJO9ZYh6m9Oz6g5haKDu4yws}") private String s3secrey; @Value("${aws.s3bucket:4dkankan}") private String s3bucket; @Value("${local.path:/home/4dkankan}") private String localPath; //上传的数据是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: uploadAws(data,key1); 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 uploadSdk(String filePath, String key1) { log.info("开始上传文件 源路径:{},目标路径:{},type:{}" , filePath,key1,type); switch (type){ case "oss":uploadSdkOss(filePath,key1); break; case "aws": uploadAws(filePath,key1); break; case "local":uploadLocal(filePath,key1); break; } } public void upload2(String filePath, String key1) { log.info("开始上传文件 源路径:{},目标路径:{},type:{}" , filePath,key1,type); switch (type){ case "oss":upload2Oss(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 "aws": deleteS3Object(key1); break; case "local":FileUtil.del(key1); break; } } public int deleteFile(String prefix){ switch (type){ case "oss":deleteOssFile(prefix); break; case "aws": deleteS3Object(prefix); break; case "local":FileUtil.del(prefix); break; } return 1; } public void deleteOss(String key1){ OSSClient ossClient = new OSSClient(point, key, secrey); try { ossClient.deleteObject(bucket, key1); } catch (Exception e) { e.printStackTrace(); } } public void deleteOssFile(String prefix){ OSSClient ossClient = new OSSClient(point, key, secrey); ObjectListing objectListing = ossClient.listObjects(bucket, prefix); List sums = objectListing.getObjectSummaries(); try { for (OSSObjectSummary s : sums) { delete(s.getKey()); } } catch (IOException e) { e.printStackTrace(); } } public void uploadOss(byte[] data,String key1){ OSSClient ossClient = new OSSClient(point, key, secrey); try { ossClient.putObject(bucket, key1, new ByteArrayInputStream(data)); } catch (Exception e) { log.error(e.toString()+key1); } } public void uploadAws(byte[] data,String key1){ } 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{ uploadS3File(filePath, key1); }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 void uploadSdkOss(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(bucketSdk, key1, new File(filePath), metadata); } catch (Exception e) { log.error(e.toString() + filePath); } } public void upload2Oss(String filePath, String key1){ OSSClient ossClient = new OSSClient(point, key, secrey); try { ObjectMetadata metadata = new ObjectMetadata(); if(filePath.contains(".jpg")){ metadata.setContentType("image/jpeg"); } if(filePath.contains(".mp4")){ metadata.setContentType("video/mp4"); } if(filePath.contains(".mp3")){ metadata.setContentType("audio/mp3"); } ossClient.putObject(bucket, key1, new File(filePath), metadata); } catch (Exception e) { log.error(e.toString() + filePath); } } //上传的数据是文件夹,参数是文件夹路径,key是上传后的文件名 public void uploadMulFiles(Map filepaths) { if (filepaths == null) { return; } Long start = System.currentTimeMillis(); log.info("开始批量上传文件:"); if (filepaths.size() > 50) { filepaths.entrySet().parallelStream().forEach(entry->{ upload2(entry.getKey(), entry.getValue()); }); } else { filepaths.entrySet().parallelStream().forEach(entry->{ upload(entry.getKey(), entry.getValue()); }); } log.info("批量上传文件结束,用时:{}" ,(System.currentTimeMillis() - start)); } public Map getUploadS3Url(List urls){ if(urls == null || urls.size() <= 0){ return null; } BasicAWSCredentials awsCred = new BasicAWSCredentials(s3key, s3secrey); AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withCredentials(new AWSStaticCredentialsProvider(awsCred)) .withRegion(Regions.EU_WEST_2) .build(); // Set the pre-signed URL to expire after one hour. java.util.Date expiration = new java.util.Date(); long expTimeMillis = expiration.getTime(); expTimeMillis += 1000 * 60 * 60 * 8; expiration.setTime(expTimeMillis); //生成预签名URL log.info("生成预签名URL"); GeneratePresignedUrlRequest generatePresignedUrlRequest = null; URL url = null; Map map = new HashMap(); for(String path : urls){ // if(path.contains(".jpg") || path.contains("png")){ // generatePresignedUrlRequest = new GeneratePresignedUrlRequest(s3bucket, path) // .withMethod(HttpMethod.PUT) // .withExpiration(expiration) // .withContentType("image/jpeg"); // }else { generatePresignedUrlRequest = new GeneratePresignedUrlRequest(s3bucket, path) .withMethod(HttpMethod.PUT) .withExpiration(expiration); // } url = s3Client.generatePresignedUrl(generatePresignedUrlRequest); map.put(path, url.toString()); } return map; } public String upload5(String filePath, String key1) { OSSClient ossClient = new OSSClient(point, key, secrey); PutObjectResult result = null; try { File file = new File(filePath); if (!file.exists()) { log.error("要上传的文件不存在:" + filePath); } result = ossClient.putObject(bucket, key1, new File(filePath)); } catch (Exception e) { log.error(e.toString() + filePath); } log.info(" getETag : " + result.getETag()); log.info("1 : " + result.toString()); log.info("2 : " + result.getRequestId()); log.info("3 : " + result.getClientCRC()); log.info("4 : " + result.getResponse()); log.info("5 : " + result.getServerCRC()); return result.getETag(); } //海外亚马逊s3 /** * s3上传文件流 * * @param file 文件 * @param updatePath 上传路径[ eg: xxx/xxx ] */ public String updateS3LoadFile(MultipartFile file, String updatePath) { if (isEmpty(file)) { return null; } /** * 创建s3对象 */ BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey); AmazonS3 s3 = AmazonS3ClientBuilder.standard() .withCredentials(new AWSStaticCredentialsProvider(awsCreds)) .withRegion(Regions.EU_WEST_2) .build(); try { // 创建临时文件,程序运行结束,会自动删除 File localFile = File.createTempFile("temp", null); // 把文件写入内存中 file.transferTo(localFile); // 指定要上传到服务器上的路径 String key = updatePath; // 设置文件并设置公读 PutObjectRequest request = new PutObjectRequest(s3bucket, key, localFile); request.withCannedAcl(CannedAccessControlList.PublicRead); // 上传文件 com.amazonaws.services.s3.model.PutObjectResult putObjectResult = s3.putObject(request); if (StringUtils.isNotEmpty(putObjectResult.getETag())) { System.out.println("success"); return key; } return null; } catch (IOException e) { } return null; } /** * s3上传文件 * @param filePath * @param key1 * @throws IOException */ private void uploadS3File(String filePath, String key1) throws Exception { /** * 创建s3对象 */ BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey); AmazonS3 s3 = AmazonS3ClientBuilder.standard() .withCredentials(new AWSStaticCredentialsProvider(awsCreds)) .withRegion(Regions.EU_WEST_2) .build(); try{ File file = new File(filePath); if(!file.exists()){ log.info("要上传s3的文件不存在"); return; } // 设置文件并设置公读 com.amazonaws.services.s3.model.ObjectMetadata metadata = new com.amazonaws.services.s3.model.ObjectMetadata(); if(filePath.contains(".jpg")){ metadata.setContentType("image/jpeg"); } if(filePath.contains(".png")){ metadata.setContentType("image/png"); } PutObjectRequest request = new PutObjectRequest(s3bucket, key1, file); request.withCannedAcl(CannedAccessControlList.PublicRead); request.withMetadata(metadata); // 上传文件 com.amazonaws.services.s3.model.PutObjectResult putObjectResult = s3.putObject(request); if (StringUtils.isNotEmpty(putObjectResult.getETag())) { log.info("s3上传文件成功:" + key1); } }catch (Exception e){ throw e; }finally { s3.shutdown(); } } /** * 删除单个文件 * * @param filePath 文件路径[ eg: /head/xxxx.jpg ] * @return */ public void deleteS3Object(String filePath) { /** * 创建s3对象 */ BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey); AmazonS3 s3 = AmazonS3ClientBuilder.standard() .withCredentials(new AWSStaticCredentialsProvider(awsCreds)) .withRegion(Regions.EU_WEST_2) .build(); if (filePath.startsWith("/")) { filePath = filePath.substring(1); } try { s3.deleteObject(s3bucket, filePath); } catch (Exception e) { } } /** * 获取文件类型 */ 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; } private static MultipartFile getMulFileByPath(String picPath) { FileItem fileItem = createFileItem(picPath); MultipartFile mfile = new CommonsMultipartFile(fileItem); return mfile; } private static FileItem createFileItem(String filePath) { FileItemFactory factory = new DiskFileItemFactory(16, null); String textFieldName = "textField"; int num = filePath.lastIndexOf("."); String extFile = filePath.substring(num); FileItem item = factory.createItem(textFieldName, "text/plain", true, "MyFileName" + extFile); File newfile = new File(filePath); int bytesRead = 0; byte[] buffer = new byte[8192]; try { FileInputStream fis = new FileInputStream(newfile); OutputStream os = item.getOutputStream(); while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) { os.write(buffer, 0, bytesRead); } os.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } return item; } public List 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 listKeysFromAli(String sourcePath) { List keyList = new ArrayList<>(); OSSClient ossClient = new OSSClient(point, key, secrey); 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); ossClient.shutdown(); return keyList; } /** * 获得文件列表-亚马逊 * @return */ public List listKeysFromAws(String sourcePath) { List keyList = new ArrayList<>(); BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey); AmazonS3 s3 = AmazonS3ClientBuilder.standard() .withCredentials(new AWSStaticCredentialsProvider(awsCreds)) .withRegion(Regions.EU_WEST_2) .build(); boolean flag = true; String nextMaker = null; com.amazonaws.services.s3.model.ListObjectsRequest listObjectsRequest = new com.amazonaws.services.s3.model.ListObjectsRequest(); listObjectsRequest.setBucketName(this.bucket); listObjectsRequest.setPrefix(sourcePath); listObjectsRequest.setMaxKeys(200); do{ listObjectsRequest.setMarker(nextMaker); com.amazonaws.services.s3.model.ObjectListing objectListing = s3.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(); flag = objectListing.isTruncated(); }while (flag); s3.shutdown(); return keyList; } /** * 获得文件列表-阿里云 * @return */ public List listKeysFromLocal(String sourcePath) { List keyList = new ArrayList<>(); return keyList; } /** *

拷贝目录 *

* @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: this.copyFilesFromAws(sourcePath, targetPath); break; case LOCAL: this.copyFilesFromLocal(sourcePath, targetPath); } } /** *

拷贝文件 *

* @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: this.copyObjectFromAws(sourceKey, targetKey); break; } } /** *

拷贝-阿里云 *

* @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(); } /** *

拷贝-阿里云 *

* @author dengsixing * @date 2022/1/18 * @param sourcePath * @param targetPath **/ public void copyFilesFromAli(String sourcePath, String targetPath) throws IOException { //获取源文件列表 List 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(); } /** *

拷贝-亚马逊 *

* @author dengsixing * @date 2022/1/18 * @param sourcePath * @param targetPath **/ public void copyFilesFromAws(String sourcePath, String targetPath){ try { List sourceKeyList = this.listKeysFromAws(sourcePath); /** * 创建s3对象 */ BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey); AmazonS3 s3 = AmazonS3ClientBuilder.standard() .withCredentials(new AWSStaticCredentialsProvider(awsCreds)) .withRegion(Regions.EU_WEST_2)//s3 地区位置 .build(); // 复制文件 sourceKeyList.parallelStream().forEach(key -> { log.info("开始复制:" + key); s3.copyObject(this.bucket, key, this.bucket, key.replace(sourcePath, targetPath)); log.info("复制成功:" + key); }); s3.shutdown(); } catch (Exception ase) { log.error("amazonS拷贝异常 " + ase.getMessage(), ase); } } /** *

拷贝-亚马逊 *

* @author dengsixing * @date 2022/1/18 * @param sourceKey * @param targetKey **/ public void copyObjectFromAws(String sourceKey, String targetKey){ BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey); AmazonS3 s3 = AmazonS3ClientBuilder.standard() .withCredentials(new AWSStaticCredentialsProvider(awsCreds)) .withRegion(Regions.EU_WEST_2)//s3 地区位置 .build(); // 复制文件 log.info("开始复制:" + sourceKey); s3.copyObject(this.bucket, sourceKey, this.bucket, targetKey); log.info("复制成功:" + sourceKey); s3.shutdown(); } /** *

拷贝-本地 *

* @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: return this.getObjectContentFromAws(bucketName, objectName); 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); } ossClient.shutdown(); }catch (Exception e){ log.error("oos找不到文件,文件路径:{}", objectName); } return contentJson.toString(); } /** * 获取文件内容-阿里云 * @param objectName * @return */ public boolean existOnAli(String objectName){ //创建oss客户端 OSSClient ossClient = new OSSClient(point, key, secrey); // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。 OSSObject ossObject = ossClient.getObject(bucket, objectName); String key = ossObject.getKey(); if(StrUtil.isNotEmpty(key)) return true; return false; } /** * 获取文件内容-亚马逊 * @param bucketName * @param objectName * @return */ public String getObjectContentFromAws(String bucketName, String objectName){ try { /** * 创建s3对象 */ BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey); AmazonS3 s3 = AmazonS3ClientBuilder.standard() .withCredentials(new AWSStaticCredentialsProvider(awsCreds)) .withRegion(Regions.EU_WEST_2) .build(); GetObjectRequest request = new GetObjectRequest(bucketName,objectName); S3Object object = s3.getObject(request); S3ObjectInputStream inputStream = object.getObjectContent(); StringBuilder content = new StringBuilder(); try(BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))){ while (true) { String line = reader.readLine(); if (line == null) break; content.append(line); } } catch (IOException e) { log.error("读取aws文件流失败", e); } return content.toString(); } catch (Exception ase) { log.error("amazonS3下载文件异常 " + ase.getMessage(), ase); } return null; } /** * 获取文件内容-亚马逊 * @param objectName * @return */ public boolean existOnAws(String objectName){ BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey); AmazonS3 s3 = AmazonS3ClientBuilder.standard() .withCredentials(new AWSStaticCredentialsProvider(awsCreds)) .withRegion(Regions.EU_WEST_2) .build(); GetObjectRequest request = new GetObjectRequest(bucket,objectName); S3Object s3Object = s3.getObject(request); String key = s3Object.getKey(); if(StrUtil.isNotEmpty(key)) return true; 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: return this.existOnAws(key); default: return false; } } /** * 获取文件内容-本地 * @param objectName * @return */ public String getObjectContentFromLocal(String objectName){ // TODO: 2022/1/21 return null; } /** * oss下载文件到本地 * @param key * @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 this.downFromS3(objectName, localPath); } return false; } /** * 从阿里云oss下载文件到本地 * @param key 云端文件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); ossClient.getObject(request, new File(localPath)); return true; }catch (Exception e){ log.error("阿里云oss文件下载失败,key=" + objectName, e); ossClient.shutdown(); } return false; } /** * 从s3下载文件到本地 * @param key 云端文件k地址 * @param localPath 本地文件地址 * @return */ public boolean downFromS3(String objectName, String localPath) { BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey); AmazonS3 s3 = AmazonS3ClientBuilder.standard() .withCredentials(new AWSStaticCredentialsProvider(awsCreds)) .withRegion(Regions.EU_WEST_2) .build(); try { GetObjectRequest request = new GetObjectRequest(this.bucket,objectName); s3.getObject(request,new File(localPath)); return true; } catch (Exception e) { log.error("amazonS3下载文件失败,key=" + objectName, e); s3.shutdown(); } return false; } }