OssFileService.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package com.fdkankan.fyun.oss;
  2. import com.aliyun.oss.OSS;
  3. import com.aliyun.oss.model.*;
  4. import com.fdkankan.fyun.constant.FYunConstants;
  5. import com.fdkankan.fyun.constant.FYunTypeEnum;
  6. import com.fdkankan.fyun.face.AbstractFYunFileService;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  11. import org.springframework.stereotype.Component;
  12. import org.springframework.util.CollectionUtils;
  13. import org.springframework.util.ObjectUtils;
  14. import java.io.*;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import java.util.Map;
  18. import java.util.stream.Collectors;
  19. @Component
  20. @ConditionalOnProperty(name = "fyun.type", havingValue = "oss")
  21. public class OssFileService extends AbstractFYunFileService {
  22. private Logger log = LoggerFactory.getLogger(this.getClass().getName());
  23. @Autowired
  24. private OSS ossClient;
  25. @Override
  26. public String uploadFile(String bucket, byte[] data, String remoteFilePath) {
  27. try {
  28. ossClient.putObject(bucket, remoteFilePath, new ByteArrayInputStream(data));
  29. } catch (Exception e) {
  30. log.error("oss上传文件失败", e);
  31. e.printStackTrace();
  32. }
  33. return null;
  34. }
  35. @Override
  36. public String uploadFile(String bucket, String filePath, String remoteFilePath) {
  37. return uploadFile(bucket, filePath, remoteFilePath, null);
  38. }
  39. @Override
  40. public String uploadFile(String bucket, String filePath, String remoteFilePath, Map<String, String> headers) {
  41. try {
  42. File file = new File(filePath);
  43. if (!file.exists()) {
  44. log.error("要上传的文件不存在:" + filePath);
  45. return null;
  46. }
  47. ObjectMetadata metadata = new ObjectMetadata();
  48. if (filePath.contains(".jpg")) {
  49. metadata.setContentType("image/jpeg");
  50. }
  51. if (filePath.contains(".mp4")) {
  52. metadata.setContentType("video/mp4");
  53. }
  54. if (filePath.contains(".mp3")) {
  55. metadata.setContentType("audio/mp3");
  56. }
  57. if (org.apache.commons.lang3.ObjectUtils.isNotEmpty(headers)) {
  58. for (Map.Entry<String, String> header : headers.entrySet()) {
  59. metadata.setHeader(header.getKey(), header.getValue());
  60. }
  61. }
  62. ossClient.putObject(bucket, remoteFilePath, new File(filePath), metadata);
  63. log.info("文件上传成功,path:{}", filePath);
  64. } catch (Exception e) {
  65. log.error("oss上传文件失败", e);
  66. e.printStackTrace();
  67. }
  68. return null;
  69. }
  70. @Override
  71. public String uploadFileByCommand(String bucket, String filePath, String remoteFilePath) {
  72. try {
  73. String optType = new File(filePath).isDirectory() ? "folder" : "file";
  74. String command = String.format(fYunConstants.UPLOAD_SH, bucket, filePath, remoteFilePath, FYunTypeEnum.OSS.code(), optType);
  75. log.info("开始上传文件, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
  76. callshell(command);
  77. } catch (Exception e) {
  78. log.error("上传文件失败, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
  79. e.printStackTrace();
  80. }
  81. return null;
  82. }
  83. @Override
  84. public void downloadFileByCommand(String bucket, String filePath, String remoteFilePath) {
  85. try {
  86. String optType = new File(filePath).isDirectory() ? "folder" : "file";
  87. String command = String.format(fYunConstants.DOWNLOAD_SH, bucket, remoteFilePath, filePath, FYunTypeEnum.OSS.code(), optType);
  88. log.info("开始上传文件, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
  89. callshell(command);
  90. } catch (Exception e) {
  91. log.error("上传文件失败, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
  92. e.printStackTrace();
  93. }
  94. }
  95. @Override
  96. public void deleteFile(String bucket, String remoteFilePath) throws IOException {
  97. try {
  98. ossClient.deleteObject(bucket, remoteFilePath);
  99. } catch (Exception e) {
  100. log.error("OSS删除文件失败,key=" + remoteFilePath);
  101. e.printStackTrace();
  102. }
  103. }
  104. @Override
  105. public void deleteFolder(String bucket, String remoteFolderPath) {
  106. try {
  107. List<String> remoteFiles = listRemoteFiles(bucket, remoteFolderPath);
  108. if (CollectionUtils.isEmpty(remoteFiles)) {
  109. return;
  110. }
  111. DeleteObjectsRequest request = new DeleteObjectsRequest(bucket);
  112. request.setKeys(remoteFiles);
  113. ossClient.deleteObjects(request);
  114. } catch (Exception e) {
  115. log.error("OSS删除文件失败,key=" + remoteFolderPath);
  116. e.printStackTrace();
  117. }
  118. }
  119. @Override
  120. public void uploadMulFiles(String bucket, Map<String, String> filepaths) {
  121. try {
  122. for (Map.Entry<String, String> entry : filepaths.entrySet()) {
  123. uploadFile(bucket, entry.getKey(), entry.getValue(), null);
  124. }
  125. } catch (Exception e) {
  126. log.error("OSS批量上传文件失败!");
  127. }
  128. }
  129. @Override
  130. public List<String> listRemoteFiles(String bucket, String sourcePath) {
  131. List<String> keyList = new ArrayList<>();
  132. try {
  133. boolean flag = true;
  134. String nextMaker = null;
  135. ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucket);
  136. //指定下一级文件
  137. listObjectsRequest.setPrefix(sourcePath);
  138. //设置分页的页容量
  139. listObjectsRequest.setMaxKeys(200);
  140. do {
  141. //获取下一页的起始点,它的下一项
  142. listObjectsRequest.setMarker(nextMaker);
  143. ObjectListing objectListing = ossClient.listObjects(listObjectsRequest);
  144. List<String> collect = objectListing.getObjectSummaries().parallelStream()
  145. .map(OSSObjectSummary::getKey).collect(Collectors.toList());
  146. if (!CollectionUtils.isEmpty(collect)) {
  147. keyList.addAll(collect);
  148. }
  149. nextMaker = objectListing.getNextMarker();
  150. //全部执行完后,为false
  151. flag = objectListing.isTruncated();
  152. } while (flag);
  153. } catch (Exception e) {
  154. log.error("获取文件列表失败,path=" + sourcePath, e);
  155. e.printStackTrace();
  156. }
  157. return keyList;
  158. }
  159. @Override
  160. public void copyFileBetweenBucket(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) {
  161. try {
  162. List<String> files = listRemoteFiles(sourceBucketName, sourcePath);
  163. if (ObjectUtils.isEmpty(files)) {
  164. log.error("源文件夹为空:{}", sourcePath);
  165. }
  166. files.stream().forEach(file -> {
  167. ossClient.copyObject(sourceBucketName, file, targetBucketName, file.replace(sourcePath, targetPath));
  168. });
  169. } catch (Exception e) {
  170. log.error("列举文件目录失败,key=" + sourcePath);
  171. }
  172. }
  173. @Override
  174. public void copyFilesBetweenBucket(String sourceBucketName, String targetBucketName, Map<String, String> pathMap) {
  175. if (ObjectUtils.isEmpty(pathMap)) {
  176. return;
  177. }
  178. try {
  179. for (Map.Entry<String, String> entry : pathMap.entrySet()) {
  180. copyFileBetweenBucket(sourceBucketName, entry.getKey(), targetBucketName, entry.getValue());
  181. }
  182. } catch (Exception e) {
  183. log.error("批量复制文件失败!");
  184. }
  185. }
  186. @Override
  187. public String getFileContent(String bucketName, String remoteFilePath) {
  188. try {
  189. OSSObject ossObject = ossClient.getObject(bucketName, remoteFilePath);
  190. InputStream objectContent = ossObject.getObjectContent();
  191. StringBuilder contentJson = new StringBuilder();
  192. try (BufferedReader reader = new BufferedReader(new InputStreamReader(objectContent))) {
  193. while (true) {
  194. String line = reader.readLine();
  195. if (line == null) break;
  196. contentJson.append(line);
  197. }
  198. } catch (IOException e) {
  199. log.error("读取scene.json文件流失败", e);
  200. }
  201. ossObject.close();
  202. return contentJson.toString();
  203. } catch (Exception e) {
  204. log.error("获取文件内容失败:{}", remoteFilePath);
  205. return null;
  206. }
  207. }
  208. @Override
  209. public boolean fileExist(String bucket, String objectName) {
  210. try {
  211. return ossClient.doesObjectExist(bucket, objectName);
  212. } catch (Exception e) {
  213. log.error("判断文件是否存在失败:{}", objectName);
  214. return false;
  215. }
  216. }
  217. @Override
  218. public void downloadFile(String bucket, String remoteFilePath, String localPath) {
  219. try {
  220. File localFile = new File(localPath);
  221. if (!localFile.getParentFile().exists()) {
  222. localFile.getParentFile().mkdirs();
  223. }
  224. DownloadFileRequest request = new DownloadFileRequest(bucket, remoteFilePath);
  225. request.setDownloadFile(localPath);
  226. // 默认5个任务并发下载
  227. request.setTaskNum(5);
  228. // 启动断点续传
  229. request.setEnableCheckpoint(true);
  230. ossClient.downloadFile(request);
  231. } catch (Throwable throwable) {
  232. log.error("文件下载失败:{}", remoteFilePath);
  233. throwable.printStackTrace();
  234. }
  235. }
  236. }