mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-07-24 02:09:35 +08:00
minio分片上传
This commit is contained in:
parent
32f6966706
commit
0db709cb3e
@ -1,10 +1,12 @@
|
||||
package xyz.playedu.api.config;
|
||||
|
||||
import io.minio.MinioAsyncClient;
|
||||
import io.minio.MinioClient;
|
||||
import lombok.Data;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import xyz.playedu.api.vendor.PlayEduMinioClient;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
@ -36,4 +38,10 @@ public class MinioConfig {
|
||||
.credentials(this.accessKey, this.secretKey)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PlayEduMinioClient getPlayEduMinioClient() {
|
||||
MinioAsyncClient client = PlayEduMinioClient.builder().endpoint(this.endPoint).credentials(this.accessKey, this.secretKey).build();
|
||||
return new PlayEduMinioClient(client);
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,11 @@
|
||||
package xyz.playedu.api.controller.backend;
|
||||
|
||||
import com.google.common.collect.Multimap;
|
||||
import io.minio.*;
|
||||
import io.minio.http.Method;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import org.checkerframework.checker.units.qual.A;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
@ -14,8 +16,8 @@ import xyz.playedu.api.service.ResourceCategoryService;
|
||||
import xyz.playedu.api.service.ResourceService;
|
||||
import xyz.playedu.api.types.JsonResponse;
|
||||
import xyz.playedu.api.util.HelperUtil;
|
||||
import xyz.playedu.api.vendor.PlayEduMinioClient;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@ -41,6 +43,9 @@ public class UploadController {
|
||||
@Autowired
|
||||
private MinioClient minioClient;
|
||||
|
||||
@Autowired
|
||||
private PlayEduMinioClient playEduMinioClient;
|
||||
|
||||
@PostMapping("/image")
|
||||
public JsonResponse image(@RequestParam HashMap<String, Object> params, MultipartFile file) {
|
||||
if (file == null || file.isEmpty() || file.getOriginalFilename() == null) {
|
||||
@ -86,10 +91,10 @@ public class UploadController {
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/minio-token")
|
||||
public JsonResponse minioToken(@RequestParam HashMap<String, Object> params) {
|
||||
@GetMapping("/minio-upload-id")
|
||||
public JsonResponse minioUploadId(@RequestParam HashMap<String, Object> params) {
|
||||
String extension = MapUtils.getString(params, "extension");
|
||||
if (extension == null || extension.isEmpty()) {
|
||||
if (extension == null || extension.trim().length() == 0) {
|
||||
return JsonResponse.error("extension参数为空");
|
||||
}
|
||||
String contentType = BackendConstant.RESOURCE_EXT_2_CONTENT_TYPE.get(extension.toLowerCase());
|
||||
@ -99,15 +104,41 @@ public class UploadController {
|
||||
String resourceType = BackendConstant.RESOURCE_EXT_2_TYPE.get(extension.toLowerCase());
|
||||
|
||||
try {
|
||||
String url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
|
||||
.bucket(minioConfig.getBucket())
|
||||
.object(HelperUtil.randomString(32) + "." + extension)
|
||||
.method(Method.PUT)
|
||||
.expiry(60 * 60 * 24)
|
||||
.build());
|
||||
String filename = HelperUtil.randomString(32) + "." + extension;
|
||||
String uploadId = playEduMinioClient.uploadId(minioConfig.getBucket(), filename);
|
||||
|
||||
HashMap<String, String> data = new HashMap<>();
|
||||
data.put("resource_type", resourceType);
|
||||
data.put("upload_id", uploadId);
|
||||
data.put("filename", filename);
|
||||
|
||||
return JsonResponse.data(data);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
return JsonResponse.error("系统错误");
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/minio-pre-sign-url")
|
||||
public JsonResponse minioPreSignUrl(@RequestParam HashMap<String, Object> params) {
|
||||
String uploadId = MapUtils.getString(params, "upload_id");
|
||||
Integer partNumber = MapUtils.getInteger(params, "part_number");
|
||||
String filename = MapUtils.getString(params, "filename");
|
||||
|
||||
try {
|
||||
Map<String, String> extraQueryParams = new HashMap<>();
|
||||
extraQueryParams.put("partNumber", partNumber + "");
|
||||
extraQueryParams.put("uploadId", uploadId);
|
||||
|
||||
String url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
|
||||
.bucket(minioConfig.getBucket())
|
||||
.object(filename)
|
||||
.method(Method.PUT)
|
||||
.expiry(60 * 60 * 24)
|
||||
.extraQueryParams(extraQueryParams)
|
||||
.build());
|
||||
|
||||
HashMap<String, String> data = new HashMap<>();
|
||||
data.put("url", url);
|
||||
|
||||
return JsonResponse.data(data);
|
||||
@ -117,4 +148,18 @@ public class UploadController {
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/minio-merge")
|
||||
public JsonResponse minioMerge(@RequestParam HashMap<String, Object> params) {
|
||||
String filename = MapUtils.getString(params, "filename");
|
||||
String uploadId = MapUtils.getString(params, "upload_id");
|
||||
if (filename == null || filename.trim().length() == 0) {
|
||||
return JsonResponse.error("filename必填");
|
||||
}
|
||||
if (uploadId == null || uploadId.trim().length() == 0) {
|
||||
return JsonResponse.error("uploadId必填");
|
||||
}
|
||||
playEduMinioClient.merge(minioConfig.getBucket(), filename, uploadId);
|
||||
return JsonResponse.success();
|
||||
}
|
||||
|
||||
}
|
||||
|
41
src/main/java/xyz/playedu/api/vendor/PlayEduMinioClient.java
vendored
Normal file
41
src/main/java/xyz/playedu/api/vendor/PlayEduMinioClient.java
vendored
Normal file
@ -0,0 +1,41 @@
|
||||
package xyz.playedu.api.vendor;
|
||||
|
||||
import io.minio.CreateMultipartUploadResponse;
|
||||
import io.minio.ListPartsResponse;
|
||||
import io.minio.MinioAsyncClient;
|
||||
import io.minio.messages.Part;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
* @create 2023/3/6 16:12
|
||||
*/
|
||||
@Slf4j
|
||||
public class PlayEduMinioClient extends MinioAsyncClient {
|
||||
public PlayEduMinioClient(MinioAsyncClient client) {
|
||||
super(client);
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public String uploadId(String bucket, String filename) {
|
||||
CreateMultipartUploadResponse response = super.createMultipartUpload(bucket, null, filename, null, null);
|
||||
return response.result().uploadId();
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public void merge(String bucketName, String objectName, String uploadId) {
|
||||
ListPartsResponse listPartsResponse = super.listParts(bucketName, null, objectName, 10000, 0, uploadId, null, null);
|
||||
List<Part> partList = listPartsResponse.result().partList();
|
||||
Part[] parts = new Part[10000];
|
||||
int partNumber = 1;
|
||||
for (Part part : partList) {
|
||||
log.info("partList {}", part.partSize());
|
||||
parts[partNumber - 1] = new Part(partNumber, part.etag());
|
||||
partNumber++;
|
||||
}
|
||||
super.completeMultipartUpload(bucketName, null, objectName, uploadId, parts, null, null);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user