12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031 |
- 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<OSSObjectSummary> 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<String, String> 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<String, String> getUploadS3Url(List<String> 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<String, String> 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<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);
- 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);
- ossClient.shutdown();
- return keyList;
- }
- /**
- * 获得文件列表-亚马逊
- * @return
- */
- public List<String> listKeysFromAws(String sourcePath) {
- List<String> 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<S3ObjectSummary> 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();
- flag = objectListing.isTruncated();
- }while (flag);
- s3.shutdown();
- return keyList;
- }
- /**
- * 获得文件列表-阿里云
- * @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:
- this.copyFilesFromAws(sourcePath, targetPath);
- 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:
- this.copyObjectFromAws(sourceKey, targetKey);
- 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 copyFilesFromAws(String sourcePath, String targetPath){
- try {
- List<String> 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);
- }
- }
- /**
- * <p>
- 拷贝-亚马逊
- * </p>
- * @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();
- }
- /**
- * <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:
- 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;
- }
- }
|