mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-07-26 11:59:31 +08:00
minio分片上传
This commit is contained in:
parent
32f6966706
commit
0db709cb3e
@ -1,10 +1,12 @@
|
|||||||
package xyz.playedu.api.config;
|
package xyz.playedu.api.config;
|
||||||
|
|
||||||
|
import io.minio.MinioAsyncClient;
|
||||||
import io.minio.MinioClient;
|
import io.minio.MinioClient;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import xyz.playedu.api.vendor.PlayEduMinioClient;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author 杭州白书科技有限公司
|
* @Author 杭州白书科技有限公司
|
||||||
@ -36,4 +38,10 @@ public class MinioConfig {
|
|||||||
.credentials(this.accessKey, this.secretKey)
|
.credentials(this.accessKey, this.secretKey)
|
||||||
.build();
|
.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;
|
package xyz.playedu.api.controller.backend;
|
||||||
|
|
||||||
|
import com.google.common.collect.Multimap;
|
||||||
import io.minio.*;
|
import io.minio.*;
|
||||||
import io.minio.http.Method;
|
import io.minio.http.Method;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.collections4.MapUtils;
|
import org.apache.commons.collections4.MapUtils;
|
||||||
|
import org.checkerframework.checker.units.qual.A;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
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.service.ResourceService;
|
||||||
import xyz.playedu.api.types.JsonResponse;
|
import xyz.playedu.api.types.JsonResponse;
|
||||||
import xyz.playedu.api.util.HelperUtil;
|
import xyz.playedu.api.util.HelperUtil;
|
||||||
|
import xyz.playedu.api.vendor.PlayEduMinioClient;
|
||||||
|
|
||||||
import java.time.ZonedDateTime;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -41,6 +43,9 @@ public class UploadController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private MinioClient minioClient;
|
private MinioClient minioClient;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private PlayEduMinioClient playEduMinioClient;
|
||||||
|
|
||||||
@PostMapping("/image")
|
@PostMapping("/image")
|
||||||
public JsonResponse image(@RequestParam HashMap<String, Object> params, MultipartFile file) {
|
public JsonResponse image(@RequestParam HashMap<String, Object> params, MultipartFile file) {
|
||||||
if (file == null || file.isEmpty() || file.getOriginalFilename() == null) {
|
if (file == null || file.isEmpty() || file.getOriginalFilename() == null) {
|
||||||
@ -86,10 +91,10 @@ public class UploadController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/minio-token")
|
@GetMapping("/minio-upload-id")
|
||||||
public JsonResponse minioToken(@RequestParam HashMap<String, Object> params) {
|
public JsonResponse minioUploadId(@RequestParam HashMap<String, Object> params) {
|
||||||
String extension = MapUtils.getString(params, "extension");
|
String extension = MapUtils.getString(params, "extension");
|
||||||
if (extension == null || extension.isEmpty()) {
|
if (extension == null || extension.trim().length() == 0) {
|
||||||
return JsonResponse.error("extension参数为空");
|
return JsonResponse.error("extension参数为空");
|
||||||
}
|
}
|
||||||
String contentType = BackendConstant.RESOURCE_EXT_2_CONTENT_TYPE.get(extension.toLowerCase());
|
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());
|
String resourceType = BackendConstant.RESOURCE_EXT_2_TYPE.get(extension.toLowerCase());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
|
String filename = HelperUtil.randomString(32) + "." + extension;
|
||||||
.bucket(minioConfig.getBucket())
|
String uploadId = playEduMinioClient.uploadId(minioConfig.getBucket(), filename);
|
||||||
.object(HelperUtil.randomString(32) + "." + extension)
|
|
||||||
.method(Method.PUT)
|
|
||||||
.expiry(60 * 60 * 24)
|
|
||||||
.build());
|
|
||||||
|
|
||||||
HashMap<String, String> data = new HashMap<>();
|
HashMap<String, String> data = new HashMap<>();
|
||||||
data.put("resource_type", resourceType);
|
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);
|
data.put("url", url);
|
||||||
|
|
||||||
return JsonResponse.data(data);
|
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