package com.fdkankan.fyun.oss; import com.aliyun.oss.OSS; import com.aliyun.oss.model.*; import com.fdkankan.fyun.face.AbstractFYunFileService; import com.fdkankan.fyun.oss.constant.OssConstants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; 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 @ConditionalOnProperty(name = "fyun.type",havingValue = "oss") public class OssFileService extends AbstractFYunFileService { private Logger log = LoggerFactory.getLogger(this.getClass().getName()); @Autowired private OSS ossClient; @Override public String uploadFile(String bucket, byte[] data, String remoteFilePath) { try { ossClient.putObject(bucket, remoteFilePath, new ByteArrayInputStream(data)); } catch (Exception e) { log.error("oss上传文件失败", e); e.printStackTrace(); } return null; } @Override public String uploadFile(String bucket, String filePath, String remoteFilePath) { return uploadFile(bucket, filePath, remoteFilePath, true, null); } @Override public String uploadFile(String bucket, String filePath, String remoteFilePath, Map headers) { return uploadFile(bucket, filePath, remoteFilePath, true, headers); } private String uploadFile(String bucket, String filePath, String remoteFilePath, Boolean shutdown,Map headers) { try { File file = new File(filePath); if (!file.exists()) { log.error("要上传的文件不存在:" + filePath); return null; } 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"); } if(org.apache.commons.lang3.ObjectUtils.isNotEmpty(headers)){ for (Map.Entry header : headers.entrySet()) { metadata.setHeader(header.getKey(),header.getValue()); } } ossClient.putObject(bucket, remoteFilePath, new File(filePath), metadata); } catch (Exception e) { log.error("oss上传文件失败", e); e.printStackTrace(); } return null; } @Override public String uploadFileByCommand(String bucket, String filePath, String remoteFilePath) { String ossPath = bucket + "/" + remoteFilePath; try { String command = String.format(OssConstants.UPLOAD_SH, ossPath, filePath); log.info("开始上传文件, ossPath:{}, srcPath:{}", ossPath, filePath); callshell(command); } catch (Exception e) { log.error("上传文件失败, ossPath:{}, srcPath:{}", ossPath, filePath); e.printStackTrace(); } return null; } @Override public void deleteFile(String bucket, String remoteFilePath) throws IOException { try { ossClient.deleteObject(bucket, remoteFilePath); } catch (Exception e) { log.error("OSS删除文件失败,key=" + remoteFilePath); e.printStackTrace(); } } @Override public void deleteFolder(String bucket, String remoteFolderPath) { try { List remoteFiles = listRemoteFiles(bucket, remoteFolderPath, false); if (CollectionUtils.isEmpty(remoteFiles)) { return; } DeleteObjectsRequest request = new DeleteObjectsRequest(bucket); request.setKeys(remoteFiles); ossClient.deleteObjects(request); } catch (Exception e) { log.error("OSS删除文件失败,key=" + remoteFolderPath); e.printStackTrace(); } } @Override public void uploadMulFiles(String bucket, Map filepaths) { try { for (Map.Entry entry : filepaths.entrySet()) { uploadFile(bucket, entry.getKey(), entry.getValue(), false,null); } } catch (Exception e) { log.error("OSS批量上传文件失败!"); } } @Override public List listRemoteFiles(String bucket, String sourcePath) { return listRemoteFiles(bucket, sourcePath, true); } private List listRemoteFiles(String bucket, String sourcePath, Boolean shutdown) { List keyList = new ArrayList<>(); try { boolean flag = true; String nextMaker = null; ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucket); //指定下一级文件 listObjectsRequest.setPrefix(sourcePath); //设置分页的页容量 listObjectsRequest.setMaxKeys(200); do { //获取下一页的起始点,它的下一项 listObjectsRequest.setMarker(nextMaker); ObjectListing objectListing = ossClient.listObjects(listObjectsRequest); List collect = objectListing.getObjectSummaries().parallelStream() .map(OSSObjectSummary::getKey).collect(Collectors.toList()); if (!CollectionUtils.isEmpty(collect)) { keyList.addAll(collect); } nextMaker = objectListing.getNextMarker(); //全部执行完后,为false flag = objectListing.isTruncated(); } while (flag); } catch (Exception e) { log.error("获取文件列表失败,path=" + sourcePath, e); e.printStackTrace(); } return keyList; } @Override public void copyFileBetweenBucket(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) { copyFileBetweenBucket(sourceBucketName, sourcePath, targetBucketName, targetPath, true); } private void copyFileBetweenBucket(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath, Boolean shutdown) { try { List files = listRemoteFiles(sourceBucketName, sourcePath, false); if (ObjectUtils.isEmpty(files)) { log.error("源文件夹为空:{}", sourcePath); } files.stream().forEach(file -> { ossClient.copyObject(sourceBucketName, file, targetBucketName, file.replace(sourcePath, targetPath)); }); } catch (Exception e) { log.error("列举文件目录失败,key=" + sourcePath); } } @Override public void copyFilesBetweenBucket(String sourceBucketName, String targetBucketName, Map pathMap) { if (ObjectUtils.isEmpty(pathMap)) { return; } try { for (Map.Entry entry : pathMap.entrySet()) { copyFileBetweenBucket(sourceBucketName, entry.getKey(), targetBucketName, entry.getValue(), false); } } catch (Exception e) { log.error("批量复制文件失败!"); } } @Override public String getFileContent(String bucketName, String remoteFilePath) { try { OSSObject ossObject = ossClient.getObject(bucketName, remoteFilePath); InputStream objectContent = ossObject.getObjectContent(); StringBuilder contentJson = new StringBuilder(); 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); } ossObject.close(); return contentJson.toString(); } catch (Exception e) { log.error("获取文件内容失败:{}", remoteFilePath); return null; } } @Override public boolean fileExist(String bucket, String objectName) { try { return ossClient.doesObjectExist(bucket, objectName); } catch (Exception e) { log.error("判断文件是否存在失败:{}", objectName); return false; } } @Override public void downloadFile(String bucket, String remoteFilePath, String localPath) { try { File localFile = new File(localPath); if (!localFile.getParentFile().exists()) { localFile.getParentFile().mkdirs(); } DownloadFileRequest request = new DownloadFileRequest(bucket, remoteFilePath); request.setDownloadFile(localPath); // 默认5个任务并发下载 request.setTaskNum(5); // 启动断点续传 request.setEnableCheckpoint(true); ossClient.downloadFile(request); } catch (Throwable throwable) { log.error("文件下载失败:{}", remoteFilePath); throwable.printStackTrace(); } } }