add(req-2080):断点续传--创建临时文件夹、断点续传参数添加到nacos配置
This commit is contained in:
parent
8c4e29dbff
commit
be908387fb
@ -7,6 +7,7 @@ import cn.axzo.framework.auth.domain.ContextInfoHolder;
|
|||||||
import cn.axzo.oss.client.vo.*;
|
import cn.axzo.oss.client.vo.*;
|
||||||
import cn.axzo.oss.common.enums.ChannelTypeEnum;
|
import cn.axzo.oss.common.enums.ChannelTypeEnum;
|
||||||
import cn.axzo.oss.common.enums.FileUploadTypeEnum;
|
import cn.axzo.oss.common.enums.FileUploadTypeEnum;
|
||||||
|
import cn.axzo.oss.common.enums.StorageUnitEnum;
|
||||||
import cn.axzo.oss.common.exception.BizException;
|
import cn.axzo.oss.common.exception.BizException;
|
||||||
import cn.axzo.oss.common.utils.BeanConvertUtil;
|
import cn.axzo.oss.common.utils.BeanConvertUtil;
|
||||||
import cn.axzo.oss.manager.api.dto.request.*;
|
import cn.axzo.oss.manager.api.dto.request.*;
|
||||||
@ -204,14 +205,22 @@ public class WebFileController {
|
|||||||
liteSaasContext = contextInfo.lite();
|
liteSaasContext = contextInfo.lite();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 文件大于100k,生成临时文件,进行断点续传
|
||||||
|
int checkPointLengthLimit = StorageUnitEnum.KB.getSize() * 100;
|
||||||
|
byte[] fileContent = file.getBytes();
|
||||||
|
int fileLength = fileContent.length;
|
||||||
|
String filePath = null;
|
||||||
|
if (fileLength > checkPointLengthLimit) {
|
||||||
|
filePath = fileService.getFilePath(file);
|
||||||
|
}
|
||||||
|
|
||||||
String filename = file.getOriginalFilename();
|
String filename = file.getOriginalFilename();
|
||||||
String filePath = fileService.getFilePath(file);
|
|
||||||
BizException.error(filename.length() < FILE_NAME_MAX_LENGTH, FILE_NAME_TOO_LONG);
|
BizException.error(filename.length() < FILE_NAME_MAX_LENGTH, FILE_NAME_TOO_LONG);
|
||||||
ServerFileUploadDto fileUploadDto = ServerFileUploadDto.builder()
|
ServerFileUploadDto fileUploadDto = ServerFileUploadDto.builder()
|
||||||
.appCode(appCode)
|
.appCode(appCode)
|
||||||
.bizScene(bizScene)
|
.bizScene(bizScene)
|
||||||
.fileName(filename)
|
.fileName(filename)
|
||||||
.fileContent(file.getBytes())
|
.fileContent(fileContent)
|
||||||
.filePath(filePath)
|
.filePath(filePath)
|
||||||
.build();
|
.build();
|
||||||
ServerFileUploadResponse response = fileService.uploadObs(fileUploadDto, liteSaasContext,
|
ServerFileUploadResponse response = fileService.uploadObs(fileUploadDto, liteSaasContext,
|
||||||
|
|||||||
@ -15,12 +15,45 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
@RefreshScope
|
@RefreshScope
|
||||||
@Data
|
@Data
|
||||||
public class HuaWeiCloudObsConfig {
|
public class HuaWeiCloudObsConfig {
|
||||||
|
/**
|
||||||
|
* AK
|
||||||
|
*/
|
||||||
@Value("${huaweicloud.obs.accessKeyId}")
|
@Value("${huaweicloud.obs.accessKeyId}")
|
||||||
private String accessKeyId;
|
private String accessKeyId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SK
|
||||||
|
*/
|
||||||
@Value("${huaweicloud.obs.secretAccessKey}")
|
@Value("${huaweicloud.obs.secretAccessKey}")
|
||||||
private String secretAccessKey;
|
private String secretAccessKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 终端节点
|
||||||
|
*/
|
||||||
@Value("${huaweicloud.obs.endpoint}")
|
@Value("${huaweicloud.obs.endpoint}")
|
||||||
private String endPoint;
|
private String endPoint;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分段上传时的最大并发数
|
||||||
|
*/
|
||||||
|
@Value("${huaweicloud.checkpoint.task-num}")
|
||||||
|
private String taskNum;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分段大小
|
||||||
|
*/
|
||||||
|
@Value("${huaweicloud.checkpoint.part-size}")
|
||||||
|
private String partSize;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否开启断点续传模式
|
||||||
|
*/
|
||||||
|
@Value("${huaweicloud.checkpoint.enable-check-point}")
|
||||||
|
private String enableCheckPoint;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否开启请求头域自动编码
|
||||||
|
*/
|
||||||
|
@Value("${huaweicloud.checkpoint.encode-headers}")
|
||||||
|
private String encodeHeaders;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,9 @@ package cn.axzo.oss.integration.s3.impl;
|
|||||||
|
|
||||||
import cn.axzo.oss.integration.s3.HuaWeiCloudService;
|
import cn.axzo.oss.integration.s3.HuaWeiCloudService;
|
||||||
import cn.axzo.oss.integration.s3.client.HuaWeiCloudObsClient;
|
import cn.axzo.oss.integration.s3.client.HuaWeiCloudObsClient;
|
||||||
|
import cn.axzo.oss.integration.s3.config.HuaWeiCloudObsConfig;
|
||||||
import cn.azxo.framework.common.utils.LogUtil;
|
import cn.azxo.framework.common.utils.LogUtil;
|
||||||
|
import cn.hutool.core.io.FileUtil;
|
||||||
import com.obs.services.ObsClient;
|
import com.obs.services.ObsClient;
|
||||||
import com.obs.services.exception.ObsException;
|
import com.obs.services.exception.ObsException;
|
||||||
import com.obs.services.model.*;
|
import com.obs.services.model.*;
|
||||||
@ -10,7 +12,6 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -30,6 +31,9 @@ public class HuaWeiCloudServiceImpl implements HuaWeiCloudService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private HuaWeiCloudObsClient huaWeiCloudObsClient;
|
private HuaWeiCloudObsClient huaWeiCloudObsClient;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private HuaWeiCloudObsConfig huaWeiCloudObsConfig;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上传文件到OBS:普通方式
|
* 上传文件到OBS:普通方式
|
||||||
*
|
*
|
||||||
@ -93,7 +97,8 @@ public class HuaWeiCloudServiceImpl implements HuaWeiCloudService {
|
|||||||
// 断点续传
|
// 断点续传
|
||||||
CompleteMultipartUploadResult completeMultipartUploadResult =
|
CompleteMultipartUploadResult completeMultipartUploadResult =
|
||||||
this.uploadFile(obsClient, bucketName, objectKey, fileName, null, filePath);
|
this.uploadFile(obsClient, bucketName, objectKey, fileName, null, filePath);
|
||||||
this.deleteFile(filePath);
|
boolean delFlag = FileUtil.del(filePath);
|
||||||
|
log.info("断点续传成功,删除临时文件:{}", delFlag);
|
||||||
return completeMultipartUploadResult.getObjectUrl();
|
return completeMultipartUploadResult.getObjectUrl();
|
||||||
} else {
|
} else {
|
||||||
// 创建上传文件的元信息,通过文件云信息设置HTTP Header
|
// 创建上传文件的元信息,通过文件云信息设置HTTP Header
|
||||||
@ -103,16 +108,15 @@ public class HuaWeiCloudServiceImpl implements HuaWeiCloudService {
|
|||||||
metadata.setContentEncoding("utf-8");
|
metadata.setContentEncoding("utf-8");
|
||||||
CompleteMultipartUploadResult completeMultipartUploadResult =
|
CompleteMultipartUploadResult completeMultipartUploadResult =
|
||||||
this.uploadFile(obsClient, bucketName, objectKey, fileName, metadata, filePath);
|
this.uploadFile(obsClient, bucketName, objectKey, fileName, metadata, filePath);
|
||||||
this.deleteFile(filePath);
|
boolean delFlag = FileUtil.del(filePath);
|
||||||
|
log.info("断点续传成功,删除临时文件:{}", delFlag);
|
||||||
return completeMultipartUploadResult.getObjectUrl();
|
return completeMultipartUploadResult.getObjectUrl();
|
||||||
}
|
}
|
||||||
} catch (ObsException exception) {
|
} catch (ObsException exception) {
|
||||||
LogUtil.error("uploadByStream ObsException", exception);
|
LogUtil.error("uploadByStream ObsException", exception);
|
||||||
this.deleteFile(filePath);
|
|
||||||
return "";
|
return "";
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LogUtil.error("uploadByStream ClientException", e);
|
LogUtil.error("uploadByStream ClientException", e);
|
||||||
this.deleteFile(filePath);
|
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -207,24 +211,15 @@ public class HuaWeiCloudServiceImpl implements HuaWeiCloudService {
|
|||||||
// 设置待上传的本地文件,fileName为待上传的本地文件路径,需要指定到具体带文件后缀的文件名
|
// 设置待上传的本地文件,fileName为待上传的本地文件路径,需要指定到具体带文件后缀的文件名
|
||||||
request.setUploadFile(filePath);
|
request.setUploadFile(filePath);
|
||||||
// 设置分段上传时的最大并发数
|
// 设置分段上传时的最大并发数
|
||||||
request.setTaskNum(5);
|
request.setTaskNum(Integer.parseInt(huaWeiCloudObsConfig.getTaskNum()));
|
||||||
// 设置分段大小为10MB
|
// 设置分段大小为10MB
|
||||||
request.setPartSize(10 * 1024 * 1024);
|
request.setPartSize(Long.parseLong(huaWeiCloudObsConfig.getPartSize()));
|
||||||
// 开启断点续传模式
|
// 开启断点续传模式
|
||||||
request.setEnableCheckpoint(true);
|
request.setEnableCheckpoint(Boolean.parseBoolean(huaWeiCloudObsConfig.getEnableCheckPoint()));
|
||||||
// 开启请求头域自动编码
|
// 开启请求头域自动编码
|
||||||
request.setIsEncodeHeaders(true);
|
request.setIsEncodeHeaders(Boolean.parseBoolean(huaWeiCloudObsConfig.getEncodeHeaders()));
|
||||||
// 对象元数据
|
// 对象元数据
|
||||||
request.setObjectMetadata(metadata);
|
request.setObjectMetadata(metadata);
|
||||||
return obsClient.uploadFile(request);
|
return obsClient.uploadFile(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void deleteFile(String filePath) {
|
|
||||||
// 上传成功后删除target目录下的文件
|
|
||||||
File file = new File(filePath);
|
|
||||||
if (file.exists()) {
|
|
||||||
boolean delete = file.delete();
|
|
||||||
log.info("删除临时文件:{}", delete);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -31,10 +31,11 @@ import org.springframework.core.env.Environment;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@ -60,6 +61,8 @@ public class FileServiceImpl implements FileService {
|
|||||||
|
|
||||||
private static final String HTTP_COM = ".com/";
|
private static final String HTTP_COM = ".com/";
|
||||||
|
|
||||||
|
private static final String SEPARATOR = java.io.File.separator;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private FileManager fileManager;
|
private FileManager fileManager;
|
||||||
|
|
||||||
@ -641,10 +644,24 @@ public class FileServiceImpl implements FileService {
|
|||||||
// 指定目标文件路径及文件名
|
// 指定目标文件路径及文件名
|
||||||
String rootDir = System.getProperty("user.dir");
|
String rootDir = System.getProperty("user.dir");
|
||||||
String fileName = file.getOriginalFilename();
|
String fileName = file.getOriginalFilename();
|
||||||
String filePath = rootDir + "/" + fileName;
|
String filePath = rootDir + SEPARATOR + "temp_directory" + SEPARATOR;
|
||||||
|
String absolutePath = filePath + fileName;
|
||||||
|
|
||||||
|
// 创建临时文件夹
|
||||||
|
java.io.File tempDirectory = new java.io.File(filePath);
|
||||||
|
if (!tempDirectory.exists()) {
|
||||||
|
boolean flag = tempDirectory.mkdirs();
|
||||||
|
if (flag) {
|
||||||
|
log.info("临时文件夹创建成功,{}", flag);
|
||||||
|
} else {
|
||||||
|
log.info("临时文件夹创建失败,{}", flag);
|
||||||
|
// 兜底
|
||||||
|
absolutePath = rootDir + SEPARATOR + fileName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 创建输出流并写入数据
|
// 创建输出流并写入数据
|
||||||
OutputStream output = new FileOutputStream(filePath);
|
OutputStream output = Files.newOutputStream(Paths.get(absolutePath));
|
||||||
byte[] buffer = new byte[1024];
|
byte[] buffer = new byte[1024];
|
||||||
int length;
|
int length;
|
||||||
while ((length = input.read(buffer)) > 0) {
|
while ((length = input.read(buffer)) > 0) {
|
||||||
@ -655,7 +672,7 @@ public class FileServiceImpl implements FileService {
|
|||||||
input.close();
|
input.close();
|
||||||
output.close();
|
output.close();
|
||||||
|
|
||||||
return filePath;
|
return absolutePath;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user