Merge remote-tracking branch 'refs/remotes/origin/dev'
This commit is contained in:
@@ -24,11 +24,27 @@
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
<version>3.0.7</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<groupId>org.springframework</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-loadbalancer</artifactId>
|
||||
<version>3.0.6</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>reactor-core</artifactId>
|
||||
<groupId>io.projectreactor</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
|
@@ -68,11 +68,28 @@
|
||||
<groupId>com.schisandra</groupId>
|
||||
<artifactId>schisandra-cloud-storage-oss-domain</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.schisandra</groupId>
|
||||
<artifactId>schisandra-cloud-storage-oss-api</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
<version>1.9.7</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jd.platform</groupId>
|
||||
<artifactId>schisandra-cloud-storage-asyncTool</artifactId>
|
||||
<version>1.4.1-SNAPSHOT</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@@ -1,5 +1,6 @@
|
||||
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.amazonaws.util.IOUtils;
|
||||
@@ -8,7 +9,6 @@ import com.schisandra.oss.application.convert.SchisandraOssMinioDTOConverter;
|
||||
import com.schisandra.oss.application.dto.SchisandraOssMinioDTO;
|
||||
import com.schisandra.oss.application.oss.core.minio.MinioOssClient;
|
||||
import com.schisandra.oss.application.oss.core.minio.MinioOssConfiguration;
|
||||
import com.schisandra.oss.application.oss.core.minio.model.MinioOssConfig;
|
||||
import com.schisandra.oss.application.oss.model.OssInfo;
|
||||
import com.schisandra.oss.common.entity.Result;
|
||||
import com.schisandra.oss.domain.bo.SchisandraOssMinioBO;
|
||||
@@ -27,7 +27,9 @@ import java.io.InputStream;
|
||||
import java.net.URLEncoder;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* controller
|
||||
@@ -96,18 +98,22 @@ public class SchisandraOssMinioController {
|
||||
* @date: 2024/6/26 13:56
|
||||
*/
|
||||
@GetMapping("downloadMinioFile")
|
||||
public void getMinioFile(@RequestParam String bucket, @RequestParam String userId, @RequestParam String filePath, HttpServletResponse response) throws Exception {
|
||||
public void getMinioFile(@RequestParam String bucket, @RequestParam String userId, @RequestParam List<String> listObjectsArgs , HttpServletResponse response) throws Exception {
|
||||
Preconditions.checkNotNull(bucket, "不能为空");
|
||||
Preconditions.checkNotNull(userId, "不能为空");
|
||||
Preconditions.checkNotNull(filePath, "不能为空");
|
||||
Preconditions.checkNotNull(listObjectsArgs, "不能为空");
|
||||
MinioOssClient bean = SpringUtil.getBean(userId);
|
||||
bean.getMinioOssConfig().setBucketName(bucket);
|
||||
InputStream stream = bean.getMinioObject(bucket, filePath);
|
||||
HashMap<String, InputStream> list=bean.getTargetDir(listObjectsArgs);
|
||||
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");
|
||||
IOUtils.copy(stream, output);
|
||||
String[] path = new String[list.size()];
|
||||
InputStream[] inputStreams= new InputStream[list.size()];
|
||||
path=list.keySet().toArray(path);
|
||||
inputStreams=list.values().toArray(inputStreams);
|
||||
ZipUtil.zip(output, path, inputStreams);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -1,18 +1,32 @@
|
||||
package com.schisandra.oss.application.controller;
|
||||
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
import com.qcloud.cos.model.CopyResult;
|
||||
import com.qcloud.cos.model.DeleteObjectsRequest;
|
||||
import com.qcloud.cos.model.DeleteObjectsResult;
|
||||
import com.schisandra.oss.application.convert.SchisandraOssTencentDTOConverter;
|
||||
import com.schisandra.oss.application.dto.SchisandraOssTencentDTO;
|
||||
|
||||
import com.schisandra.oss.application.oss.core.minio.MinioOssClient;
|
||||
import com.schisandra.oss.application.oss.core.tencent.TencentOssClient;
|
||||
import com.schisandra.oss.application.oss.core.tencent.TencentOssConfiguration;
|
||||
import com.schisandra.oss.common.entity.Result;
|
||||
import com.schisandra.oss.domain.bo.SchisandraOssTencentBO;
|
||||
import com.schisandra.oss.domain.service.SchisandraOssTencentDomainService;
|
||||
import io.minio.errors.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 腾讯云对象存储配置表 controller
|
||||
@@ -27,6 +41,190 @@ public class SchisandraOssTencentController {
|
||||
|
||||
@Resource
|
||||
private SchisandraOssTencentDomainService schisandraOssTencentDomainService;
|
||||
@Resource
|
||||
TencentOssConfiguration tencentOssConfiguration;
|
||||
|
||||
@PostMapping("init")
|
||||
public void initTencent(@RequestParam String userId) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
|
||||
|
||||
|
||||
if (log.isInfoEnabled()) {
|
||||
log.info("SchisandraOssTencentController.init.userId:{}", userId);
|
||||
}
|
||||
Preconditions.checkNotNull(userId, "用户id不能为空!");
|
||||
|
||||
Result result = tencentOssConfiguration.tencentOssClient(userId);
|
||||
if (result.getSuccess()) {
|
||||
log.info("用户: {}-> TencentOSS 初始化完成!", userId);
|
||||
} else {
|
||||
log.error("用户: {}-> TencentOSS 初始化完成!", userId);
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* @description: 查询所有存储桶
|
||||
* @param: [userId]
|
||||
* @return: com.schisandra.oss.common.entity.Result<java.lang.String>
|
||||
* @author zlg
|
||||
* @date: 2024/6/29 13:28
|
||||
*/
|
||||
@PostMapping("seleteBucket")
|
||||
public Result<String> seleteBucket(@RequestParam String userId) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
|
||||
Preconditions.checkNotNull(userId, "不能为空");
|
||||
TencentOssClient bean = SpringUtil.getBean(userId);
|
||||
return Result.ok(bean.selectAllBucket());
|
||||
}
|
||||
/**
|
||||
* @description: 查询指定目录
|
||||
* @param: [userId, dirName, bucket]
|
||||
* @return: com.schisandra.oss.common.entity.Result<java.lang.String>
|
||||
* @author zlg
|
||||
* @date: 2024/6/29 13:28
|
||||
*/
|
||||
@GetMapping("listTencentDir")
|
||||
public Result<String> listMinioInfo(@RequestParam String userId, @RequestParam String dirName,@RequestParam String bucket) throws Exception {
|
||||
Preconditions.checkNotNull(bucket, "不能为空");
|
||||
Preconditions.checkNotNull(userId, "不能为空");
|
||||
TencentOssClient bean = SpringUtil.getBean(userId);
|
||||
bean.getTencentOssConfig().setBucketName(bucket);
|
||||
return Result.ok(bean.listTargetDir(bucket, dirName));
|
||||
}
|
||||
/**
|
||||
* @description: 创建桶
|
||||
* @param: [userId, bucket, appId]
|
||||
* @return: com.schisandra.oss.common.entity.Result<java.lang.String>
|
||||
* @author zlg
|
||||
* @date: 2024/6/29 13:27
|
||||
*/
|
||||
@PostMapping("createBucket")
|
||||
public Result<String> createBucket(@RequestParam String userId, @RequestParam String bucket,@RequestParam String appId) {
|
||||
Preconditions.checkNotNull(userId, "不能为空");
|
||||
Preconditions.checkNotNull(bucket, "不能为空");
|
||||
TencentOssClient bean = SpringUtil.getBean(userId);
|
||||
// bean.getTencentOssConfig().setBucketName(bucket);
|
||||
return Result.ok(bean.createBuctet(bucket,appId));
|
||||
}
|
||||
/**
|
||||
* @description: 删除腾讯云对象存储bucket
|
||||
* @param: [userId, bucket, appId]
|
||||
* @return: com.schisandra.oss.common.entity.Result<java.lang.String>
|
||||
* @author zlg
|
||||
* @date: 2024/6/29 13:27
|
||||
*/
|
||||
@PostMapping("deleteBucket")
|
||||
public Result<String> deleteBucket(@RequestParam String userId, @RequestParam String bucket,@RequestParam String appId) {
|
||||
Preconditions.checkNotNull(userId, "不能为空");
|
||||
Preconditions.checkNotNull(bucket, "不能为空");
|
||||
TencentOssClient bean = SpringUtil.getBean(userId);
|
||||
// bean.getTencentOssConfig().setBucketName(bucket);
|
||||
if (bean.deleteBucket(bucket,appId)){
|
||||
return Result.ok();
|
||||
}else {
|
||||
return Result.fail();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @description: 分享腾讯云对象存储链接
|
||||
* @param: [userId, bucket, target]
|
||||
* @return: com.schisandra.oss.common.entity.Result<java.lang.String>
|
||||
* @author zlg
|
||||
* @date: 2024/6/29 13:27
|
||||
*/
|
||||
@GetMapping("shareTencentUrl")
|
||||
public Result<String> shareTencentUrl(@RequestParam String userId, @RequestParam String bucket,@RequestParam String target) {
|
||||
Preconditions.checkNotNull(userId, "不能为空");
|
||||
Preconditions.checkNotNull(bucket, "不能为空");
|
||||
TencentOssClient bean = SpringUtil.getBean(userId);
|
||||
return Result.ok(bean.shareTencentObject(bucket,target));
|
||||
}
|
||||
/**
|
||||
* @description: 腾讯云对象存储上传文件
|
||||
* @param: [userId, bucket, target, file]
|
||||
* @return: com.schisandra.oss.common.entity.Result<java.lang.String>
|
||||
* @author zlg
|
||||
* @date: 2024/7/1 16:01
|
||||
*/
|
||||
@PostMapping("uploadTencent")
|
||||
public Result<String> 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();
|
||||
}
|
||||
TencentOssClient bean = SpringUtil.getBean(userId);
|
||||
InputStream is = file.getInputStream();
|
||||
bean.getTencentOssConfig().setBucketName(bucket);
|
||||
return Result.ok(bean.upLoadParts(is,target,file.getOriginalFilename()));
|
||||
}
|
||||
/**
|
||||
* @description: 腾讯云对象存储下载文件
|
||||
* @param: [userId, bucket, target]
|
||||
* @return: com.schisandra.oss.common.entity.Result<java.lang.String>
|
||||
* @author zlg
|
||||
* @date: 2024/7/1 16:02
|
||||
*/
|
||||
@GetMapping("downloadTencent")
|
||||
public Result<String> downloadTencent(@RequestParam String userId, @RequestParam String bucket, @RequestParam String target) {
|
||||
Preconditions.checkNotNull(userId, "不能为空");
|
||||
Preconditions.checkNotNull(bucket, "不能为空");
|
||||
TencentOssClient bean = SpringUtil.getBean(userId);
|
||||
return Result.ok(bean.downloadTencent(bucket,target));
|
||||
}
|
||||
/**
|
||||
* @description: 腾讯云对象存储移动文件
|
||||
* @param: [userId, sourceBucket, targetBucket, targetName, SourceName]
|
||||
* @return: com.schisandra.oss.common.entity.Result<java.lang.String>
|
||||
* @author zlg
|
||||
* @date: 2024/7/1 16:02
|
||||
*/
|
||||
@PostMapping("moveTencent")
|
||||
public Result<String> 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(userId);
|
||||
CopyResult copyResult = bean.moveTencent(SourceName, targetName, sourceBucket, targetBucket);
|
||||
if (copyResult==null){
|
||||
return Result.fail();
|
||||
}
|
||||
return Result.ok(copyResult);
|
||||
}
|
||||
/**
|
||||
* @description: 腾讯云对象存储复制文件
|
||||
* @param: [userId, sourceBucket, targetBucket, targetName, SourceName]
|
||||
* @return: com.schisandra.oss.common.entity.Result<java.lang.String>
|
||||
* @author zlg
|
||||
* @date: 2024/7/1 16:02
|
||||
*/
|
||||
@PostMapping("copyTencent")
|
||||
public Result<String> 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(userId);
|
||||
CopyResult copyResult = bean.copyTencent(SourceName,targetName , sourceBucket, targetBucket);
|
||||
if (copyResult==null){
|
||||
return Result.fail();
|
||||
}
|
||||
return Result.ok(copyResult);
|
||||
}
|
||||
/**
|
||||
* @description: 腾讯云对象存储删除文件
|
||||
* @param: [userId, bucketName, target]
|
||||
* @return: com.schisandra.oss.common.entity.Result<java.lang.String>
|
||||
* @author zlg
|
||||
* @date: 2024/7/1 16:02
|
||||
*/
|
||||
@PostMapping("deleteTencent")
|
||||
public Result<String> deleteTencent(@RequestParam String userId, @RequestParam String bucketName, @RequestParam List<DeleteObjectsRequest.KeyVersion> target) throws IOException {
|
||||
Preconditions.checkNotNull(userId, "不能为空");
|
||||
TencentOssClient bean = SpringUtil.getBean(userId);
|
||||
DeleteObjectsResult deleteResult = bean.deleteTencent(bucketName, target);
|
||||
if (deleteResult==null){
|
||||
return Result.fail();
|
||||
}
|
||||
return Result.ok(deleteResult);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 新增腾讯云对象存储配置表
|
||||
|
@@ -12,6 +12,9 @@ import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.druid.util.StringUtils;
|
||||
import com.aliyun.oss.common.utils.HttpHeaders;
|
||||
import com.jd.platform.async.executor.Async;
|
||||
import com.jd.platform.async.worker.WorkResult;
|
||||
import com.jd.platform.async.wrapper.WorkerWrapper;
|
||||
import com.schisandra.oss.application.oss.constant.OssConstant;
|
||||
import com.schisandra.oss.application.oss.core.StandardOssClient;
|
||||
import com.schisandra.oss.application.oss.core.minio.model.MinioOssConfig;
|
||||
@@ -41,6 +44,9 @@ import java.io.OutputStream;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
@@ -153,6 +159,37 @@ public class MinioOssClient implements StandardOssClient {
|
||||
return names;
|
||||
}
|
||||
|
||||
public HashMap<String,InputStream> getTargetDir(List<String> listObjectsArgs) throws Exception {
|
||||
HashMap<String,InputStream> list = new HashMap<>();
|
||||
List<WorkerWrapper> wrappers=new ArrayList<>();
|
||||
listObjectsArgs.forEach(args -> {
|
||||
minioClient.listObjects(
|
||||
ListObjectsArgs.builder().prefix(args).bucket(getBucket()).recursive(true).build()).forEach(r -> {
|
||||
OssInfo info = new OssInfo();
|
||||
try {
|
||||
WorkerWrapper wrapper1 = new WorkerWrapper.Builder<String, InputStream>().worker((String dirName, Map<String, WorkerWrapper> allWrappers) -> {
|
||||
try {
|
||||
list.put(dirName,getMinioObject(getBucket(), dirName));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}).param(r.get().objectName()).id(r.get().versionId()).callback((boolean success, String param,WorkResult<InputStream> re)->{
|
||||
System.out.println("success:"+success+" param:");
|
||||
}).build();
|
||||
wrappers.add(wrapper1);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Async.beginWork(3500L,new ThreadPoolExecutor(10, 100, 5L,
|
||||
TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()), wrappers);
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @description: 获取目录文件信息
|
||||
* @param: [bucket, dirName]
|
||||
@@ -389,7 +426,7 @@ public class MinioOssClient implements StandardOssClient {
|
||||
};
|
||||
}
|
||||
|
||||
private String getBucket() {
|
||||
public String getBucket() {
|
||||
String bucketName = minioOssConfig.getBucketName();
|
||||
try {
|
||||
if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
|
||||
|
@@ -7,12 +7,16 @@ import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.io.file.FileNameUtil;
|
||||
import cn.hutool.core.io.unit.DataSizeUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.qcloud.cos.COSClient;
|
||||
import com.qcloud.cos.exception.CosClientException;
|
||||
import com.qcloud.cos.exception.CosServiceException;
|
||||
import com.qcloud.cos.exception.MultiObjectDeleteException;
|
||||
import com.qcloud.cos.model.*;
|
||||
|
||||
import com.qcloud.cos.transfer.*;
|
||||
import com.schisandra.oss.application.oss.constant.OssConstant;
|
||||
import com.schisandra.oss.application.oss.core.StandardOssClient;
|
||||
import com.schisandra.oss.application.oss.core.tencent.model.TencentOssConfig;
|
||||
@@ -24,37 +28,262 @@ import com.schisandra.oss.application.oss.model.download.DownloadCheckPoint;
|
||||
import com.schisandra.oss.application.oss.model.download.DownloadObjectStat;
|
||||
import com.schisandra.oss.application.oss.model.upload.*;
|
||||
import com.schisandra.oss.application.oss.utils.OssPathUtil;
|
||||
import io.minio.errors.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URL;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Component
|
||||
public class TencentOssClient implements StandardOssClient {
|
||||
|
||||
public static final String COS_OBJECT_NAME = "cosClient";
|
||||
|
||||
private COSClient cosClient;
|
||||
|
||||
private TencentOssConfig tencentOssConfig;
|
||||
|
||||
TransferManager createTransferManager() {
|
||||
// 自定义线程池大小,建议在客户端与 COS 网络充足(例如使用腾讯云的 CVM,同地域上传 COS)的情况下,设置成16或32即可,可较充分的利用网络资源
|
||||
// 对于使用公网传输且网络带宽质量不高的情况,建议减小该值,避免因网速过慢,造成请求超时。
|
||||
ExecutorService threadPool = Executors.newFixedThreadPool(32);
|
||||
// 传入一个 threadpool, 若不传入线程池,默认 TransferManager 中会生成一个单线程的线程池。
|
||||
TransferManager transferManager = new TransferManager(cosClient, threadPool);
|
||||
// 设置高级接口的配置项
|
||||
// 分块上传阈值和分块大小分别为 5MB 和 1MB
|
||||
TransferManagerConfiguration transferManagerConfiguration = new TransferManagerConfiguration();
|
||||
transferManagerConfiguration.setMultipartUploadThreshold(5 * 1024 * 1024);
|
||||
transferManagerConfiguration.setMinimumUploadPartSize(1024 * 1024);
|
||||
transferManager.setConfiguration(transferManagerConfiguration);
|
||||
return transferManager;
|
||||
}
|
||||
|
||||
void shutdownTransferManager(TransferManager transferManager) {
|
||||
// 指定参数为 true, 则同时会关闭 transferManager 内部的 COSClient 实例。
|
||||
// 指定参数为 false, 则不会关闭 transferManager 内部的 COSClient 实例。
|
||||
transferManager.shutdownNow(false);
|
||||
}
|
||||
|
||||
public TencentOssConfig getTencentOssConfig() {
|
||||
return tencentOssConfig;
|
||||
}
|
||||
|
||||
public void setTencentOssConfig(TencentOssConfig tencentOssConfig) {
|
||||
this.tencentOssConfig = tencentOssConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 创建腾讯云对象存储
|
||||
* @param: [bucketName, appId]
|
||||
* @return: com.qcloud.cos.model.Bucket
|
||||
* @author zlg
|
||||
* @date: 2024/6/29 10:13
|
||||
*/
|
||||
public Bucket createBuctet(String bucketName, String appId) {
|
||||
//存储桶名称,格式:BucketName-APPID
|
||||
bucketName = bucketName + "-" + appId;
|
||||
if (!cosClient.doesBucketExist(bucketName)) {
|
||||
return null;
|
||||
}
|
||||
CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
|
||||
createBucketRequest.setCannedAcl(CannedAccessControlList.Private);
|
||||
Bucket bucketResult = cosClient.createBucket(createBucketRequest);
|
||||
return bucketResult;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @description: 删除腾讯云对象存储
|
||||
* @param: [bucketName, appId]
|
||||
* @return: java.lang.Boolean
|
||||
* @author zlg
|
||||
* @date: 2024/6/29 10:17
|
||||
*/
|
||||
public Boolean deleteBucket(String bucketName, String appId) {
|
||||
bucketName = bucketName + appId;
|
||||
try {
|
||||
cosClient.deleteBucket(bucketName);
|
||||
} catch (CosClientException e) {
|
||||
log.error("删除腾讯云对象存储失败", e);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 查找腾讯云对象存储
|
||||
* @param: []
|
||||
* @return: java.util.HashMap<java.lang.String, java.lang.String>
|
||||
* @author zlg
|
||||
* @date: 2024/6/29 10:13
|
||||
*/
|
||||
public HashMap<String, String> selectAllBucket() throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
|
||||
List<Bucket> list = cosClient.listBuckets();
|
||||
HashMap<String, String> names = new HashMap<>();
|
||||
list.forEach(bucket -> {
|
||||
names.put(bucket.getName(), getTencentBucketSize(bucket.getName()));
|
||||
});
|
||||
return names;
|
||||
}
|
||||
|
||||
public String getTencentBucketSize(String bucket) {
|
||||
ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
|
||||
listObjectsRequest.setBucketName(bucket);
|
||||
listObjectsRequest.setMaxKeys(1000);
|
||||
ObjectListing results = cosClient.listObjects(listObjectsRequest);
|
||||
long fileSize = 0L;
|
||||
for (COSObjectSummary objectSummary : results.getObjectSummaries()) {
|
||||
fileSize = objectSummary.getSize() + fileSize;
|
||||
}
|
||||
return DataSizeUtil.format(fileSize);
|
||||
}
|
||||
public Download downloadTencent(String bucket, String target) {
|
||||
|
||||
ExecutorService threadPool = Executors.newFixedThreadPool(4);
|
||||
// 传入一个threadpool, 若不传入线程池, 默认TransferManager中会生成一个单线程的线程池。
|
||||
TransferManager transferManager = new TransferManager(cosClient, threadPool);
|
||||
|
||||
try {
|
||||
Download download = transferManager.download(bucket, target,
|
||||
new File("D:\\java_project\\schisandra-cloud-storage\\"));
|
||||
download.waitForCompletion();
|
||||
return download;
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public HashMap<String, String> listTargetDir(String bucket, String dirName) {
|
||||
ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
|
||||
listObjectsRequest.setBucketName(bucket);
|
||||
listObjectsRequest.setPrefix(dirName);
|
||||
listObjectsRequest.setMaxKeys(1000);
|
||||
HashMap<String, String> map = new HashMap<>();
|
||||
ObjectListing results = cosClient.listObjects(listObjectsRequest);
|
||||
long fileSize = 0L;
|
||||
for (COSObjectSummary objectSummary : results.getObjectSummaries()) {
|
||||
if (!objectSummary.getKey().equals(dirName + "/")) {
|
||||
map.put(objectSummary.getKey(), DataSizeUtil.format(objectSummary.getSize()));
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public String shareTencentObject(String bucket, String target) {
|
||||
try {
|
||||
Date expirationDate = new Date(System.currentTimeMillis() + 30 * 60 * 1000);
|
||||
|
||||
URL url = cosClient.generatePresignedUrl(bucket, target, expirationDate);
|
||||
return String.valueOf(url);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return "链接获取失败";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Upload upLoadParts(InputStream is, String targetName, String fileName) {
|
||||
TransferManager transferManager = createTransferManager();
|
||||
ObjectMetadata objectMetadata = new ObjectMetadata();
|
||||
// int inputStreamLength = 1024 * 1024;
|
||||
// byte data[] = new byte[inputStreamLength];
|
||||
try {
|
||||
objectMetadata.setContentLength(is.available());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
PutObjectRequest putObjectRequest = new PutObjectRequest(getBucket(), targetName, is, objectMetadata);
|
||||
try {
|
||||
// 高级接口会返回一个异步结果Upload
|
||||
// 可同步地调用 waitForUploadResult 方法等待上传完成,成功返回 UploadResult, 失败抛出异常
|
||||
Upload upload = transferManager.upload(putObjectRequest);
|
||||
UploadResult uploadResult = upload.waitForUploadResult();
|
||||
return upload;
|
||||
} catch (CosServiceException e) {
|
||||
e.printStackTrace();
|
||||
} catch (CosClientException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
shutdownTransferManager(transferManager);
|
||||
return null;
|
||||
}
|
||||
|
||||
public CopyResult copyTencent(String sourceName, String targetName, String sourceBucket, String targetBucket) {
|
||||
TransferManager transferManager = createTransferManager();
|
||||
ObjectMetadata objectMetadata = new ObjectMetadata();
|
||||
|
||||
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(sourceBucket,
|
||||
sourceName, targetBucket, targetName);
|
||||
try {
|
||||
Copy copy = transferManager.copy(copyObjectRequest);
|
||||
// 高级接口会返回一个异步结果 Copy
|
||||
// 可同步的调用 waitForCopyResult 等待复制结束, 成功返回 CopyResult, 失败抛出异常
|
||||
CopyResult copyResult = copy.waitForCopyResult();
|
||||
return copyResult;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public CopyResult moveTencent(String sourceName, String targetName, String sourceBucket, String targetBucket) {
|
||||
TransferManager transferManager = createTransferManager();
|
||||
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(sourceBucket,
|
||||
sourceName, targetBucket, targetName);
|
||||
ArrayList<DeleteObjectsRequest.KeyVersion> keyList = new ArrayList<>();
|
||||
keyList.add(new DeleteObjectsRequest.KeyVersion(sourceName));
|
||||
try {
|
||||
Copy copy = transferManager.copy(copyObjectRequest);
|
||||
// 高级接口会返回一个异步结果 Copy
|
||||
// 可同步的调用 waitForCopyResult 等待复制结束, 成功返回 CopyResult, 失败抛出异常
|
||||
CopyResult moveResult = copy.waitForCopyResult();
|
||||
deleteTencent(targetBucket,keyList);
|
||||
return moveResult;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public DeleteObjectsResult deleteTencent(String bucket, List<DeleteObjectsRequest.KeyVersion> targetName) {
|
||||
DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucket);
|
||||
deleteObjectsRequest.setKeys(targetName);
|
||||
try {
|
||||
DeleteObjectsResult deleteObjectsResult = cosClient.deleteObjects(deleteObjectsRequest);
|
||||
List<DeleteObjectsResult.DeletedObject> deleteObjectResultArray = deleteObjectsResult.getDeletedObjects();
|
||||
return deleteObjectsResult;
|
||||
} catch (MultiObjectDeleteException mde) {
|
||||
// 如果部分删除成功部分失败, 返回 MultiObjectDeleteException
|
||||
List<DeleteObjectsResult.DeletedObject> deleteObjects = mde.getDeletedObjects();
|
||||
List<MultiObjectDeleteException.DeleteError> deleteErrors = mde.getErrors();
|
||||
mde.printStackTrace();
|
||||
} catch (CosServiceException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OssInfo upLoad(InputStream is, String targetName, Boolean isOverride) {
|
||||
String bucketName = getBucket();
|
||||
String key = getKey(targetName, false);
|
||||
|
||||
if (isOverride || !cosClient.doesObjectExist(bucketName, key)) {
|
||||
cosClient.putObject(bucketName, key, is, new ObjectMetadata());
|
||||
}
|
||||
return getInfo(targetName);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -112,7 +341,6 @@ public class TencentOssClient implements StandardOssClient {
|
||||
partResult = new UpLoadPartResult(partNum + 1, uploadPart.getOffset(), partSize);
|
||||
try {
|
||||
inputStream.skip(uploadPart.getOffset());
|
||||
|
||||
UploadPartRequest uploadPartRequest = new UploadPartRequest();
|
||||
uploadPartRequest.setBucketName(upLoadCheckPoint.getBucket());
|
||||
uploadPartRequest.setKey(upLoadCheckPoint.getKey());
|
||||
@@ -120,9 +348,7 @@ public class TencentOssClient implements StandardOssClient {
|
||||
uploadPartRequest.setInputStream(inputStream);
|
||||
uploadPartRequest.setPartSize(partSize);
|
||||
uploadPartRequest.setPartNumber(uploadPart.getNumber());
|
||||
|
||||
UploadPartResult uploadPartResponse = cosClient.uploadPart(uploadPartRequest);
|
||||
|
||||
partResult.setNumber(uploadPartResponse.getPartNumber());
|
||||
partResult.setEntityTag(new UpLoadPartEntityTag().setETag(uploadPartResponse.getETag())
|
||||
.setPartNumber(uploadPartResponse.getPartNumber()));
|
||||
@@ -196,11 +422,26 @@ public class TencentOssClient implements StandardOssClient {
|
||||
|
||||
@Override
|
||||
public void copy(String sourceName, String targetName, Boolean isOverride) {
|
||||
String bucketName = getBucket();
|
||||
String targetKey = getKey(targetName, false);
|
||||
if (isOverride || !cosClient.doesObjectExist(bucketName, targetKey)) {
|
||||
cosClient.copyObject(getBucket(), getKey(sourceName, false), getBucket(), targetKey);
|
||||
TransferManager transferManager = createTransferManager();
|
||||
ObjectMetadata objectMetadata = new ObjectMetadata();
|
||||
|
||||
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(getBucket(),
|
||||
sourceName, getBucket(), targetName);
|
||||
|
||||
try {
|
||||
Copy copy = transferManager.copy(copyObjectRequest);
|
||||
// 高级接口会返回一个异步结果 Copy
|
||||
// 可同步的调用 waitForCopyResult 等待复制结束, 成功返回 CopyResult, 失败抛出异常
|
||||
CopyResult copyResult = copy.waitForCopyResult();
|
||||
System.out.println(copyResult.getRequestId());
|
||||
} catch (CosServiceException e) {
|
||||
e.printStackTrace();
|
||||
} catch (CosClientException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -265,7 +506,7 @@ public class TencentOssClient implements StandardOssClient {
|
||||
};
|
||||
}
|
||||
|
||||
private String getBucket() {
|
||||
public String getBucket() {
|
||||
String bucketName = tencentOssConfig.getBucketName();
|
||||
if (!cosClient.doesBucketExist(bucketName)) {
|
||||
cosClient.createBucket(bucketName);
|
||||
|
@@ -1,23 +1,32 @@
|
||||
package com.schisandra.oss.application.oss.core.tencent;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
|
||||
|
||||
import com.qcloud.cos.COSClient;
|
||||
import com.qcloud.cos.ClientConfig;
|
||||
import com.qcloud.cos.auth.BasicCOSCredentials;
|
||||
import com.qcloud.cos.auth.COSCredentials;
|
||||
import com.qcloud.cos.http.HttpProtocol;
|
||||
import com.qcloud.cos.region.Region;
|
||||
import com.schisandra.oss.application.convert.SchisandraOssTencentDTOConverter;
|
||||
import com.schisandra.oss.application.dto.SchisandraOssTencentDTO;
|
||||
import com.schisandra.oss.application.oss.core.StandardOssClient;
|
||||
import com.schisandra.oss.application.oss.core.tencent.model.TencentOssClientConfig;
|
||||
import com.schisandra.oss.application.oss.core.tencent.model.TencentOssConfig;
|
||||
import com.schisandra.oss.common.entity.Result;
|
||||
import com.schisandra.oss.domain.bo.SchisandraOssTencentBO;
|
||||
import com.schisandra.oss.domain.service.SchisandraOssTencentDomainService;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Optional;
|
||||
|
||||
import static com.tencentcloudapi.common.profile.Region.Chengdu;
|
||||
|
||||
/**
|
||||
* @author zlg
|
||||
* @description:腾讯云对象存储配置
|
||||
@@ -33,7 +42,7 @@ public class TencentOssConfiguration {
|
||||
private SchisandraOssTencentDomainService schisandraOssTencentDomainService;
|
||||
|
||||
|
||||
public StandardOssClient tencentOssClient(String userId) {
|
||||
public Result tencentOssClient(String userId) {
|
||||
SchisandraOssTencentBO schisandraOssTencentBO = schisandraOssTencentDomainService.getTencentOssConfig(userId);
|
||||
SchisandraOssTencentDTO schisandraOssTencentDTO = SchisandraOssTencentDTOConverter.INSTANCE.convertBOToDTO(schisandraOssTencentBO);
|
||||
if (ObjectUtil.isEmpty(schisandraOssTencentDTO)) {
|
||||
@@ -45,11 +54,25 @@ public class TencentOssConfiguration {
|
||||
TencentOssConfig tencentOssConfig = new TencentOssConfig();
|
||||
tencentOssConfig.setSecretId(secretId);
|
||||
tencentOssConfig.setSecretKey(secretKey);
|
||||
return tencentOssClient(tencentOssConfig);
|
||||
|
||||
|
||||
// COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
|
||||
// Region region = new Region();
|
||||
//高级设置
|
||||
TencentOssClientConfig tencentOssClientConfig = new TencentOssClientConfig();
|
||||
tencentOssClientConfig.setRegion("ap-chengdu");
|
||||
// COSClient cosClient = new COSClient(cred, clientConfig.toClientConfig());
|
||||
tencentOssConfig.setClientConfig(tencentOssClientConfig);
|
||||
//
|
||||
|
||||
try {
|
||||
SpringUtil.registerBean(userId, tencentOssClient(tencentOssConfig));
|
||||
return Result.ok();
|
||||
} catch (Exception e) {
|
||||
log.error("TencentOssConfiguration.tencentOssClient:{}", e.getMessage(), e);
|
||||
return Result.fail();
|
||||
}
|
||||
}
|
||||
|
||||
private StandardOssClient tencentOssClient(TencentOssConfig tencentOssConfig) {
|
||||
TencentOssClientConfig clientConfig = Optional.ofNullable(tencentOssConfig.getClientConfig()).orElse(new TencentOssClientConfig());
|
||||
COSCredentials cosCredentials = cosCredentials(tencentOssConfig);
|
||||
|
@@ -90,7 +90,7 @@ public class TencentOssClientConfig {
|
||||
|
||||
public ClientConfig toClientConfig() {
|
||||
ClientConfig clientConfig = new ClientConfig();
|
||||
BeanUtil.copyProperties(this, clientConfig, new CopyOptions().setIgnoreNullValue(true).setIgnoreProperties("region"));
|
||||
// BeanUtil.copyProperties(this, clientConfig, new CopyOptions().setIgnoreNullValue(true).setIgnoreProperties("region"));
|
||||
if (ObjectUtil.isNotEmpty(region)) {
|
||||
clientConfig.setRegion(new Region(region));
|
||||
}
|
||||
|
@@ -11,6 +11,9 @@
|
||||
<packaging>jar</packaging>
|
||||
<name>schisandra-cloud-storage-oss-common</name>
|
||||
|
||||
|
||||
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
@@ -176,7 +179,7 @@
|
||||
<dependency>
|
||||
<groupId>com.qcloud</groupId>
|
||||
<artifactId>cos_api</artifactId>
|
||||
<version>5.6.69</version>
|
||||
<version>5.6.219</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 七牛云依赖 -->
|
||||
|
@@ -25,12 +25,24 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||
<version>2.4.2</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<!--druid连接池-->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
<version>1.1.21</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>spring-boot</artifactId>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<!--mysql-->
|
||||
<dependency>
|
||||
@@ -42,6 +54,28 @@
|
||||
<groupId>com.mybatis-flex</groupId>
|
||||
<artifactId>mybatis-flex-spring-boot-starter</artifactId>
|
||||
<version>1.9.3</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
<groupId>org.springframework</groupId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<groupId>org.springframework</groupId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<groupId>org.springframework</groupId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<groupId>org.springframework</groupId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.mybatis-flex</groupId>
|
||||
@@ -53,6 +87,24 @@
|
||||
<groupId>com.schisandra</groupId>
|
||||
<artifactId>schisandra-cloud-storage-oss-common</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<artifactId>spring-context-support</artifactId>
|
||||
<groupId>org.springframework</groupId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<artifactId>spring-tx</artifactId>
|
||||
<groupId>org.springframework</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
Reference in New Issue
Block a user