Browse Source

调整云文件操作逻辑

tianboguang 3 years ago
parent
commit
3f7458ba56

+ 8 - 71
4dkankan-utils-fyun-oss/src/main/java/com/fdkankan/fyun/oss/OssFileService.java

@@ -3,8 +3,7 @@ package com.fdkankan.fyun.oss;
 import com.aliyun.oss.OSS;
 import com.aliyun.oss.model.*;
 import com.fdkankan.common.util.CreateObjUtil;
-import com.fdkankan.fyun.config.FYunFileConfig;
-import com.fdkankan.fyun.face.FYunFileService;
+import com.fdkankan.fyun.face.AbstractFYunFileService;
 import com.fdkankan.fyun.oss.constant.OssConstants;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -14,24 +13,19 @@ import org.springframework.util.CollectionUtils;
 import org.springframework.util.ObjectUtils;
 
 import java.io.*;
-import java.net.FileNameMap;
-import java.net.URLConnection;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
 
 @Component
-public class OssFileService implements FYunFileService {
+public class OssFileService extends AbstractFYunFileService {
 
     private Logger log = LoggerFactory.getLogger(this.getClass().getName());
 
     @Autowired
     private OSS ossClient;
 
-    @Autowired
-    private FYunFileConfig fYunFileConfig;
-
     @Override
     public void uploadFile(String bucket, byte[] data, String remoteFilePath) throws Exception {
         try {
@@ -47,20 +41,10 @@ public class OssFileService implements FYunFileService {
     }
 
     @Override
-    public void uploadFile(byte[] data, String remoteFilePath) throws Exception {
-        uploadFile(fYunFileConfig.getBucket(), data, remoteFilePath);
-    }
-
-    @Override
     public void uploadFile(String bucket, String filePath, String remoteFilePath) throws Exception {
         uploadFile(bucket, filePath, remoteFilePath, true);
     }
 
-    @Override
-    public void uploadFile(String filePath, String remoteFilePath) throws Exception {
-        uploadFile(fYunFileConfig.getBucket(), filePath, remoteFilePath, true);
-    }
-
     private void uploadFile(String bucket, String filePath, String remoteFilePath, Boolean shutdown) throws Exception {
         try {
             File file = new File(filePath);
@@ -103,11 +87,6 @@ public class OssFileService implements FYunFileService {
     }
 
     @Override
-    public void uploadFileByCommand(String filePath, String remoteFilePath) throws Exception {
-        uploadFileByCommand(fYunFileConfig.getBucket(), filePath, remoteFilePath);
-    }
-
-    @Override
     public void deleteFile(String bucket, String remoteFilePath) throws IOException {
         try {
             ossClient.deleteObject(bucket, remoteFilePath);
@@ -122,15 +101,10 @@ public class OssFileService implements FYunFileService {
     }
 
     @Override
-    public void deleteFile(String remoteFilePath) throws IOException {
-        deleteFile(fYunFileConfig.getBucket(), remoteFilePath);
-    }
-
-    @Override
     public void deleteFolder(String bucket, String remoteFolderPath) throws Exception {
         try {
             List<String> remoteFiles = listRemoteFiles(bucket, remoteFolderPath, false);
-            if(CollectionUtils.isEmpty(remoteFiles)){
+            if (CollectionUtils.isEmpty(remoteFiles)) {
                 return;
             }
             DeleteObjectsRequest request = new DeleteObjectsRequest(bucket);
@@ -147,11 +121,6 @@ public class OssFileService implements FYunFileService {
     }
 
     @Override
-    public void deleteFolder(String remoteFolderPath) throws Exception {
-        deleteFolder(fYunFileConfig.getBucket(), remoteFolderPath);
-    }
-
-    @Override
     public void uploadMulFiles(String bucket, Map<String, String> filepaths) throws Exception {
         try {
             for (Map.Entry<String, String> entry : filepaths.entrySet()) {
@@ -166,21 +135,12 @@ public class OssFileService implements FYunFileService {
         }
     }
 
-    @Override
-    public void uploadMulFiles(Map<String, String> filepaths) throws Exception {
-        uploadMulFiles(fYunFileConfig.getBucket(), filepaths);
-    }
 
     @Override
     public List<String> listRemoteFiles(String bucket, String sourcePath) throws Exception {
         return listRemoteFiles(bucket, sourcePath, true);
     }
 
-    @Override
-    public List<String> listRemoteFiles(String sourcePath) throws Exception {
-        return listRemoteFiles(fYunFileConfig.getBucket(), sourcePath);
-    }
-
     private List<String> listRemoteFiles(String bucket, String sourcePath, Boolean shutdown) throws Exception {
         List<String> keyList = new ArrayList<>();
         try {
@@ -217,10 +177,10 @@ public class OssFileService implements FYunFileService {
 
     @Override
     public void copyFileBetweenFyun(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) throws Exception {
-        copyFileBetweenFyun(sourceBucketName,sourcePath,targetBucketName,targetPath,true);
+        copyFileBetweenFyun(sourceBucketName, sourcePath, targetBucketName, targetPath, true);
     }
 
-    private void copyFileBetweenFyun(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath,Boolean shutdown) throws Exception {
+    private void copyFileBetweenFyun(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath, Boolean shutdown) throws Exception {
         try {
             List<String> files = listRemoteFiles(sourceBucketName, sourcePath, false);
             if (ObjectUtils.isEmpty(files)) {
@@ -239,11 +199,6 @@ public class OssFileService implements FYunFileService {
     }
 
     @Override
-    public void copyFileBetweenFyun(String sourcePath, String targetBucketName, String targetPath) throws Exception {
-        copyFileBetweenFyun(fYunFileConfig.getBucket(),sourcePath, targetBucketName, targetPath);
-    }
-
-    @Override
     public void copyFilesBetweenFyun(String sourceBucketName, String targetBucketName, Map<String, String> pathMap) throws Exception {
         if (ObjectUtils.isEmpty(pathMap)) {
             return;
@@ -262,11 +217,6 @@ public class OssFileService implements FYunFileService {
     }
 
     @Override
-    public void copyFilesBetweenFyun(String targetBucketName, Map<String, String> pathMap) throws Exception {
-        copyFilesBetweenFyun(fYunFileConfig.getBucket(),targetBucketName,pathMap);
-    }
-
-    @Override
     public String getFileContent(String bucketName, String remoteFilePath) throws Exception {
         try {
             OSSObject ossObject = ossClient.getObject(bucketName, remoteFilePath);
@@ -293,11 +243,6 @@ public class OssFileService implements FYunFileService {
     }
 
     @Override
-    public String getFileContent(String remoteFilePath) throws Exception {
-        return getFileContent(fYunFileConfig.getBucket(), remoteFilePath);
-    }
-
-    @Override
     public boolean fileExist(String bucket, String objectName) throws Exception {
         try {
             return ossClient.doesObjectExist(bucket, objectName);
@@ -312,15 +257,10 @@ public class OssFileService implements FYunFileService {
     }
 
     @Override
-    public boolean fileExist(String objectName) throws Exception {
-        return fileExist(fYunFileConfig.getBucket(), objectName);
-    }
-
-    @Override
     public void downloadFile(String bucket, String remoteFilePath, String localPath) throws Exception {
         try {
             File localFile = new File(localPath);
-            if(!localFile.getParentFile().exists()){
+            if (!localFile.getParentFile().exists()) {
                 localFile.getParentFile().mkdirs();
             }
             DownloadFileRequest request = new DownloadFileRequest(bucket, remoteFilePath);
@@ -333,15 +273,12 @@ public class OssFileService implements FYunFileService {
         } catch (Throwable throwable) {
             log.error("文件下载失败:{}", remoteFilePath);
             throwable.printStackTrace();
-        }finally {
+        } finally {
             if (!ObjectUtils.isEmpty(ossClient)) {
                 ossClient.shutdown();
             }
         }
     }
 
-    @Override
-    public void downloadFile(String remoteFilePath, String localPath) throws Exception {
-        downloadFile(fYunFileConfig.getBucket(),remoteFilePath,localPath);
-    }
+
 }

+ 10 - 0
4dkankan-utils-fyun-parent/src/main/java/com/fdkankan/fyun/constant/FYunConstants.java

@@ -0,0 +1,10 @@
+package com.fdkankan.fyun.constant;
+
+public class FYunConstants {
+    /**
+     * oss文件上传命令
+     * 第一个参数是oss路径,要包含bucket名称
+     * 第二个参数是本地文件路径
+     */
+    public static final String UPLOAD_SH = "bash /opt/ossutil/upload.sh %s %s";
+}

+ 77 - 0
4dkankan-utils-fyun-parent/src/main/java/com/fdkankan/fyun/face/AbstractFYunFileService.java

@@ -0,0 +1,77 @@
+package com.fdkankan.fyun.face;
+
+import com.fdkankan.fyun.config.FYunFileConfig;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+
+@Component
+public abstract class AbstractFYunFileService implements FYunFileServiceInterface {
+
+    @Autowired
+    public FYunFileConfig fYunFileConfig;
+
+    @Override
+    public void uploadFile(byte[] data, String remoteFilePath) throws Exception {
+        uploadFile(fYunFileConfig.getBucket(), data, remoteFilePath);
+    }
+
+    @Override
+    public void uploadFile(String filePath, String remoteFilePath) throws Exception {
+        uploadFile(fYunFileConfig.getBucket(), filePath, remoteFilePath);
+    }
+
+
+    @Override
+    public void uploadFileByCommand(String filePath, String remoteFilePath) throws Exception {
+        uploadFileByCommand(fYunFileConfig.getBucket(), filePath, remoteFilePath);
+    }
+
+    @Override
+    public void deleteFile(String remoteFilePath) throws IOException {
+        deleteFile(fYunFileConfig.getBucket(), remoteFilePath);
+    }
+
+    @Override
+    public void deleteFolder(String remoteFolderPath) throws Exception {
+        deleteFolder(fYunFileConfig.getBucket(), remoteFolderPath);
+    }
+
+    @Override
+    public void uploadMulFiles(Map<String, String> filepaths) throws Exception {
+        uploadMulFiles(fYunFileConfig.getBucket(), filepaths);
+    }
+
+    @Override
+    public List<String> listRemoteFiles(String sourcePath) throws Exception {
+        return listRemoteFiles(fYunFileConfig.getBucket(), sourcePath);
+    }
+
+    @Override
+    public void copyFileBetweenFyun(String sourcePath, String targetBucketName, String targetPath) throws Exception {
+        copyFileBetweenFyun(fYunFileConfig.getBucket(),sourcePath, targetBucketName, targetPath);
+    }
+
+    @Override
+    public void copyFilesBetweenFyun(String targetBucketName, Map<String, String> pathMap) throws Exception {
+        copyFilesBetweenFyun(fYunFileConfig.getBucket(),targetBucketName,pathMap);
+    }
+
+    @Override
+    public String getFileContent(String remoteFilePath) throws Exception {
+        return getFileContent(fYunFileConfig.getBucket(), remoteFilePath);
+    }
+
+    @Override
+    public boolean fileExist(String objectName) throws Exception {
+        return fileExist(fYunFileConfig.getBucket(), objectName);
+    }
+
+    @Override
+    public void downloadFile(String remoteFilePath, String localPath) throws Exception {
+        downloadFile(fYunFileConfig.getBucket(),remoteFilePath,localPath);
+    }
+}

+ 1 - 2
4dkankan-utils-fyun-parent/src/main/java/com/fdkankan/fyun/face/FYunFileService.java

@@ -7,7 +7,7 @@ import java.util.List;
 import java.util.Map;
 
 @Component
-public interface FYunFileService {
+public interface FYunFileServiceInterface {
 
 	/**
 	 *  上传文件
@@ -26,7 +26,6 @@ public interface FYunFileService {
 	 */
 	void uploadFile(byte[] data, String remoteFilePath) throws Exception;
 
-
 	/**
 	 * 上传本地文件
 	 * @param bucket 目标bucket

+ 302 - 0
4dkankan-utils-fyun-s3/src/main/java/com/fdkankan/fyun/s3/S3FileService.java

@@ -0,0 +1,302 @@
+package com.fdkankan.fyun.s3;
+
+import cn.hutool.core.collection.CollUtil;
+import com.amazonaws.services.s3.AmazonS3;
+import com.amazonaws.services.s3.model.*;
+import com.fdkankan.common.util.CreateObjUtil;
+import com.fdkankan.fyun.constant.FYunConstants;
+import com.fdkankan.fyun.face.AbstractFYunFileService;
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.util.ObjectUtils;
+
+import java.io.*;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+@Component
+public class S3FileService extends AbstractFYunFileService {
+
+    private Logger log = LoggerFactory.getLogger(this.getClass().getName());
+
+    @Autowired
+    private AmazonS3 s3;
+
+    @Override
+    public void uploadFile(String bucket, byte[] data, String remoteFilePath) throws Exception {
+        try {
+            ObjectMetadata metadata = new ObjectMetadata();
+            PutObjectRequest request = new PutObjectRequest(bucket, remoteFilePath, new ByteArrayInputStream(data), metadata);
+            request.withCannedAcl(CannedAccessControlList.PublicRead);
+            s3.putObject(request);
+        } catch (Exception e) {
+            log.error("s3上传文件失败", e);
+        } finally {
+            if (s3 != null) {
+                s3.shutdown();
+            }
+        }
+    }
+
+    @Override
+    public void uploadFile(String bucket, String filePath, String remoteFilePath) throws Exception {
+        uploadFile(bucket, filePath, remoteFilePath,true);
+    }
+
+    private void uploadFile(String bucket, String filePath, String remoteFilePath, Boolean shutdown) throws Exception {
+        try {
+            File file = new File(filePath);
+            if (!file.exists()) {
+                log.info("要上传s3的文件不存在");
+                return;
+            }
+
+            // 设置文件并设置公读
+            ObjectMetadata metadata = new ObjectMetadata();
+            if (filePath.contains(".jpg")) {
+                metadata.setContentType("image/jpeg");
+            }
+            if (filePath.contains(".png")) {
+                metadata.setContentType("image/png");
+            }
+            PutObjectRequest request = new PutObjectRequest(bucket, remoteFilePath, file);
+            request.withCannedAcl(CannedAccessControlList.PublicRead);
+            request.withMetadata(metadata);
+
+            // 上传文件
+            PutObjectResult putObjectResult = s3.putObject(request);
+            if (StringUtils.isNotEmpty(putObjectResult.getETag())) {
+                log.info("s3上传文件成功:" + remoteFilePath);
+            }
+        } catch (Exception e) {
+            throw e;
+        } finally {
+            if ((ObjectUtils.isEmpty(shutdown) || shutdown) && !ObjectUtils.isEmpty(s3)) {
+                s3.shutdown();
+            }
+        }
+    }
+
+    @Override
+    public void uploadFileByCommand(String bucket, String filePath, String remoteFilePath) throws Exception {
+        String ossPath = bucket + "/" + remoteFilePath;
+        try {
+            String command = String.format(FYunConstants.UPLOAD_SH, ossPath, filePath);
+            log.info("开始上传文件, ossPath:{}, srcPath:{}", ossPath, filePath);
+            CreateObjUtil.callshell(command);
+        } catch (Exception e) {
+            log.error("上传文件失败, ossPath:{}, srcPath:{}", ossPath, filePath);
+            e.printStackTrace();
+        }
+    }
+
+    @Override
+    public void deleteFile(String bucket, String remoteFilePath) throws IOException {
+        if (remoteFilePath.startsWith("/")) {
+            remoteFilePath = remoteFilePath.substring(1);
+        }
+        try {
+            s3.deleteObject(bucket, remoteFilePath);
+        } catch (Exception e) {
+            log.error("s3删除文件失败,key=" + remoteFilePath, e);
+        } finally {
+            if (s3 != null) {
+                s3.shutdown();
+            }
+        }
+    }
+
+    @Override
+    public void deleteFolder(String bucket, String remoteFolderPath) throws Exception {
+        try {
+            int maxKeys = 200;
+            String nextMaker = null;
+
+            ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
+            listObjectsRequest.setBucketName(bucket);
+            listObjectsRequest.setPrefix(remoteFolderPath);
+            listObjectsRequest.setMaxKeys(maxKeys);
+
+            com.amazonaws.services.s3.model.ObjectListing objectListing;
+            do {
+                listObjectsRequest.setMarker(nextMaker);
+                objectListing = s3.listObjects(listObjectsRequest);
+                List<S3ObjectSummary> objectSummaries = objectListing.getObjectSummaries();
+                List<DeleteObjectsRequest.KeyVersion> keys = objectSummaries.stream().map(summary -> new DeleteObjectsRequest.KeyVersion(summary.getKey())).collect(Collectors.toList());
+
+                DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(bucket)
+                                .withKeys(keys).withQuiet(false);
+                DeleteObjectsResult delObjRes = s3.deleteObjects(multiObjectDeleteRequest);
+                int successfulDeletes = delObjRes.getDeletedObjects().size();
+                log.info("删除aws文件成功,删除文件数;{}", successfulDeletes);
+                nextMaker = objectListing.getNextMarker();
+            } while (objectListing.isTruncated());
+        } catch (Exception e) {
+            log.error("删除was文件失败,path=" + remoteFolderPath, e);
+        } finally {
+            if (s3 != null) {
+                s3.shutdown();
+            }
+        }
+    }
+
+    @Override
+    public void uploadMulFiles(String bucket, Map<String, String> filepaths) throws Exception {
+        try {
+            for (Map.Entry<String, String> entry : filepaths.entrySet()) {
+                uploadFile(bucket, entry.getKey(), entry.getValue(), false);
+            }
+        } catch (Exception e) {
+            log.error("OSS批量上传文件失败!");
+        } finally {
+            if (!ObjectUtils.isEmpty(s3)) {
+                s3.shutdown();
+            }
+        }
+    }
+
+    @Override
+    public List<String> listRemoteFiles(String bucket, String sourcePath) throws Exception {
+        return listRemoteFiles(bucket, sourcePath, true);
+    }
+
+    private List<String> listRemoteFiles(String bucket, String sourcePath, Boolean shutdown) throws Exception {
+        List<String> keyList = new ArrayList<>();
+        try {
+            boolean flag = true;
+            String nextMaker = null;
+            ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
+            listObjectsRequest.setBucketName(bucket);
+            listObjectsRequest.setPrefix(sourcePath);
+            listObjectsRequest.setMaxKeys(200);
+
+            do {
+                listObjectsRequest.setMarker(nextMaker);
+                ObjectListing objectListing = s3.listObjects(listObjectsRequest);
+                List<S3ObjectSummary> objectSummaries = objectListing.getObjectSummaries();
+                List<String> collect = objectSummaries.stream().map(S3ObjectSummary::getKey).collect(Collectors.toList());
+                if (CollUtil.isNotEmpty(collect)) {
+                    keyList.addAll(collect);
+                }
+                nextMaker = objectListing.getNextMarker();
+                flag = objectListing.isTruncated();
+            } while (flag);
+        } catch (Exception e) {
+            log.error("获取文件列表失败,path=" + sourcePath, e);
+            e.printStackTrace();
+        } finally {
+            if ((ObjectUtils.isEmpty(shutdown) || shutdown) && !ObjectUtils.isEmpty(s3)) {
+                s3.shutdown();
+            }
+        }
+        return keyList;
+    }
+
+    @Override
+    public void copyFileBetweenFyun(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) throws Exception {
+        copyFileBetweenFyun(sourceBucketName, sourcePath, targetBucketName, targetPath, true);
+    }
+
+    private void copyFileBetweenFyun(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath, Boolean shutdown) throws Exception {
+        try {
+            List<String> files = listRemoteFiles(sourceBucketName, sourcePath, false);
+            if (ObjectUtils.isEmpty(files)) {
+                log.error("源文件夹为空:{}", sourcePath);
+            }
+            files.stream().forEach(file -> {
+				CopyObjectRequest request = new CopyObjectRequest(sourceBucketName, file, targetBucketName, file.replace(sourcePath, targetPath));
+				request.withCannedAccessControlList(CannedAccessControlList.PublicRead);
+				s3.copyObject(request);
+            });
+        } catch (Exception e) {
+            log.error("列举文件目录失败,key=" + sourcePath);
+        } finally {
+            if ((ObjectUtils.isEmpty(shutdown) || shutdown) && !ObjectUtils.isEmpty(s3)) {
+                s3.shutdown();
+            }
+        }
+    }
+
+    @Override
+    public void copyFilesBetweenFyun(String sourceBucketName, String targetBucketName, Map<String, String> pathMap) throws Exception {
+        if (ObjectUtils.isEmpty(pathMap)) {
+            return;
+        }
+        try {
+            for (Map.Entry<String, String> entry : pathMap.entrySet()) {
+                copyFileBetweenFyun(sourceBucketName, entry.getKey(), targetBucketName, entry.getValue(), false);
+            }
+        } catch (Exception e) {
+            log.error("批量复制文件失败!");
+        } finally {
+            if (!ObjectUtils.isEmpty(s3)) {
+                s3.shutdown();
+            }
+        }
+    }
+
+    @Override
+    public String getFileContent(String bucketName, String remoteFilePath) throws Exception {
+        try {
+			GetObjectRequest request  = new GetObjectRequest(bucketName,remoteFilePath);
+			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 e) {
+            log.error("获取文件内容失败:{}", remoteFilePath);
+            return null;
+        } finally {
+            if (!ObjectUtils.isEmpty(s3)) {
+                s3.shutdown();
+            }
+        }
+    }
+
+    @Override
+    public boolean fileExist(String bucket, String objectName) throws Exception {
+        try {
+            return s3.doesObjectExist(bucket, objectName);
+        } catch (Exception e) {
+            log.error("判断文件是否存在失败:{}", objectName);
+            return false;
+        } finally {
+            if (!ObjectUtils.isEmpty(s3)) {
+                s3.shutdown();
+            }
+        }
+    }
+
+    @Override
+    public void downloadFile(String bucket, String remoteFilePath, String localPath) throws Exception {
+        try {
+            File localFile = new File(localPath);
+            if (!localFile.getParentFile().exists()) {
+                localFile.getParentFile().mkdirs();
+            }
+			GetObjectRequest request = new GetObjectRequest(bucket, remoteFilePath);
+			s3.getObject(request,new File(localPath));
+        } catch (Throwable throwable) {
+            log.error("文件下载失败:{}", remoteFilePath);
+            throwable.printStackTrace();
+        } finally {
+            if (!ObjectUtils.isEmpty(s3)) {
+                s3.shutdown();
+            }
+        }
+    }
+}

+ 32 - 0
4dkankan-utils-fyun-s3/src/main/java/com/fdkankan/fyun/s3/config/S3Config.java

@@ -0,0 +1,32 @@
+package com.fdkankan.fyun.s3.config;
+
+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.fdkankan.fyun.config.FYunFileConfig;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.config.ConfigurableBeanFactory;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Scope;
+import org.springframework.stereotype.Component;
+
+@Component
+public class S3Config {
+
+    @Autowired
+    private FYunFileConfig fYunFileConfig;
+
+    /**
+     * 配置多实例,且使用时候要手动shutdown,否则会造成内存溢出
+     * @return
+     */
+    @Bean
+    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
+    public AmazonS3 amazonS3(){
+        BasicAWSCredentials awsCreds = new BasicAWSCredentials(fYunFileConfig.getKey(), fYunFileConfig.getSecret());
+        return AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCreds))
+                .withRegion(Regions.EU_WEST_2).build();
+    }
+}