minio的sdk更换为s3

This commit is contained in:
xxx
2023-11-13 10:54:56 +08:00
parent f0f316c504
commit 21c016af62
12 changed files with 251 additions and 294 deletions

View File

@@ -15,6 +15,8 @@
*/
package xyz.playedu.api.controller;
import com.amazonaws.services.s3.model.AmazonS3Exception;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.RedisConnectionFailureException;
@@ -40,7 +42,7 @@ public class ExceptionController {
@ExceptionHandler(Exception.class)
public JsonResponse exceptionHandler(Exception e) {
log.error(e.getMessage());
log.error("{}-{}", e, e.getMessage());
return JsonResponse.error("系统错误", 500);
}
@@ -95,4 +97,10 @@ public class ExceptionController {
public JsonResponse serviceExceptionHandler(LimitException e) {
return JsonResponse.error("请稍后再试", 429);
}
@ExceptionHandler(AmazonS3Exception.class)
public JsonResponse serviceExceptionHandler(AmazonS3Exception e) {
log.error("s3错误={}", e.getMessage());
return JsonResponse.error(e.getMessage(), 500);
}
}

View File

@@ -34,10 +34,11 @@ import xyz.playedu.common.domain.AdminUser;
import xyz.playedu.common.exception.NotFoundException;
import xyz.playedu.common.exception.ServiceException;
import xyz.playedu.common.service.AdminUserService;
import xyz.playedu.common.service.MinioService;
import xyz.playedu.common.service.AppConfigService;
import xyz.playedu.common.types.JsonResponse;
import xyz.playedu.common.types.paginate.PaginationResult;
import xyz.playedu.common.types.paginate.ResourcePaginateFilter;
import xyz.playedu.common.util.S3Util;
import xyz.playedu.resource.domain.Resource;
import xyz.playedu.resource.domain.ResourceVideo;
import xyz.playedu.resource.service.ResourceService;
@@ -56,7 +57,7 @@ public class ResourceController {
@Autowired private ResourceVideoService resourceVideoService;
@Autowired private MinioService minioService;
@Autowired private AppConfigService appConfigService;
@Autowired private BackendBus backendBus;
@@ -134,7 +135,8 @@ public class ResourceController {
}
// 删除文件
minioService.removeByPath(resource.getPath());
S3Util s3Util = new S3Util(appConfigService.getS3Config());
s3Util.removeByPath(resource.getPath());
// 如果是视频资源文件则删除对应的时长关联记录
if (BackendConstant.RESOURCE_TYPE_VIDEO.equals(resource.getType())) {
resourceVideoService.removeByRid(resource.getId());
@@ -157,6 +159,8 @@ public class ResourceController {
return JsonResponse.success();
}
S3Util s3Util = new S3Util(appConfigService.getS3Config());
for (Resource resourceItem : resources) {
// 权限校验
if (!backendBus.isSuperAdmin()) {
@@ -166,7 +170,7 @@ public class ResourceController {
}
// 删除资源源文件
minioService.removeByPath(resourceItem.getPath());
s3Util.removeByPath(resourceItem.getPath());
// 如果是视频资源的话还需要删除视频的关联资源,如: 封面截图
if (BackendConstant.RESOURCE_TYPE_VIDEO.equals(resourceItem.getType())) {
resourceVideoService.removeByRid(resourceItem.getId());

View File

@@ -31,9 +31,10 @@ import xyz.playedu.common.constant.BackendConstant;
import xyz.playedu.common.constant.BusinessTypeConstant;
import xyz.playedu.common.context.BCtx;
import xyz.playedu.common.exception.ServiceException;
import xyz.playedu.common.service.MinioService;
import xyz.playedu.common.service.AppConfigService;
import xyz.playedu.common.types.JsonResponse;
import xyz.playedu.common.util.HelperUtil;
import xyz.playedu.common.util.S3Util;
import xyz.playedu.resource.domain.Resource;
import xyz.playedu.resource.service.ResourceService;
import xyz.playedu.resource.service.UploadService;
@@ -44,10 +45,11 @@ import java.util.HashMap;
@Slf4j
@RequestMapping("/backend/v1/upload")
public class UploadController {
@Autowired private MinioService minioService;
@Autowired private UploadService uploadService;
@Autowired private AppConfigService appConfigService;
@Autowired private ResourceService resourceService;
@BackendPermission(slug = BPermissionConstant.UPLOAD)
@@ -74,9 +76,11 @@ public class UploadController {
return JsonResponse.error("该格式文件不支持上传");
}
S3Util s3Util = new S3Util(appConfigService.getS3Config());
String filename = HelperUtil.randomString(32) + "." + extension; // 文件名
String path = BackendConstant.RESOURCE_TYPE_2_DIR.get(type) + filename; // 存储路径
String uploadId = minioService.uploadId(path);
String uploadId = s3Util.uploadId(path);
HashMap<String, String> data = new HashMap<>();
data.put("resource_type", type);
@@ -94,7 +98,9 @@ public class UploadController {
Integer partNumber = MapUtils.getInteger(params, "part_number");
String filename = MapUtils.getString(params, "filename");
String url = minioService.chunkPreSignUrl(filename, partNumber + "", uploadId);
S3Util s3Util = new S3Util(appConfigService.getS3Config());
String url = s3Util.generatePartUploadPreSignUrl(filename, partNumber + "", uploadId);
HashMap<String, String> data = new HashMap<>();
data.put("url", url);
@@ -115,7 +121,8 @@ public class UploadController {
String originalFilename = req.getOriginalFilename().replaceAll("(?i)." + extension, "");
// 合并资源文件
String url = minioService.merge(req.getFilename(), req.getUploadId());
S3Util s3Util = new S3Util(appConfigService.getS3Config());
String url = s3Util.merge(req.getFilename(), req.getUploadId());
// 资源素材保存
Resource videoResource =
@@ -162,7 +169,9 @@ public class UploadController {
return JsonResponse.error("uploadId必填");
}
String url = minioService.merge(filename, uploadId);
S3Util s3Util = new S3Util(appConfigService.getS3Config());
String url = s3Util.merge(filename, uploadId);
HashMap<String, Object> data = new HashMap<>();
data.put("url", url);