新增大文件分段上传
parent
bf7fa5accc
commit
703664701b
|
@ -11,6 +11,7 @@ import io.swagger.annotations.Api;
|
|||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
|
@ -43,6 +44,10 @@ public class FileController {
|
|||
@Autowired
|
||||
private ObsClient obsClient;
|
||||
|
||||
@Value("${huawei.obs.bucketName}")
|
||||
private String bucketName;
|
||||
|
||||
|
||||
/***
|
||||
*
|
||||
* 图片
|
||||
|
@ -141,37 +146,40 @@ public class FileController {
|
|||
/**
|
||||
* 启动上传任务
|
||||
*/
|
||||
@GetMapping("/getUploadId/{objectKey}")
|
||||
public String getUploadId(@PathVariable("objectKey")String objectKey) {
|
||||
InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest("<桶名称>", objectKey);
|
||||
@ApiOperation(value = "启动上传任务")
|
||||
@GetMapping("/getUploadId")
|
||||
public R getUploadId(@RequestParam("objectKey")String objectKey) {
|
||||
InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, objectKey);
|
||||
ObjectMetadata metadata = new ObjectMetadata();
|
||||
metadata.addUserMetadata("property", "property-value");
|
||||
metadata.setContentType("text/plain");
|
||||
request.setMetadata(metadata);
|
||||
InitiateMultipartUploadResult result = obsClient.initiateMultipartUpload(request);
|
||||
String uploadId = result.getUploadId();
|
||||
return uploadId;
|
||||
return R.ok(uploadId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分片上传
|
||||
*/
|
||||
@ApiOperation(value = "分片上传")
|
||||
@PostMapping("/chunk")
|
||||
public Map<String,String> splitFileUpload(
|
||||
public R splitFileUpload(
|
||||
@RequestParam("objectKey")String objectKey,
|
||||
@RequestParam("file") MultipartFile file,
|
||||
@RequestParam("chunk") int chunk,
|
||||
@RequestParam("uploadId") String uploadId) throws Exception {
|
||||
File file1 = multipartFileToFile(file);
|
||||
Map<String,String> map = uploadChunk(uploadId, file1, chunk, objectKey);
|
||||
return map;
|
||||
return R.ok(map);
|
||||
}
|
||||
|
||||
/**
|
||||
* 合并上传
|
||||
*/
|
||||
@ApiOperation(value = "合并上传")
|
||||
@PostMapping("/completeUpload")
|
||||
public CompleteMultipartUploadResult completeUpload(
|
||||
public R completeUpload(
|
||||
@RequestParam("objectKey")String objectKey,
|
||||
@RequestParam("uploadId") String uploadId,
|
||||
@RequestBody List<Map<String,String>> mapList
|
||||
|
@ -184,27 +192,31 @@ public class FileController {
|
|||
partEtags.add(part1);
|
||||
}
|
||||
CompleteMultipartUploadRequest request = new CompleteMultipartUploadRequest(
|
||||
"<桶名称>", objectKey, uploadId, partEtags);
|
||||
bucketName, objectKey, uploadId, partEtags);
|
||||
CompleteMultipartUploadResult result = obsClient.completeMultipartUpload(request);
|
||||
return result;
|
||||
|
||||
return R.ok(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消任务上传
|
||||
*/
|
||||
@ApiOperation(value = "取消任务上传")
|
||||
@GetMapping("/cancelUpload")
|
||||
public void cancelUpload(
|
||||
public R cancelUpload(
|
||||
@RequestParam("objectKey")String objectKey,
|
||||
@RequestParam("uploadId") String uploadId
|
||||
){
|
||||
AbortMultipartUploadRequest request = new AbortMultipartUploadRequest("<桶名称>", objectKey, uploadId);
|
||||
AbortMultipartUploadRequest request = new AbortMultipartUploadRequest(bucketName, objectKey, uploadId);
|
||||
obsClient.abortMultipartUpload(request);
|
||||
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
public Map<String,String> uploadChunk(String uploadId, File file,int chunk, String objectKey){
|
||||
// Endpoint以北京四为例,其他地区请按实际情况填写。
|
||||
Map<String,String> map = new HashMap<>();
|
||||
UploadPartRequest request = new UploadPartRequest("<桶名称>", objectKey);
|
||||
UploadPartRequest request = new UploadPartRequest(bucketName, objectKey);
|
||||
request.setUploadId(uploadId);
|
||||
request.setPartNumber(chunk);
|
||||
request.setFile(file);
|
||||
|
@ -263,4 +275,41 @@ public class FileController {
|
|||
return fileService.selectHash(hashCode,type);
|
||||
}
|
||||
|
||||
// @PostMapping("/test")
|
||||
// public R test(@RequestParam File file){
|
||||
//
|
||||
// // 创建上传请求
|
||||
// ObjectMetadata metadata = new ObjectMetadata();
|
||||
// metadata.setContentLength(file.length());
|
||||
// InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest(bucketName, file.getName(), metadata);
|
||||
// InitiateMultipartUploadResult initiateResult = obsClient.initiateMultipartUpload(initiateRequest);
|
||||
// String uploadId = initiateResult.getUploadId();
|
||||
//
|
||||
// // 分片上传
|
||||
// long partSize = 5 * 1024 * 1024; // 分片大小为5MB
|
||||
// long filePosition = 0;
|
||||
// int partNumber = 1;
|
||||
// while (filePosition < file.length()) {
|
||||
// long currentPartSize = Math.min(partSize, file.length() - filePosition);
|
||||
// UploadPartRequest uploadRequest = new UploadPartRequest();
|
||||
// uploadRequest.setBucketName(bucketName);
|
||||
// uploadRequest.setObjectKey(file.getName());
|
||||
// uploadRequest.setUploadId(uploadId);
|
||||
// uploadRequest.setFile(file);
|
||||
// uploadRequest.setPartSize(currentPartSize);
|
||||
// uploadRequest.setPartNumber(partNumber);
|
||||
// uploadRequest.setPosition(filePosition);
|
||||
// UploadPartResult uploadResult = obsClient.uploadPart(uploadRequest);
|
||||
// PartEtag partEtag = new PartEtag(uploadResult.getEtag(), uploadResult.getPartNumber());
|
||||
// partNumber++;
|
||||
// filePosition += currentPartSize;
|
||||
// }
|
||||
//
|
||||
// // 完成分片上传
|
||||
// CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest(bucketName, file.getName(), uploadId);
|
||||
// obsClient.completeMultipartUpload(completeRequest);
|
||||
// String key = completeRequest.getObjectKey();
|
||||
// return R.ok(key);
|
||||
// }
|
||||
|
||||
}
|
||||
|
|
|
@ -395,7 +395,7 @@
|
|||
<version>5.3.25</version> <!-- 请确保与 spring-web 使用相同的主要版本 -->
|
||||
</dependency>
|
||||
|
||||
<!-- obs文件存储 3.23.5 -->
|
||||
<!-- obs文件存储 3.23.5 3.19.7 -->
|
||||
<dependency>
|
||||
<groupId>com.huaweicloud</groupId>
|
||||
<artifactId>esdk-obs-java</artifactId>
|
||||
|
@ -408,6 +408,12 @@
|
|||
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</dependencies>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>huawei-public</id>
|
||||
<url>https://repo.huaweicloud.com/repository/maven/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
</project>
|
||||
|
|
|
@ -67,7 +67,7 @@ public class ObsUtils {
|
|||
LocalDateTime now = LocalDateTime.now();
|
||||
String url =
|
||||
now.getYear() + "/" +
|
||||
now.getMonth() + "/" +
|
||||
now.getMonthValue() + "/" +
|
||||
now.getDayOfMonth() + "/"
|
||||
+ uuid + "/"
|
||||
+ name;
|
||||
|
|
Loading…
Reference in New Issue