优化文件OSS
This commit is contained in:
@@ -26,6 +26,7 @@ import java.util.Date;
|
|||||||
@Component("ossAbility")
|
@Component("ossAbility")
|
||||||
@ConditionalOnProperty(name = "app.oss.service_provider", havingValue = "aliyun")
|
@ConditionalOnProperty(name = "app.oss.service_provider", havingValue = "aliyun")
|
||||||
public class AliOssAbility {
|
public class AliOssAbility {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private DefaultCredentialProvider credentialsProvider;
|
private DefaultCredentialProvider credentialsProvider;
|
||||||
|
|
||||||
@@ -52,6 +53,7 @@ public class AliOssAbility {
|
|||||||
// putObjectRequest.setMetadata(metadata);
|
// putObjectRequest.setMetadata(metadata);
|
||||||
|
|
||||||
PutObjectResult result = ossClient.putObject(putObjectRequest);
|
PutObjectResult result = ossClient.putObject(putObjectRequest);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -83,6 +85,7 @@ public class AliOssAbility {
|
|||||||
inputStream.close();
|
inputStream.close();
|
||||||
ossObject.close();
|
ossObject.close();
|
||||||
return fileBytes;
|
return fileBytes;
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("aliOss 请求封装出现异常 异常信息:{}", e.getMessage());
|
log.error("aliOss 请求封装出现异常 异常信息:{}", e.getMessage());
|
||||||
} finally {
|
} finally {
|
||||||
@@ -108,8 +111,10 @@ public class AliOssAbility {
|
|||||||
try {
|
try {
|
||||||
// 封装生成签名url需要的信息
|
// 封装生成签名url需要的信息
|
||||||
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, objectPathName);
|
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, objectPathName);
|
||||||
|
|
||||||
// 过期时间 毫秒
|
// 过期时间 毫秒
|
||||||
generatePresignedUrlRequest.setExpiration(new Date(new Date().getTime() + second * 1000L));
|
generatePresignedUrlRequest.setExpiration(new Date(new Date().getTime() + second * 1000L));
|
||||||
|
|
||||||
// 设置请求方法
|
// 设置请求方法
|
||||||
generatePresignedUrlRequest.setMethod(httpMethod);
|
generatePresignedUrlRequest.setMethod(httpMethod);
|
||||||
|
|
||||||
|
|||||||
@@ -1,36 +1,36 @@
|
|||||||
package org.jiayunet.constant;
|
package org.jiayunet.constant;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Oss桶
|
* Oss桶
|
||||||
*/
|
*/
|
||||||
@Getter
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
public enum OssBucketEnum {
|
public enum OssBucketEnum {
|
||||||
|
|
||||||
|
|
||||||
HuiDongNi("oss-cn-guangzhou.aliyuncs.com", "jiayupet", 1,"https://jiayupet.oss-cn-guangzhou.aliyuncs.com");
|
HuiDongNi("oss-cn-guangzhou.aliyuncs.com", "jiayupet", 1,"https://jiayupet.oss-cn-guangzhou.aliyuncs.com");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 服务端口
|
* 服务端口
|
||||||
*/
|
*/
|
||||||
private final String endpoint;
|
private final String endpoint;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 桶名
|
* 桶名
|
||||||
*/
|
*/
|
||||||
private final String bucketName;
|
private final String bucketName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 权限 0 私有 1公共读 2 公共读写
|
* 权限 0 私有 1公共读 2 公共读写
|
||||||
*/
|
*/
|
||||||
private final int power;
|
private final int power;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* 域名
|
||||||
*/
|
*/
|
||||||
private final String domain;
|
private final String domain;
|
||||||
|
|
||||||
OssBucketEnum(String endpoint, String bucketName, int power, String domain) {
|
|
||||||
this.endpoint = endpoint;
|
|
||||||
this.bucketName = bucketName;
|
|
||||||
this.power = power;
|
|
||||||
this.domain = domain;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package org.jiayunet.constant;
|
package org.jiayunet.constant;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
@@ -9,27 +10,31 @@ import java.util.UUID;
|
|||||||
/**
|
/**
|
||||||
* oss 文件路径
|
* oss 文件路径
|
||||||
*/
|
*/
|
||||||
|
@AllArgsConstructor
|
||||||
@Getter
|
@Getter
|
||||||
public enum OssPathEnum {
|
public enum OssPathEnum {
|
||||||
UserImage("image/user", "用户图片"),
|
|
||||||
ResumeImage("image/resume", "简历图片"),
|
|
||||||
SystemImage("image/system", "系统图片"),
|
|
||||||
ResumeFile("file/resume", "简历文件"),
|
|
||||||
|
|
||||||
;
|
UserImage("image/user", "用户图片",OssBucketEnum.HuiDongNi),
|
||||||
|
ResumeImage("image/resume", "简历图片",OssBucketEnum.HuiDongNi),
|
||||||
|
SystemImage("image/system", "系统图片",OssBucketEnum.HuiDongNi),
|
||||||
|
ResumeFile("file/resume", "简历文件",OssBucketEnum.HuiDongNi),
|
||||||
|
OtherFile("other","其他文件",OssBucketEnum.HuiDongNi);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件存放目录
|
* 文件存放目录
|
||||||
*/
|
*/
|
||||||
private final String catalogue;
|
private final String catalogue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 备注
|
* 备注
|
||||||
*/
|
*/
|
||||||
private final String comment;
|
private final String comment;
|
||||||
|
|
||||||
OssPathEnum(String catalogue, String comment) {
|
/**
|
||||||
this.catalogue = catalogue;
|
* 文件桶
|
||||||
this.comment = comment;
|
*/
|
||||||
}
|
private final OssBucketEnum ossBucketEnum;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取文件路径
|
* 获取文件路径
|
||||||
|
|||||||
@@ -4,13 +4,17 @@ package org.jiayunet.controller;
|
|||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
import org.jiayunet.constant.OssPathEnum;
|
import org.jiayunet.constant.OssPathEnum;
|
||||||
import org.jiayunet.pojo.vo.OssUrlVo;
|
import org.jiayunet.pojo.vo.OssUrlVo;
|
||||||
import org.jiayunet.server.OssServer;
|
import org.jiayunet.server.OssServer;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* oss控制类
|
* oss控制类
|
||||||
*/
|
*/
|
||||||
@@ -21,10 +25,33 @@ import org.jiayunet.server.OssServer;
|
|||||||
public class OssController {
|
public class OssController {
|
||||||
private OssServer ossServer;
|
private OssServer ossServer;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取授权上传地址
|
||||||
|
* @param fileName 文件名
|
||||||
|
* @param pathEnum 文件地址枚举
|
||||||
|
* @return 地址相关
|
||||||
|
*/
|
||||||
@GetMapping("/requestUploadUrl")
|
@GetMapping("/requestUploadUrl")
|
||||||
public OssUrlVo requestUploadUrl(@RequestParam String fileName, @RequestParam OssPathEnum pathEnum){
|
public OssUrlVo requestUploadUrl(@RequestParam String fileName, @RequestParam OssPathEnum pathEnum){
|
||||||
return ossServer.requestUploadUrl(fileName,pathEnum);
|
return ossServer.requestUploadUrl(fileName,pathEnum);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 简单上传
|
||||||
|
* @param file 文件
|
||||||
|
* @param pathEnum 地址枚举
|
||||||
|
* @return 下载地址
|
||||||
|
*/
|
||||||
|
@PostMapping("/simpleUpload")
|
||||||
|
public OssUrlVo simpleUpload(@RequestParam("file") MultipartFile file, @RequestParam OssPathEnum pathEnum) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
return ossServer.simpleUpload(file.getOriginalFilename(), pathEnum, file.getInputStream());
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,27 +40,31 @@ public class OssServer {
|
|||||||
*/
|
*/
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public OssUrlVo requestUploadUrl(String fileName, OssPathEnum pathEnum) {
|
public OssUrlVo requestUploadUrl(String fileName, OssPathEnum pathEnum) {
|
||||||
|
|
||||||
Assert.notNull(pathEnum, " 上传路径不能为空");
|
Assert.notNull(pathEnum, " 上传路径不能为空");
|
||||||
Assert.hasText(fileName, "文件不能为空");
|
Assert.hasText(fileName, "文件不能为空");
|
||||||
Assert.isTrue(fileName.lastIndexOf(".") >= 0, "文件名必须包含类型");
|
Assert.isTrue(fileName.lastIndexOf(".") >= 0, "文件名必须包含类型");
|
||||||
|
|
||||||
OssBucketEnum jiaYuPet = OssBucketEnum.HuiDongNi;
|
OssBucketEnum bucketEnum = pathEnum.getOssBucketEnum();
|
||||||
|
|
||||||
Long userId = UserSecurityTool.getUserId();
|
Long userId = UserSecurityTool.getUserId();
|
||||||
String type = fileName.substring(fileName.lastIndexOf(".") + 1);
|
String type = fileName.substring(fileName.lastIndexOf(".") + 1);
|
||||||
|
|
||||||
String path = pathEnum.getPath(fileName);
|
String path = pathEnum.getPath(fileName);
|
||||||
|
|
||||||
|
|
||||||
OssUrlVo ossUrlVo = new OssUrlVo();
|
OssUrlVo ossUrlVo = new OssUrlVo();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
//保存上传信息
|
//保存上传信息
|
||||||
ossFileMapper.insert(new OssFile(null, jiaYuPet.getBucketName(), type, path, fileName, userId, 0, Instant.now(), null));
|
ossFileMapper.insert(new OssFile(null, bucketEnum.getBucketName(), type, path, fileName, userId, 0, Instant.now(), null));
|
||||||
|
|
||||||
//获取上传Url
|
//获取上传Url
|
||||||
String uploadUrl = aliOssAbility.signatureUrl(jiaYuPet.getEndpoint(), jiaYuPet.getBucketName(), path, 60, HttpMethod.PUT);
|
String uploadUrl = aliOssAbility.signatureUrl(bucketEnum.getEndpoint(), bucketEnum.getBucketName(), path, 60, HttpMethod.PUT);
|
||||||
|
|
||||||
//组装下载地址
|
//组装下载地址
|
||||||
String downloadUrl = jiaYuPet.getDomain() + "/" + path;
|
String downloadUrl = bucketEnum.getDomain() + "/" + path;
|
||||||
|
|
||||||
ossUrlVo.setUploadUrl(uploadUrl);
|
ossUrlVo.setUploadUrl(uploadUrl);
|
||||||
ossUrlVo.setDownloadUrl(downloadUrl);
|
ossUrlVo.setDownloadUrl(downloadUrl);
|
||||||
@@ -73,6 +77,9 @@ public class OssServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public OssUrlVo simpleUpload(String fileName, OssPathEnum pathEnum, InputStream inputStream) {
|
public OssUrlVo simpleUpload(String fileName, OssPathEnum pathEnum, InputStream inputStream) {
|
||||||
Assert.notNull(pathEnum, " 上传路径不能为空");
|
Assert.notNull(pathEnum, " 上传路径不能为空");
|
||||||
Assert.hasText(fileName, "文件不能为空");
|
Assert.hasText(fileName, "文件不能为空");
|
||||||
@@ -108,4 +115,8 @@ public class OssServer {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user