package com.fdkankan.fyun.oss; import cn.hutool.core.collection.CollUtil; import com.aliyun.oss.HttpMethod; import com.aliyun.oss.OSS; import com.aliyun.oss.model.*; import com.fdkankan.fyun.constant.FYunTypeEnum; import com.fdkankan.fyun.face.AbstractFYunFileService; 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 org.springframework.util.StringUtils; import java.io.*; import java.net.URL; 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上传文件失败,remoteFilePath:" + remoteFilePath, e); } return null; } @Override public String uploadFile(String bucket, String filePath, String remoteFilePath) { return uploadFile(bucket, filePath, remoteFilePath, null); } @Override public String uploadFile(String bucket, InputStream inputStream, String remoteFilePath) { try { ossClient.putObject(bucket, remoteFilePath, inputStream); log.info("文件流上传成功,目标路径:remoteFilePath:{}", remoteFilePath); } catch (Exception e) { log.error("oss上传文件失败,remoteFilePath:"+remoteFilePath, e); } return null; } @Override public String uploadFile(String bucket, String filePath, String remoteFilePath, Map headers) { try { File file = new File(filePath); if (!file.exists()) { log.warn("要上传的文件不存在,filePath" + 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, file, metadata); log.info("文件上传成功,path:{}", filePath); } catch (Exception e) { log.error("oss上传文件失败,filePath:"+filePath, e); } return null; } @Override public String uploadFileByCommand(String bucket, String filePath, String remoteFilePath) { try { String optType = new File(filePath).isDirectory() ? "folder" : "file"; String command = String.format(fYunConstants.UPLOAD_SH, bucket, filePath, remoteFilePath, FYunTypeEnum.OSS.code(), optType); log.info("开始上传文件, ossPath:{}, srcPath:{}", remoteFilePath, filePath); callshell(command); log.info("上传文件完毕, ossPath:{}, srcPath:{}", remoteFilePath, filePath); } catch (Exception e) { log.error(String.format("上传文件失败, ossPath:%s, srcPath:%s", remoteFilePath, filePath), e); } return null; } @Override public void downloadFileByCommand(String bucket, String filePath, String remoteFilePath) { try { String optType = remoteFilePath.contains(".") ? "file" : "folder"; String command = String.format(fYunConstants.DOWNLOAD_SH, bucket, remoteFilePath, filePath, FYunTypeEnum.OSS.code(), optType); log.info("开始下载文件, ossPath:{}, srcPath:{}", remoteFilePath, filePath); callshell(command); log.info("下载文件完毕, ossPath:{}, srcPath:{}", remoteFilePath, filePath); } catch (Exception e) { log.error(String.format("下载文件失败, ossPath:%s, srcPath:%s", remoteFilePath, filePath), e); } } @Override public void deleteFile(String bucket, String remoteFilePath) throws IOException { try { ossClient.deleteObject(bucket, remoteFilePath); } catch (Exception e) { log.error("OSS删除文件失败,key:" + remoteFilePath, e); } } @Override public void deleteFolder(String bucket, String remoteFolderPath) { try { if (!remoteFolderPath.endsWith("/")) { remoteFolderPath = remoteFolderPath + "/"; } log.info("开始删除文件夹:{}", remoteFolderPath); boolean flag = true; String nextMaker = null; ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucket).withPrefix(remoteFolderPath).withMaxKeys(1000); DeleteObjectsRequest request = new DeleteObjectsRequest(bucket); do { //获取下一页的起始点,它的下一项 listObjectsRequest.setMarker(nextMaker); ObjectListing objectListing = ossClient.listObjects(listObjectsRequest); List keys = objectListing.getObjectSummaries().parallelStream() .map(OSSObjectSummary::getKey).collect(Collectors.toList()); if (!CollectionUtils.isEmpty(keys)) { request.setKeys(keys); ossClient.deleteObjects(request); } nextMaker = objectListing.getNextMarker(); //全部执行完后,为false flag = objectListing.isTruncated(); } while (flag); } catch (Exception e) { log.error("OSS删除文件失败,key:" + remoteFolderPath, e); } } @Override public void uploadMulFiles(String bucket, Map filepaths) { try { for (Map.Entry entry : filepaths.entrySet()) { uploadFile(bucket, entry.getKey(), entry.getValue(), null); } } catch (Exception e) { log.error("OSS批量上传文件失败!"); } } @Override public List listRemoteFiles(String bucket, String sourcePath) { 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); } return keyList; } @Override public void copyFileBetweenBucket(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) { try { List files = listRemoteFiles(sourceBucketName, sourcePath); if (ObjectUtils.isEmpty(files)) { return; } files.stream().forEach(file -> { ossClient.copyObject(sourceBucketName, file, targetBucketName, file.replace(sourcePath, targetPath)); }); } catch (Exception e) { log.error("列举文件目录失败,key:" + sourcePath, e); } } @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()); } } catch (Exception e) { log.error(String.format("批量复制文件失败, sourceBucketName:%s, targetBucketName:%s", sourceBucketName, targetBucketName), e); } } @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) { throw e; } return contentJson.toString(); } catch (Exception e) { log.error("获取文件内容失败:key:"+remoteFilePath, e); } return null; } @Override public boolean fileExist(String bucket, String objectName) { try { return ossClient.doesObjectExist(bucket, objectName); } catch (Exception e) { log.error("判断文件是否存在失败,key:"+objectName, e); } 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(); } if(localFile.isDirectory()){ String fileName = remoteFilePath.substring(remoteFilePath.lastIndexOf("/")+1); log.info("未配置文件名,使用默认文件名:{}",fileName); localPath = localPath.concat(File.separator).concat(fileName); } DownloadFileRequest request = new DownloadFileRequest(bucket, remoteFilePath); request.setDownloadFile(localPath); // 默认5个任务并发下载 request.setTaskNum(5); // 启动断点续传 request.setEnableCheckpoint(true); ossClient.downloadFile(request); } catch (Throwable throwable) { log.error("文件下载失败,key:"+remoteFilePath, throwable); } } @Override public URL getPresignedUrl(String bucket, String url) { java.util.Date expiration = new java.util.Date(); long expTimeMillis = expiration.getTime(); expTimeMillis += 1000 * 60 * 60 * 8; expiration.setTime(expTimeMillis); GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucket, url); generatePresignedUrlRequest.setMethod(HttpMethod.PUT); generatePresignedUrlRequest.setExpiration(expiration); return ossClient.generatePresignedUrl(generatePresignedUrlRequest); } @Override public long getSubFileNums(String bucket, String url) { long totalSubFileNum = 0; try { boolean flag = true; String nextMaker = null; ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucket); //指定下一级文件 listObjectsRequest.setPrefix(url); //设置分页的页容量 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)) { totalSubFileNum = totalSubFileNum + collect.size(); } nextMaker = objectListing.getNextMarker(); //全部执行完后,为false flag = objectListing.isTruncated(); } while (flag); } catch (Exception e) { log.error("获取文件数量失败,path:" + url, e); } return totalSubFileNum; } @Override public Boolean checkStore(String bucket,String filePath){ ObjectMetadata objectMetadata = ossClient.getObjectMetadata(bucket, filePath); return !objectMetadata.isRestoreCompleted(); } @Override public void restoreFolder(String bucket, String folderName, Integer priority) { List objectList = this.listRemoteFiles(bucket, folderName); if(CollUtil.isEmpty(objectList)){ return; } objectList.parallelStream().forEach(objectName -> { this.restoreFile(bucket, objectName, priority); }); } @Override public void restoreFile(String bucket, String objectName, Integer priority){ ObjectMetadata objectMetadata = ossClient.getObjectMetadata(bucket, objectName); // 校验Object是否为归档类型Object。 StorageClass storageClass = objectMetadata.getObjectStorageClass(); if (storageClass == StorageClass.ColdArchive) { // 设置解冻冷归档Object的优先级。 // RestoreTier.RESTORE_TIER_EXPEDITED 表示1小时内完成解冻。 // RestoreTier.RESTORE_TIER_STANDARD 表示2~5小时内完成解冻。 // RestoreTier.RESTORE_TIER_BULK 表示5~12小时内完成解冻。 RestoreTier restoreTier = null; switch (priority){ case 1 : restoreTier = RestoreTier.RESTORE_TIER_EXPEDITED; break; case 2 : restoreTier = RestoreTier.RESTORE_TIER_STANDARD; break; default: restoreTier = RestoreTier.RESTORE_TIER_BULK; } RestoreJobParameters jobParameters = new RestoreJobParameters(restoreTier); // 配置解冻参数,以设置5小时内解冻完成,解冻状态保持2天为例。 // 第一个参数表示保持解冻状态的天数,默认是1天,此参数适用于解冻Archive(归档)与ColdArchive(冷归档)类型Object。 // 第二个参数jobParameters表示解冻优先级,只适用于解冻ColdArchive类型Object。 RestoreConfiguration configuration = new RestoreConfiguration(1, jobParameters); //开始解冻 ossClient.restoreObject(bucket, objectName, configuration); // // 等待解冻完成。 // do { // try { // Thread.sleep(1000); // } catch (InterruptedException e) { // e.printStackTrace(); // } // objectMetadata = ossClient.getObjectMetadata(bucket, objectName); // } while (!objectMetadata.isRestoreCompleted()); } } }