From 35ae89ad3e3b6367fb4f4c71875ce2bb9492343c Mon Sep 17 00:00:00 2001 From: zlg <482370576@qq.com> Date: Tue, 16 Jul 2024 11:26:45 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=92=96=E5=95=A1=E5=9B=A0=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E8=AE=BE=E7=BD=AE=EF=BC=8C=E6=A1=B6=E4=B8=8E=E5=A4=A7?= =?UTF-8?q?=E5=B0=8F=E5=88=86=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SchisandraOssAliController.java | 126 ++++++------- .../SchisandraOssMinioController.java | 27 ++- .../SchisandraOssTencentController.java | 165 ++++++++++-------- .../oss/core/ali/AliOssClient.java | 105 +++++++---- 4 files changed, 253 insertions(+), 170 deletions(-) diff --git a/schisandra-cloud-storage-oss/schisandra-cloud-storage-oss-application/schisandra-cloud-storage-oss-application-controller/src/main/java/com/schisandra/oss/application/controller/SchisandraOssAliController.java b/schisandra-cloud-storage-oss/schisandra-cloud-storage-oss-application/schisandra-cloud-storage-oss-application-controller/src/main/java/com/schisandra/oss/application/controller/SchisandraOssAliController.java index 0e99a95..cd50479 100644 --- a/schisandra-cloud-storage-oss/schisandra-cloud-storage-oss-application/schisandra-cloud-storage-oss-application-controller/src/main/java/com/schisandra/oss/application/controller/SchisandraOssAliController.java +++ b/schisandra-cloud-storage-oss/schisandra-cloud-storage-oss-application/schisandra-cloud-storage-oss-application-controller/src/main/java/com/schisandra/oss/application/controller/SchisandraOssAliController.java @@ -1,9 +1,11 @@ package com.schisandra.oss.application.controller; +import cn.hutool.core.util.ZipUtil; import cn.hutool.extra.spring.SpringUtil; import com.alibaba.fastjson.JSON; import com.google.common.base.Preconditions; import com.schisandra.oss.application.convert.SchisandraOssAliDTOConverter; +import com.schisandra.oss.application.convert.SchisandraOssMinioDTOConverter; import com.schisandra.oss.application.dto.SchisandraOssAliDTO; import com.schisandra.oss.application.oss.constant.OssConstant; import com.schisandra.oss.application.oss.core.ali.AliOssClient; @@ -11,23 +13,30 @@ import com.schisandra.oss.application.oss.core.ali.AliOssConfiguration; import com.schisandra.oss.application.oss.model.OssInfo; import com.schisandra.oss.common.entity.Result; import com.schisandra.oss.domain.bo.SchisandraOssAliBO; +import com.schisandra.oss.domain.bo.SchisandraOssMinioBO; import com.schisandra.oss.domain.redis.RedisUtil; import com.schisandra.oss.domain.service.SchisandraOssAliDomainService; import lombok.extern.slf4j.Slf4j; +import org.springframework.context.ApplicationContext; +import org.springframework.util.StreamUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.net.URLEncoder; import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; /** @@ -50,37 +59,6 @@ public class SchisandraOssAliController { @Resource RedisUtil redisUtil; - /** - * @description: 预览文件 - * @param: [userId, bucket, filePath] - * @return: com.schisandra.oss.common.entity.Result - * @date: 2024/6/27 10:14 - */ - @PostMapping("previewFile") - public Result previewMinioFile(@RequestParam String userId, @RequestParam String bucket, @RequestParam String filePath) throws IOException { - Preconditions.checkNotNull(userId, "不能为空"); - Preconditions.checkNotNull(bucket, "不能为空"); - Preconditions.checkNotNull(filePath, "不能为空"); - AliOssClient bean = SpringUtil.getBean(OssConstant.OssType.ALI + userId); - bean.getAliOssConfig().setBucketName(bucket); - try { - Date date = new Date(); - SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); - String formattedDate = dateFormat.format(date); - String key = redisUtil.buildKey(USER_OSS_PREFIX, "previewFile", formattedDate, userId, filePath); - HashMap map = new HashMap<>(); - String url = bean.getAliPreviewUrl(filePath); - map.put("url", url); - map.put("time", formattedDate); - redisUtil.setNx(key, map, 30L, TimeUnit.DAYS); - return Result.ok(url); - } catch (Exception e) { - return Result.fail(e.getMessage()); - } - - } - - /** * 返回ali表所有数据 * @@ -89,7 +67,7 @@ public class SchisandraOssAliController { @GetMapping("returnAll") public Result returnAll() { List list = schisandraOssAliDomainService.selectAll(); - if (list.isEmpty()) { + if(list.isEmpty()){ return Result.fail(); } return Result.ok(SchisandraOssAliDTOConverter.INSTANCE.convertBOToDTOList(list)); @@ -97,7 +75,7 @@ public class SchisandraOssAliController { @GetMapping("init") - public Result init(@RequestParam String userId, @RequestParam String Id) { + public Result init(@RequestParam String userId,@RequestParam String Id) { if (log.isInfoEnabled()) { log.info("SchisandraOssAliController.init.userId:{}", userId); } @@ -106,15 +84,15 @@ public class SchisandraOssAliController { bo.setId(Long.valueOf(Id)); bo.setUserId(Long.valueOf(userId)); bo.setStatus(String.valueOf(true)); - if (schisandraOssAliDomainService.update(bo)) { + if(schisandraOssAliDomainService.update(bo)){ return aliOssConfiguration.aliOssClient(userId); - } else { + }else{ return Result.fail("初始化失败"); } } @PostMapping("shutdown") - public Result shutdownAli(@RequestParam String userId, @RequestParam String Id) { + public Result shutdownAli(@RequestParam String userId,@RequestParam String Id){ if (log.isInfoEnabled()) { log.info("SchisandraOssAliController.shutdown.userId:{}", JSON.toJSONString(userId)); } @@ -122,10 +100,10 @@ public class SchisandraOssAliController { bo.setId(Long.valueOf(Id)); bo.setUserId(Long.valueOf(userId)); bo.setStatus(String.valueOf(false)); - if (schisandraOssAliDomainService.update(bo)) { - SpringUtil.unregisterBean(OssConstant.OssType.ALI + userId); + if(schisandraOssAliDomainService.update(bo)){ + SpringUtil.unregisterBean(OssConstant.OssType.ALI+ userId); return Result.ok("关闭成功"); - } else { + }else{ return Result.fail("关闭失败"); } } @@ -198,13 +176,13 @@ public class SchisandraOssAliController { @GetMapping("listDir") public Result listAliDir(@RequestParam String userId, @RequestParam String bucket, @RequestParam String prefix) throws Exception { Preconditions.checkNotNull(userId, "不能为空"); - AliOssClient bean = SpringUtil.getBean(OssConstant.OssType.ALI + userId); + AliOssClient bean = SpringUtil.getBean(OssConstant.OssType.ALI+ userId); bean.getAliOssConfig().setBucketName(bucket); if (prefix == null) prefix = ""; - if (bean.listAliInfo(bucket, prefix) != null) { + if(bean.listAliInfo(bucket, prefix) != null){ return Result.ok(bean.listAliInfo(bucket, prefix)); - } else { + }else{ return Result.fail("获取文件目录信息失败"); } @@ -219,10 +197,10 @@ public class SchisandraOssAliController { @PostMapping("seleteBucket") public Result seleteBucket(@RequestParam String userId) { Preconditions.checkNotNull(userId, "不能为空"); - AliOssClient bean = SpringUtil.getBean(OssConstant.OssType.ALI + userId); - if (bean.selectAllBucket() != null) { + AliOssClient bean = SpringUtil.getBean(OssConstant.OssType.ALI+ userId); + if(bean.selectAllBucket() != null){ return Result.ok(bean.selectAllBucket()); - } else { + }else{ return Result.fail("查询失败"); } @@ -239,11 +217,11 @@ public class SchisandraOssAliController { public Result createBucket(@RequestParam String userId, @RequestParam String bucket) { Preconditions.checkNotNull(userId, "不能为空"); Preconditions.checkNotNull(bucket, "不能为空"); - AliOssClient bean = SpringUtil.getBean(OssConstant.OssType.ALI + userId); + AliOssClient bean = SpringUtil.getBean(OssConstant.OssType.ALI+ userId); bean.getAliOssConfig().setBucketName(bucket); - if (bean.createBucket(bucket).equals(bucket)) { + if(bean.createBucket(bucket).equals(bucket)){ return Result.ok(bean.createBucket(bucket)); - } else { + }else{ return Result.fail("创建失败"); } } @@ -261,9 +239,9 @@ public class SchisandraOssAliController { Preconditions.checkNotNull(bucket, "不能为空"); AliOssClient bean = SpringUtil.getBean(OssConstant.OssType.ALI + userId); bean.getAliOssConfig().setBucketName(bucket); - if (bean.deleteBucket(bucket).equals("yes")) { + if(bean.deleteBucket(bucket).equals("yes")){ return Result.ok(bean.deleteBucket(bucket)); - } else { + }else{ return Result.fail("删除失败,或许桶已经不存在"); } } @@ -280,10 +258,7 @@ public class SchisandraOssAliController { Preconditions.checkNotNull(userId, "不能为空"); Preconditions.checkNotNull(fileName, "不能为空"); Preconditions.checkNotNull(bucket, "不能为空"); - // 获取文件输入流 - InputStream is = file.getInputStream(); - AliOssClient bean = SpringUtil.getBean(OssConstant.OssType.ALI + userId); - bean.getAliOssConfig().setBucketName(bucket); + //设置热力图 Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); @@ -296,16 +271,20 @@ public class SchisandraOssAliController { } else { redisUtil.set(key, String.valueOf(1)); } - - if (bean.upLoad(is, fileName, true) != null) { + // 获取文件输入流 + InputStream is = file.getInputStream(); + AliOssClient bean = SpringUtil.getBean(OssConstant.OssType.ALI + userId); + bean.getAliOssConfig().setBucketName(bucket); + if(bean.upLoad(is, fileName, true) != null){ return Result.ok(bean.upLoad(is, fileName, true)); - } else { + }else{ return Result.fail(); } } + /** * @description: 下载文件 * @param: [schisandraOssAliDTO] @@ -313,18 +292,43 @@ public class SchisandraOssAliController { * @date: 2024/6/26 13:56 */ @GetMapping("downloadFile") - public Result getAliFile(@RequestParam String bucket, @RequestParam String userId, @RequestParam String filePath, HttpServletResponse response) throws Exception { + public void getAliFile(@RequestParam String bucket, @RequestParam String userId, @RequestParam List filePath, HttpServletResponse response) throws Exception { Preconditions.checkNotNull(bucket, "不能为空"); Preconditions.checkNotNull(userId, "不能为空"); Preconditions.checkNotNull(filePath, "不能为空"); AliOssClient bean = SpringUtil.getBean(OssConstant.OssType.ALI + userId); bean.getAliOssConfig().setBucketName(bucket); ServletOutputStream output = response.getOutputStream(); - response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filePath.substring(filePath.lastIndexOf("/") + 1), "UTF-8")); + response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("下载.zip", "UTF-8")); response.setContentType("application/octet-stream"); response.setCharacterEncoding("UTF-8"); - bean.downLoad(output, filePath); - return Result.ok(); + HashMap list=bean.downLoadMany(filePath); + String[] path = new String[list.size()]; + InputStream[] inputStreams = new InputStream[list.size()]; + path = list.keySet().toArray(path); + inputStreams = list.values().toArray(inputStreams); + //记录用户下载 + Date date = new Date(); + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + String formattedDate = dateFormat.format(date); + AtomicInteger count = new AtomicInteger(0); + InputStream[] inputStream2 = new InputStream[list.size()]; + list.keySet().forEach( name -> { + try { + byte[] bytes = StreamUtils.copyToByteArray(list.get(name)); + inputStream2[count.get()] = new ByteArrayInputStream(bytes); + String key = redisUtil.buildKey(USER_OSS_PREFIX, "download", formattedDate, userId, name); + HashMap map = new HashMap<>(); + map.put("size", bytes.length); + map.put("time",formattedDate); + redisUtil.setNx(key, map, 30L, TimeUnit.DAYS); + count.incrementAndGet(); + } catch (IOException e) { + throw new RuntimeException(e); + } + + }); + ZipUtil.zip(output, path, inputStream2); } /** diff --git a/schisandra-cloud-storage-oss/schisandra-cloud-storage-oss-application/schisandra-cloud-storage-oss-application-controller/src/main/java/com/schisandra/oss/application/controller/SchisandraOssMinioController.java b/schisandra-cloud-storage-oss/schisandra-cloud-storage-oss-application/schisandra-cloud-storage-oss-application-controller/src/main/java/com/schisandra/oss/application/controller/SchisandraOssMinioController.java index 16da308..f586c7e 100644 --- a/schisandra-cloud-storage-oss/schisandra-cloud-storage-oss-application/schisandra-cloud-storage-oss-application-controller/src/main/java/com/schisandra/oss/application/controller/SchisandraOssMinioController.java +++ b/schisandra-cloud-storage-oss/schisandra-cloud-storage-oss-application/schisandra-cloud-storage-oss-application-controller/src/main/java/com/schisandra/oss/application/controller/SchisandraOssMinioController.java @@ -9,6 +9,7 @@ import com.schisandra.oss.application.dto.SchisandraOssMinioDTO; import com.schisandra.oss.application.oss.constant.OssConstant; import com.schisandra.oss.application.oss.core.minio.MinioOssClient; import com.schisandra.oss.application.oss.core.minio.MinioOssConfiguration; +import com.schisandra.oss.application.oss.model.OssInfo; import com.schisandra.oss.common.entity.Result; import com.schisandra.oss.domain.bo.SchisandraOssMinioBO; import com.schisandra.oss.domain.redis.RedisUtil; @@ -16,6 +17,7 @@ import com.schisandra.oss.domain.service.SchisandraOssMinioDomainService; import io.minio.errors.*; import lombok.extern.slf4j.Slf4j; import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Caching; import org.springframework.util.StreamUtils; @@ -25,12 +27,15 @@ import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.List; @@ -128,9 +133,9 @@ public class SchisandraOssMinioController { * @author zlg * @date: 2024/6/26 13:55 */ - @Cacheable(value = "MinioListDir", key = "#userId+#bucket+#dirName") + @Cacheable(value = "MinioListDir", key = "#userId+#bucket+#dirName",unless = "#result == null") @GetMapping("listDir") - public Result listMinioInfo(@RequestParam String userId, @RequestParam String dirName, @RequestParam String bucket) throws Exception { + public Result listMinioInfo(@RequestParam String userId, @RequestParam String dirName, @RequestParam String bucket) throws Exception { Preconditions.checkNotNull(userId, "不能为空"); MinioOssClient bean = SpringUtil.getBean(OssConstant.OssType.MINIO + userId); bean.getMinioOssConfig().setBucketName(bucket); @@ -152,16 +157,14 @@ public class SchisandraOssMinioController { MinioOssClient bean = SpringUtil.getBean(OssConstant.OssType.MINIO + userId); bean.getMinioOssConfig().setBucketName(bucket); HashMap list = bean.getTargetDir(listObjectsArgs); - - ServletOutputStream output = response.getOutputStream(); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("下载.zip", "UTF-8")); response.setContentType("application/octet-stream"); response.setCharacterEncoding("UTF-8"); + ServletOutputStream output = response.getOutputStream(); String[] path = new String[list.size()]; InputStream[] inputStreams = new InputStream[list.size()]; path = list.keySet().toArray(path); inputStreams = list.values().toArray(inputStreams); - //记录用户下载 Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); @@ -169,6 +172,7 @@ public class SchisandraOssMinioController { list.keySet().forEach(name -> { try { byte[] bytes = StreamUtils.copyToByteArray(list.get(name)); + list.get(name).reset(); String key = redisUtil.buildKey(USER_OSS_PREFIX, "download", formattedDate, userId, name); HashMap map = new HashMap<>(); map.put("size", bytes.length); @@ -177,7 +181,6 @@ public class SchisandraOssMinioController { } catch (IOException e) { throw new RuntimeException(e); } - }); ZipUtil.zip(output, path, inputStreams); } @@ -189,7 +192,7 @@ public class SchisandraOssMinioController { * @author zlg * @date: 2024/6/26 14:34 */ - @CacheEvict(value = "MinioListDir", key = "#userId+#bucket+#dirName") + @CacheEvict(value = "MinioListDir", key = "#userId+#bucket+#filePath") @PostMapping("deleteFile") public Result deleteMinioFile(@RequestParam String bucket, @RequestParam String userId, @RequestParam String filePath) { Preconditions.checkNotNull(bucket, "不能为空"); @@ -345,12 +348,20 @@ public class SchisandraOssMinioController { * @author zlg * @date: 2024/6/26 17:37 */ + @Cacheable(value = "minioBucket", key = "#userId",unless = "#result==null") @PostMapping("seleteBucket") public Result seleteBucket(@RequestParam String userId) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException { Preconditions.checkNotNull(userId, "不能为空"); MinioOssClient bean = SpringUtil.getBean(OssConstant.OssType.MINIO + userId); return Result.ok(bean.selectAllBucket()); } + @Cacheable(value = "minioBucket", key = "#userId+#bucket",unless = "#result==null") + @PostMapping("getBucketSize") + public Result getBucketSize(@RequestParam String userId, @RequestParam String bucket) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException { + Preconditions.checkNotNull(userId, "不能为空"); + MinioOssClient bean = SpringUtil.getBean(OssConstant.OssType.MINIO+userId); + return Result.ok(bean.getMinioBucketSize(bucket)); + } /** * @description: 创建bucket @@ -359,6 +370,7 @@ public class SchisandraOssMinioController { * @author zlg * @date: 2024/6/26 17:38 */ + @CacheEvict(value = "minioBucket", key = "#userId") @PostMapping("createBucket") public Result createBucket(@RequestParam String userId, @RequestParam String bucket) { Preconditions.checkNotNull(userId, "不能为空"); @@ -375,6 +387,7 @@ public class SchisandraOssMinioController { * @author zlg * @date: 2024/6/26 17:38 */ + @CacheEvict(value = "minioBucket", key = "#userId") @PostMapping("deleteBucket") public Result deleteBucket(@RequestParam String userId, @RequestParam String bucket) { Preconditions.checkNotNull(userId, "不能为空"); diff --git a/schisandra-cloud-storage-oss/schisandra-cloud-storage-oss-application/schisandra-cloud-storage-oss-application-controller/src/main/java/com/schisandra/oss/application/controller/SchisandraOssTencentController.java b/schisandra-cloud-storage-oss/schisandra-cloud-storage-oss-application/schisandra-cloud-storage-oss-application-controller/src/main/java/com/schisandra/oss/application/controller/SchisandraOssTencentController.java index f6e9ddc..e0b5b75 100644 --- a/schisandra-cloud-storage-oss/schisandra-cloud-storage-oss-application/schisandra-cloud-storage-oss-application-controller/src/main/java/com/schisandra/oss/application/controller/SchisandraOssTencentController.java +++ b/schisandra-cloud-storage-oss/schisandra-cloud-storage-oss-application/schisandra-cloud-storage-oss-application-controller/src/main/java/com/schisandra/oss/application/controller/SchisandraOssTencentController.java @@ -24,6 +24,9 @@ import com.schisandra.oss.domain.redis.RedisUtil; import com.schisandra.oss.domain.service.SchisandraOssTencentDomainService; import io.minio.errors.*; import lombok.extern.slf4j.Slf4j; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.cache.annotation.Caching; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; @@ -47,22 +50,23 @@ import java.util.List; @Slf4j public class SchisandraOssTencentController { - @Resource - private SchisandraOssTencentDomainService schisandraOssTencentDomainService; - @Resource - TencentOssConfiguration tencentOssConfiguration; private final String USER_OSS_PREFIX = "oss:user:heat"; @Resource + TencentOssConfiguration tencentOssConfiguration; + @Resource RedisUtil redisUtil; + @Resource + private SchisandraOssTencentDomainService schisandraOssTencentDomainService; /** * 返回tencent表所有数据 + * * @return */ @GetMapping("returnAll") public Result returnAll() { List list = schisandraOssTencentDomainService.selectAll(); - if(list.isEmpty()){ + if (list.isEmpty()) { return Result.fail(); } return Result.ok(SchisandraOssTencentDTOConverter.INSTANCE.convertBOToDTOList(list)); @@ -70,7 +74,7 @@ public class SchisandraOssTencentController { @PostMapping("init") - public Result initTencent(@RequestParam String userId,@RequestParam String Id){ + public Result initTencent(@RequestParam String userId, @RequestParam String Id) { if (log.isInfoEnabled()) { log.info("SchisandraOssTencentController.init.userId:{}", userId); } @@ -79,15 +83,15 @@ public class SchisandraOssTencentController { bo.setId(Long.valueOf(Id)); bo.setUserId(Long.valueOf(userId)); bo.setStatus(String.valueOf(true)); - if(schisandraOssTencentDomainService.update(bo)){ + if (schisandraOssTencentDomainService.update(bo)) { return tencentOssConfiguration.tencentOssClient(userId); - }else{ + } else { return Result.fail("初始化失败"); } } @PostMapping("shutdown") - public Result shutdownTencent(@RequestParam String userId,@RequestParam String Id){ + public Result shutdownTencent(@RequestParam String userId, @RequestParam String Id) { if (log.isInfoEnabled()) { log.info("SchisandraOssTencentController.shutdown.userId:{}", JSON.toJSONString(userId)); } @@ -95,10 +99,10 @@ public class SchisandraOssTencentController { bo.setId(Long.valueOf(Id)); bo.setUserId(Long.valueOf(userId)); bo.setStatus(String.valueOf(false)); - if(schisandraOssTencentDomainService.update(bo)){ - SpringUtil.unregisterBean(OssConstant.OssType.TENCENT+ userId); + if (schisandraOssTencentDomainService.update(bo)) { + SpringUtil.unregisterBean(OssConstant.OssType.TENCENT + userId); return Result.ok("关闭成功"); - }else{ + } else { return Result.fail("关闭失败"); } } @@ -110,12 +114,23 @@ public class SchisandraOssTencentController { * @author zlg * @date: 2024/6/29 13:28 */ + @Cacheable(value = "TencentBucket", key = "#userId") @PostMapping("seleteBucket") public Result seleteBucket(@RequestParam String userId) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException { Preconditions.checkNotNull(userId, "不能为空"); - TencentOssClient bean = SpringUtil.getBean(OssConstant.OssType.TENCENT+ userId); + TencentOssClient bean = SpringUtil.getBean(OssConstant.OssType.TENCENT + userId); return Result.ok(bean.selectAllBucket()); } + + @Cacheable(value = "TencentBucket", key = "#userId+#bucket") + @PostMapping("getBucketSize") + public Result getBucketSize(@RequestParam String userId, @RequestParam String bucket) throws Exception { + Preconditions.checkNotNull(bucket, "不能为空"); + TencentOssClient bean = SpringUtil.getBean(OssConstant.OssType.TENCENT + userId); + bean.getTencentOssConfig().setBucketName(bucket); + return Result.ok(bean.getTencentBucketSize(bucket)); + } + /** * @description: 查询指定目录 * @param: [userId, dirName, bucket] @@ -123,62 +138,69 @@ public class SchisandraOssTencentController { * @author zlg * @date: 2024/6/29 13:28 */ + @Cacheable(value = "TencentBucketDir", key = "#userId+#bucket+#dirName") @GetMapping("listDir") - public Result listTencentInfo(@RequestParam String userId, @RequestParam String dirName,@RequestParam String bucket) throws Exception { + public Result listTencentInfo(@RequestParam String userId, @RequestParam String dirName, @RequestParam String bucket) throws Exception { Preconditions.checkNotNull(bucket, "不能为空"); Preconditions.checkNotNull(userId, "不能为空"); - TencentOssClient bean = SpringUtil.getBean(OssConstant.OssType.TENCENT+ userId); + TencentOssClient bean = SpringUtil.getBean(OssConstant.OssType.TENCENT + userId); bean.getTencentOssConfig().setBucketName(bucket); return Result.ok(bean.listTargetDir(bucket, dirName)); } -/** - * @description: 创建桶 - * @param: [userId, bucket, appId] - * @return: com.schisandra.oss.common.entity.Result - * @author zlg - * @date: 2024/6/29 13:27 - */ + + /** + * @description: 创建桶 + * @param: [userId, bucket, appId] + * @return: com.schisandra.oss.common.entity.Result + * @author zlg + * @date: 2024/6/29 13:27 + */ + @CacheEvict(value = "TencentBucket", key = "#userId") @PostMapping("createBucket") - public Result createBucket(@RequestParam String userId, @RequestParam String bucket,@RequestParam String appId) { + public Result createBucket(@RequestParam String userId, @RequestParam String bucket, @RequestParam String appId) { Preconditions.checkNotNull(userId, "不能为空"); Preconditions.checkNotNull(bucket, "不能为空"); - TencentOssClient bean = SpringUtil.getBean(OssConstant.OssType.TENCENT+ userId); + TencentOssClient bean = SpringUtil.getBean(OssConstant.OssType.TENCENT + userId); // bean.getTencentOssConfig().setBucketName(bucket); - return Result.ok(bean.createBuctet(bucket,appId)); + return Result.ok(bean.createBuctet(bucket, appId)); } -/** - * @description: 删除腾讯云对象存储bucket - * @param: [userId, bucket, appId] - * @return: com.schisandra.oss.common.entity.Result - * @author zlg - * @date: 2024/6/29 13:27 - */ + + /** + * @description: 删除腾讯云对象存储bucket + * @param: [userId, bucket, appId] + * @return: com.schisandra.oss.common.entity.Result + * @author zlg + * @date: 2024/6/29 13:27 + */ + @CacheEvict(value = "TencentBucket", key = "#userId") @PostMapping("deleteBucket") - public Result deleteBucket(@RequestParam String userId, @RequestParam String bucket,@RequestParam String appId) { + public Result deleteBucket(@RequestParam String userId, @RequestParam String bucket, @RequestParam String appId) { Preconditions.checkNotNull(userId, "不能为空"); Preconditions.checkNotNull(bucket, "不能为空"); - TencentOssClient bean = SpringUtil.getBean(OssConstant.OssType.TENCENT+ userId); + TencentOssClient bean = SpringUtil.getBean(OssConstant.OssType.TENCENT + userId); // bean.getTencentOssConfig().setBucketName(bucket); - if (bean.deleteBucket(bucket,appId)){ + if (bean.deleteBucket(bucket, appId)) { return Result.ok(); - }else { + } else { return Result.fail(); } } -/** - * @description: 分享腾讯云对象存储链接 - * @param: [userId, bucket, target] - * @return: com.schisandra.oss.common.entity.Result - * @author zlg - * @date: 2024/6/29 13:27 - */ + + /** + * @description: 分享腾讯云对象存储链接 + * @param: [userId, bucket, target] + * @return: com.schisandra.oss.common.entity.Result + * @author zlg + * @date: 2024/6/29 13:27 + */ @GetMapping("shareUrl") - public Result shareTencentUrl(@RequestParam String userId, @RequestParam String bucket,@RequestParam String target) { + public Result shareTencentUrl(@RequestParam String userId, @RequestParam String bucket, @RequestParam String target) { Preconditions.checkNotNull(userId, "不能为空"); Preconditions.checkNotNull(bucket, "不能为空"); - TencentOssClient bean = SpringUtil.getBean(OssConstant.OssType.TENCENT+ userId); - return Result.ok(bean.shareTencentObject(bucket,target)); + TencentOssClient bean = SpringUtil.getBean(OssConstant.OssType.TENCENT + userId); + return Result.ok(bean.shareTencentObject(bucket, target)); } + /** * @description: 腾讯云对象存储上传文件 * @param: [userId, bucket, target, file] @@ -190,26 +212,27 @@ public class SchisandraOssTencentController { public Result uploadTencent(@RequestParam String userId, @RequestParam String bucket, @RequestParam String target, @RequestParam MultipartFile file) throws IOException { Preconditions.checkNotNull(userId, "不能为空"); Preconditions.checkNotNull(bucket, "不能为空"); - if (!target.isEmpty()){ - target=target+"/"+file.getOriginalFilename(); + if (!target.isEmpty()) { + target = target + "/" + file.getOriginalFilename(); } - TencentOssClient bean = SpringUtil.getBean(OssConstant.OssType.TENCENT+ userId); + TencentOssClient bean = SpringUtil.getBean(OssConstant.OssType.TENCENT + userId); InputStream is = file.getInputStream(); bean.getTencentOssConfig().setBucketName(bucket); //设置热力图 - Date date =new Date(); + Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); String formattedDate = dateFormat.format(date); - String key = redisUtil.buildKey(USER_OSS_PREFIX+":"+userId,formattedDate); - int count=1; - if (redisUtil.exist(key)){ - count= Integer.parseInt(redisUtil.get(key)); - redisUtil.set(key, String.valueOf(count+1)); - }else { + String key = redisUtil.buildKey(USER_OSS_PREFIX + ":" + userId, formattedDate); + int count = 1; + if (redisUtil.exist(key)) { + count = Integer.parseInt(redisUtil.get(key)); + redisUtil.set(key, String.valueOf(count + 1)); + } else { redisUtil.set(key, String.valueOf(1)); } - return Result.ok(bean.upLoadParts(is,target,file.getOriginalFilename())); + return Result.ok(bean.upLoadParts(is, target, file.getOriginalFilename())); } + /** * @description: 腾讯云对象存储下载文件 * @param: [userId, bucket, target] @@ -221,9 +244,10 @@ public class SchisandraOssTencentController { public Result downloadTencent(@RequestParam String userId, @RequestParam String bucket, @RequestParam String target) { Preconditions.checkNotNull(userId, "不能为空"); Preconditions.checkNotNull(bucket, "不能为空"); - TencentOssClient bean = SpringUtil.getBean(OssConstant.OssType.TENCENT+ userId); - return Result.ok(bean.downloadTencent(bucket,target)); + TencentOssClient bean = SpringUtil.getBean(OssConstant.OssType.TENCENT + userId); + return Result.ok(bean.downloadTencent(bucket, target)); } + /** * @description: 腾讯云对象存储移动文件 * @param: [userId, sourceBucket, targetBucket, targetName, SourceName] @@ -231,16 +255,18 @@ public class SchisandraOssTencentController { * @author zlg * @date: 2024/7/1 16:02 */ + @Caching(cacheable = @Cacheable(value = "TencentBucketDir", key = "#userId+#targetBucket+#targetName"), evict = @CacheEvict(value = "MinioListDir", key = "#userId+#sourceBucket+#SourceName")) @PostMapping("moveFile") - public Result moveTencent(@RequestParam String userId, @RequestParam String sourceBucket, @RequestParam String targetBucket, @RequestParam String targetName,@RequestParam String SourceName) throws IOException { + public Result moveTencent(@RequestParam String userId, @RequestParam String sourceBucket, @RequestParam String targetBucket, @RequestParam String targetName, @RequestParam String SourceName) throws IOException { Preconditions.checkNotNull(userId, "不能为空"); - TencentOssClient bean = SpringUtil.getBean(OssConstant.OssType.TENCENT+ userId); + TencentOssClient bean = SpringUtil.getBean(OssConstant.OssType.TENCENT + userId); CopyResult copyResult = bean.moveTencent(SourceName, targetName, sourceBucket, targetBucket); - if (copyResult==null){ + if (copyResult == null) { return Result.fail(); } return Result.ok(copyResult); } + /** * @description: 腾讯云对象存储复制文件 * @param: [userId, sourceBucket, targetBucket, targetName, SourceName] @@ -248,16 +274,18 @@ public class SchisandraOssTencentController { * @author zlg * @date: 2024/7/1 16:02 */ + @Cacheable(value = "TencentBucketDir", key = "#userId+#targetBucket+#targetName") @PostMapping("copyFile") - public Result copyTencent(@RequestParam String userId, @RequestParam String sourceBucket, @RequestParam String targetBucket, @RequestParam String targetName,@RequestParam String SourceName) throws IOException { + public Result copyTencent(@RequestParam String userId, @RequestParam String sourceBucket, @RequestParam String targetBucket, @RequestParam String targetName, @RequestParam String SourceName) throws IOException { Preconditions.checkNotNull(userId, "不能为空"); - TencentOssClient bean = SpringUtil.getBean(OssConstant.OssType.TENCENT+ userId); - CopyResult copyResult = bean.copyTencent(SourceName,targetName , sourceBucket, targetBucket); - if (copyResult==null){ + TencentOssClient bean = SpringUtil.getBean(OssConstant.OssType.TENCENT + userId); + CopyResult copyResult = bean.copyTencent(SourceName, targetName, sourceBucket, targetBucket); + if (copyResult == null) { return Result.fail(); } return Result.ok(copyResult); } + /** * @description: 腾讯云对象存储删除文件 * @param: [userId, bucketName, target] @@ -265,12 +293,13 @@ public class SchisandraOssTencentController { * @author zlg * @date: 2024/7/1 16:02 */ + @Cacheable() @PostMapping("deleteFile") public Result deleteTencent(@RequestParam String userId, @RequestParam String bucketName, @RequestParam List target) throws IOException { Preconditions.checkNotNull(userId, "不能为空"); - TencentOssClient bean = SpringUtil.getBean(OssConstant.OssType.TENCENT+ userId); + TencentOssClient bean = SpringUtil.getBean(OssConstant.OssType.TENCENT + userId); DeleteObjectsResult deleteResult = bean.deleteTencent(bucketName, target); - if (deleteResult==null){ + if (deleteResult == null) { return Result.fail(); } return Result.ok(deleteResult); diff --git a/schisandra-cloud-storage-oss/schisandra-cloud-storage-oss-application/schisandra-cloud-storage-oss-application-controller/src/main/java/com/schisandra/oss/application/oss/core/ali/AliOssClient.java b/schisandra-cloud-storage-oss/schisandra-cloud-storage-oss-application/schisandra-cloud-storage-oss-application-controller/src/main/java/com/schisandra/oss/application/oss/core/ali/AliOssClient.java index b5919e8..a9da3e5 100644 --- a/schisandra-cloud-storage-oss/schisandra-cloud-storage-oss-application/schisandra-cloud-storage-oss-application-controller/src/main/java/com/schisandra/oss/application/oss/core/ali/AliOssClient.java +++ b/schisandra-cloud-storage-oss/schisandra-cloud-storage-oss-application/schisandra-cloud-storage-oss-application-controller/src/main/java/com/schisandra/oss/application/oss/core/ali/AliOssClient.java @@ -6,6 +6,7 @@ import cn.hutool.core.date.DateUtil; import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.file.FileNameUtil; import cn.hutool.core.io.unit.DataSizeUtil; +import cn.hutool.core.lang.hash.Hash; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.StrUtil; @@ -13,6 +14,11 @@ import com.aliyun.oss.OSS; import com.aliyun.oss.common.utils.HttpHeaders; import com.aliyun.oss.model.*; +import com.jd.platform.async.executor.Async; +import com.jd.platform.async.worker.WorkResult; +import com.jd.platform.async.wrapper.WorkerWrapper; +import com.qiniu.storage.BucketManager; +import com.qiniu.storage.model.FileInfo; import com.schisandra.oss.application.oss.constant.OssConstant; import com.schisandra.oss.application.oss.core.StandardOssClient; import com.schisandra.oss.application.oss.core.ali.model.AliOssConfig; @@ -22,6 +28,7 @@ import com.schisandra.oss.application.oss.model.FileOssInfo; import com.schisandra.oss.application.oss.model.OssInfo; import com.schisandra.oss.application.oss.model.SliceConfig; import com.schisandra.oss.application.oss.utils.OssPathUtil; +import io.minio.ListObjectsArgs; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @@ -29,12 +36,16 @@ import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; +import java.io.BufferedInputStream; import java.io.File; import java.io.InputStream; import java.io.OutputStream; -import java.net.URL; import java.text.SimpleDateFormat; import java.util.*; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; /** * https://help.aliyun.com/product/31815.html @@ -55,29 +66,23 @@ public class AliOssClient implements StandardOssClient { private OSS oss; private AliOssConfig aliOssConfig; - - public String getAliPreviewUrl(String fileName) { - URL presignedUrl = oss.generatePresignedUrl(getBucketName(), fileName, new Date()); - return String.valueOf(presignedUrl); - } - public String getAliBucketSize(String bucket) { ObjectListing objectListing = oss.listObjects(bucket, ""); long x = 0L; List sums = objectListing.getObjectSummaries(); - for (OSSObjectSummary objectSummary : sums) { - x += objectSummary.getSize(); + for(OSSObjectSummary objectSummary : sums){ + x+=objectSummary.getSize(); } return DataSizeUtil.format(x); } - public List> selectAllBucket() { + public List> selectAllBucket() { List buckets = oss.listBuckets(); - List> list1 = new ArrayList<>(); + List> list1 = new ArrayList<>(); buckets.forEach(bucket -> { HashMap names = new HashMap<>(); - names.put("name", bucket.getName()); - names.put("size", getAliBucketSize(bucket.getName())); + names.put("name",bucket.getName()); + names.put("size",getAliBucketSize(bucket.getName())); list1.add(names); }); return list1; @@ -89,12 +94,12 @@ public class AliOssClient implements StandardOssClient { * @return: java.util.List * @date: 2024/7/5 13:34 */ - public String transfer(String before) { + public String transfer(String before){ SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US); Date date = new Date(); - try { + try{ date = sdf.parse(before); - } catch (Exception e) { + }catch (Exception e){ e.printStackTrace(); } String formatStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date); @@ -102,23 +107,23 @@ public class AliOssClient implements StandardOssClient { return formatStr; } - public List listAliInfo(String bucket, String prefix) { + public List listAliInfo(String bucket, String prefix){ // 列举文件。如果不设置Prefix,则列举存储空间下的所有文件。如果设置Prefix,则列举包含指定前缀的文件。 ObjectListing objectListing = oss.listObjects(bucket, prefix); List sums = objectListing.getObjectSummaries(); List infos = new ArrayList<>(); - if (prefix == "") { - for (OSSObjectSummary objectSummary : sums) { + if(prefix == ""){ + for(OSSObjectSummary objectSummary : sums){ OssInfo info = new OssInfo(); String[] split_path = objectSummary.getKey().split("/"); - if (split_path.length >= 2) + if(split_path.length>=2) continue; String[] split_path_again = split_path[0].split("\\."); - if (split_path_again.length >= 2) { + if(split_path_again.length>=2) { info.setIsDir(false); - } else { + }else{ info.setIsDir(true); } info.setName(split_path[0]); @@ -130,21 +135,21 @@ public class AliOssClient implements StandardOssClient { infos.add(info); } - } else { - for (OSSObjectSummary objectSummary : sums) { + }else{ + for(OSSObjectSummary objectSummary : sums){ OssInfo info = new OssInfo(); int x = prefix.split("/").length; String[] split_path = objectSummary.getKey().split("/"); - if (split_path.length >= x + 2) + if(split_path.length>=x+2) continue; - String[] split_path_again = split_path[split_path.length - 1].split("\\."); - if (split_path_again.length >= 2) { + String[] split_path_again = split_path[split_path.length-1].split("\\."); + if(split_path_again.length>=2){ info.setIsDir(false); - } else { + }else{ info.setIsDir(true); } - info.setName(split_path[split_path.length - 1]); + info.setName(split_path[split_path.length-1]); info.setPath(objectSummary.getKey()); info.setLength(DataSizeUtil.format(objectSummary.getSize())); @@ -163,7 +168,7 @@ public class AliOssClient implements StandardOssClient { if (!oss.doesBucketExist(bucketName)) { Bucket bucket = oss.createBucket(bucketName); bucket.setRegion("chengdu"); - } else { + }else { return "bucket already exists"; } } catch (Exception e) { @@ -173,13 +178,14 @@ public class AliOssClient implements StandardOssClient { } public String deleteBucket(String bucketName) { - try { - if (oss.doesBucketExist(bucketName)) { + try{ + if (oss.doesBucketExist(bucketName)){ VoidResult voidResult = oss.deleteBucket(bucketName); - } else { + } + else{ return "bucket does not exist"; } - } catch (Exception e) { + }catch(Exception e){ e.printStackTrace(); } return "yes"; @@ -228,9 +234,40 @@ public class AliOssClient implements StandardOssClient { } return getInfo(targetName); } + public HashMap downLoadMany(List listObjectsArgs) throws ExecutionException, InterruptedException { + HashMap list = new HashMap<>(); + List wrappers=new ArrayList<>(); + listObjectsArgs.forEach(args -> { + String bucketName = getBucketName(); + String key = getKey(args, false); + ObjectListing objectListing=oss .listObjects(bucketName,args); + List sums = objectListing.getObjectSummaries(); + sums.forEach(r -> { + try { + WorkerWrapper wrapper1 = new WorkerWrapper.Builder().worker((String dirName, Map allWrappers) -> { + try { + list.put(dirName,oss.getObject(bucketName, r.getKey()).getObjectContent()); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + }).param(r.getKey().toString()).id(r.getETag()).callback((boolean success, String param, WorkResult re)->{ + }).build(); + wrappers.add(wrapper1); + } catch (Exception e) { + e.printStackTrace(); + } + }); + }); + Async.beginWork(3500L,new ThreadPoolExecutor(10, 100, 5L, + TimeUnit.SECONDS, new LinkedBlockingQueue()), wrappers); + return list; + + } @Override public void downLoad(OutputStream os, String targetName) { + String bucketName = getBucketName(); String key = getKey(targetName, false); OSSObject ossObject = oss.getObject(bucketName, key);