mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 15:55:18 +08:00
阿里云oss
This commit is contained in:
@@ -44,8 +44,7 @@ public class FileService extends BaseService<FileEntity, FileRepository> {
|
||||
@Autowired ObsClient obsClient;
|
||||
@Autowired HwCloudProperty hwCloudProperty;
|
||||
|
||||
@Autowired
|
||||
AliyunProperty aliyunProperty;
|
||||
@Autowired AliyunProperty aliyunProperty;
|
||||
|
||||
@Autowired OSSClient ossClient;
|
||||
|
||||
@@ -94,11 +93,17 @@ public class FileService extends BaseService<FileEntity, FileRepository> {
|
||||
|
||||
return page.map(this.mapper::toDto);
|
||||
}
|
||||
@Autowired
|
||||
ObjectMapper objectMapper;
|
||||
|
||||
@Autowired ObjectMapper objectMapper;
|
||||
|
||||
@SneakyThrows
|
||||
public FileDto upload(MultipartFile file, String name, String dir, String sortKey, String code, String remarks) {
|
||||
public FileDto upload(
|
||||
MultipartFile file,
|
||||
String name,
|
||||
String dir,
|
||||
String sortKey,
|
||||
String code,
|
||||
String remarks) {
|
||||
FileEntity fileEntity = new FileEntity();
|
||||
fileEntity.setFileName(name);
|
||||
fileEntity.setFileSize(file.getSize());
|
||||
@@ -118,38 +123,54 @@ public class FileService extends BaseService<FileEntity, FileRepository> {
|
||||
Ctx.currentUser().getOrganizationId(),
|
||||
StringUtils.defaultIfBlank(dir, "public")));
|
||||
InputStream inputStream = file.getInputStream();
|
||||
fileEntity.setObjectId(UUID.randomUUID() + "." + FilenameUtils.getExtension(name.replace(":", "_")));
|
||||
fileEntity.setObjectId(
|
||||
UUID.randomUUID() + "." + FilenameUtils.getExtension(name.replace(":", "_")));
|
||||
try (inputStream) {
|
||||
fileEntity.setMimeType(tika.detect(inputStream));
|
||||
}
|
||||
|
||||
String objectKey = fileEntity.getDirectory() + "/" + fileEntity.getObjectId();
|
||||
String objectUrl = "";
|
||||
// PutObjectResult putObjectResult =
|
||||
// obsClient.putObject(
|
||||
// hwCloudProperty.getObs().getBucketName(),
|
||||
// objectKey,
|
||||
// file.getInputStream());
|
||||
//
|
||||
// if (putObjectResult.getStatusCode() != 200) {
|
||||
// throw new BizException("上传文件失败 %s".formatted(putObjectResult.toString()));
|
||||
// }
|
||||
// PutObjectResult putObjectResult =
|
||||
// obsClient.putObject(
|
||||
// hwCloudProperty.getObs().getBucketName(),
|
||||
// objectKey,
|
||||
// file.getInputStream());
|
||||
//
|
||||
// if (putObjectResult.getStatusCode() != 200) {
|
||||
// throw new BizException("上传文件失败 %s".formatted(putObjectResult.toString()));
|
||||
// }
|
||||
|
||||
// objectUrl = putObjectResult.getObjectUrl();
|
||||
// objectUrl = putObjectResult.getObjectUrl();
|
||||
try {
|
||||
|
||||
com.aliyun.oss.model.PutObjectResult objectResult = ossClient.putObject(aliyunProperty.getOSS().getBucketName(), objectKey, file.getInputStream());
|
||||
com.aliyun.oss.model.PutObjectResult objectResult =
|
||||
ossClient.putObject(
|
||||
aliyunProperty.getOSS().getBucketName(),
|
||||
objectKey,
|
||||
file.getInputStream());
|
||||
} catch (Exception e) {
|
||||
|
||||
if (!objectResult.getResponse().isSuccessful()){
|
||||
log.warn("上传文件失败 {}", objectMapper.writeValueAsString(objectResult));
|
||||
throw new BizException("上传文件失败 %s".formatted(objectResult.getResponse().getErrorResponseAsString()));
|
||||
|
||||
}else {
|
||||
AliyunProperty.OSSRegion ossRegion = aliyunProperty.getOSS().getRegions().stream().filter(x -> x.getCode().equalsIgnoreCase(aliyunProperty.getOSS().getRegion())).findFirst().orElseThrow(() -> new RuntimeException("region not found"));
|
||||
|
||||
objectUrl = "https://%s.%s/%s".formatted(aliyunProperty.getOSS().getBucketName(), ossRegion.getEndpoint(), objectKey);
|
||||
log.warn("上传文件失败", e);
|
||||
throw new BizException("上传文件失败 %s".formatted(e.getMessage()));
|
||||
}
|
||||
|
||||
AliyunProperty.OSSRegion ossRegion =
|
||||
aliyunProperty.getOSS().getRegions().stream()
|
||||
.filter(
|
||||
x ->
|
||||
x.getCode()
|
||||
.equalsIgnoreCase(
|
||||
aliyunProperty.getOSS().getRegion()))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new RuntimeException("region not found"));
|
||||
|
||||
objectUrl =
|
||||
"https://%s.%s/%s"
|
||||
.formatted(
|
||||
aliyunProperty.getOSS().getBucketName(),
|
||||
ossRegion.getEndpoint(),
|
||||
objectKey);
|
||||
|
||||
fileEntity.setPublicUrl(objectUrl);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user