华为云obs;
This commit is contained in:
@@ -14,9 +14,7 @@ public class FileController {
|
||||
@PostMapping("/upload")
|
||||
public AjaxResult<String> uploadFile(@RequestParam("file") MultipartFile file) {
|
||||
String fileKey = fileStorageService.uploadFile(file);
|
||||
String fileUrl = fileStorageService.getFileUrl(fileKey);
|
||||
|
||||
return AjaxResult.success(fileUrl);
|
||||
return AjaxResult.success(fileKey);
|
||||
}
|
||||
|
||||
@GetMapping("/url")
|
||||
|
||||
+22
-8
@@ -1,7 +1,9 @@
|
||||
package com.linke.product.infrastructure.obs;
|
||||
import com.mhd.common.core.utils.StringUtils;
|
||||
import com.obs.services.ObsClient;
|
||||
import com.obs.services.model.HttpMethodEnum;
|
||||
import com.obs.services.model.ObsObject;
|
||||
import com.obs.services.model.TemporarySignatureRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -22,6 +24,8 @@ public class HuaweiObsService implements FileStorageService {
|
||||
|
||||
private String bucketName;
|
||||
|
||||
private String folder = "mhd";
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
this.bucketName = obsProperties.getBucketName();
|
||||
@@ -31,8 +35,7 @@ public class HuaweiObsService implements FileStorageService {
|
||||
public String uploadFile(MultipartFile file) {
|
||||
String originalFilename = file.getOriginalFilename();
|
||||
String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
|
||||
String fileKey = UUID.randomUUID().toString() + fileExtension;
|
||||
|
||||
String fileKey = folder + "/" + UUID.randomUUID().toString() + fileExtension;
|
||||
return uploadFile(fileKey, file);
|
||||
}
|
||||
|
||||
@@ -52,12 +55,23 @@ public class HuaweiObsService implements FileStorageService {
|
||||
|
||||
@Override
|
||||
public String getFileUrl(String fileKey) {
|
||||
if (StringUtils.isNotBlank(obsProperties.getCdnDomain())) {
|
||||
// 使用CDN域名
|
||||
return "https://" + obsProperties.getCdnDomain() + "/" + fileKey;
|
||||
} else {
|
||||
// 生成临时访问URL(默认1小时有效期)
|
||||
return "";
|
||||
return generateTemporaryUrl(fileKey,60*60);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成指定有效期的临时URL
|
||||
*/
|
||||
public String generateTemporaryUrl(String objectKey, long expireSeconds) {
|
||||
try {
|
||||
TemporarySignatureRequest request = new TemporarySignatureRequest(
|
||||
HttpMethodEnum.GET,
|
||||
expireSeconds
|
||||
);
|
||||
request.setBucketName(bucketName);
|
||||
request.setObjectKey(objectKey);
|
||||
|
||||
return obsClient.createTemporarySignature(request).getSignedUrl();
|
||||
} finally {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user