Commit 3f4d415c by 18356073052

增加腾讯云COS文件上传

1 parent 56136e32
package com.linkwechat.web.controller.common; package com.linkwechat.web.controller.common;
import com.linkwechat.common.config.CosConfig;
import com.linkwechat.common.config.RuoYiConfig; import com.linkwechat.common.config.RuoYiConfig;
import com.linkwechat.common.config.ServerConfig; import com.linkwechat.common.config.ServerConfig;
import com.linkwechat.common.constant.Constants; import com.linkwechat.common.constant.Constants;
...@@ -30,6 +31,9 @@ public class CommonController { ...@@ -30,6 +31,9 @@ public class CommonController {
@Autowired @Autowired
private ServerConfig serverConfig; private ServerConfig serverConfig;
@Autowired
private CosConfig cosConfig;
/** /**
* 通用下载请求 * 通用下载请求
* *
...@@ -104,4 +108,29 @@ public class CommonController { ...@@ -104,4 +108,29 @@ public class CommonController {
public void webResourceDownload(String url, HttpServletRequest request, HttpServletResponse response) throws Exception { public void webResourceDownload(String url, HttpServletRequest request, HttpServletResponse response) throws Exception {
FileUtils.downloadFile(url, response.getOutputStream()); FileUtils.downloadFile(url, response.getOutputStream());
} }
/**
* 通用上传请求
*/
@PostMapping("/common/uploadFile2Cos")
public AjaxResult uploadFile2Cos(MultipartFile file) throws Exception {
try {
String fileName = FileUploadUtils.upload2Cos(file, cosConfig);
AjaxResult ajax = AjaxResult.success();
ajax.put("fileName", fileName);
ajax.put("url", url(fileName));
return ajax;
} catch (Exception e) {
return AjaxResult.error(e.getMessage());
}
}
public String url(String fileName){
return "https://link-wechat-1251309172.cos.ap-nanjing.myqcloud.com/"+fileName;
}
} }
...@@ -145,7 +145,7 @@ xss: ...@@ -145,7 +145,7 @@ xss:
# elasticsearch配置 # elasticsearch配置
elasticsearch: elasticsearch:
schema: http schema: http
address: ${ES_ADDRESS:localhost:9200} address: ${ES_ADDRESS:localhost}
connectTimeout: 10000 connectTimeout: 10000
socketTimeout: 10000 socketTimeout: 10000
connectionRequestTimeout: 10000 connectionRequestTimeout: 10000
...@@ -207,3 +207,10 @@ wecome: ...@@ -207,3 +207,10 @@ wecome:
appIdOrCorpId: ww38152475d1bca752 appIdOrCorpId: ww38152475d1bca752
token: uEyhhuTKfpS token: uEyhhuTKfpS
encodingAesKey: diTFQ6B4IW14UN2JplADuhMZsBZHjvuBG24kHFrr4Px encodingAesKey: diTFQ6B4IW14UN2JplADuhMZsBZHjvuBG24kHFrr4Px
#腾讯云对象存储
cos:
secretId: AKIDQRCizIeUGl13vakkDQMH0VkjhPr1rBBv
secretKey: e3D8oFetseC0f8OX9XeV7UcTDiyyQ8cr
#link-wechat-1251309172.cos.ap-nanjing.myqcloud.com
region: ap-nanjing
bucketName: link-wechat-1251309172
\ No newline at end of file \ No newline at end of file
...@@ -239,6 +239,11 @@ ...@@ -239,6 +239,11 @@
<artifactId>elasticsearch</artifactId> <artifactId>elasticsearch</artifactId>
<version>7.6.0</version> <version>7.6.0</version>
</dependency> </dependency>
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos_api</artifactId>
<version>5.6.24</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
\ No newline at end of file \ No newline at end of file
package com.linkwechat.common.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author Kewen
*/
@Component
@ConfigurationProperties(prefix = "cos")
@Data
public class CosConfig {
private String secretId;
private String secretKey;
private String region;
private String bucketName;
}
...@@ -3,6 +3,8 @@ package com.linkwechat.common.utils.file; ...@@ -3,6 +3,8 @@ package com.linkwechat.common.utils.file;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.linkwechat.common.config.CosConfig;
import com.linkwechat.common.config.RuoYiConfig; import com.linkwechat.common.config.RuoYiConfig;
import com.linkwechat.common.constant.Constants; import com.linkwechat.common.constant.Constants;
import com.linkwechat.common.exception.file.FileNameLengthLimitExceededException; import com.linkwechat.common.exception.file.FileNameLengthLimitExceededException;
...@@ -11,6 +13,15 @@ import com.linkwechat.common.exception.file.InvalidExtensionException; ...@@ -11,6 +13,15 @@ import com.linkwechat.common.exception.file.InvalidExtensionException;
import com.linkwechat.common.utils.DateUtils; import com.linkwechat.common.utils.DateUtils;
import com.linkwechat.common.utils.StringUtils; import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.common.utils.uuid.IdUtils; import com.linkwechat.common.utils.uuid.IdUtils;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
...@@ -19,6 +30,7 @@ import org.springframework.web.multipart.MultipartFile; ...@@ -19,6 +30,7 @@ import org.springframework.web.multipart.MultipartFile;
* *
* @author ruoyi * @author ruoyi
*/ */
@Slf4j
public class FileUploadUtils { public class FileUploadUtils {
/** /**
* 默认大小 50M * 默认大小 50M
...@@ -120,6 +132,67 @@ public class FileUploadUtils { ...@@ -120,6 +132,67 @@ public class FileUploadUtils {
return pathFileName; return pathFileName;
} }
/**
* 上传到腾讯对象云存储中
*
* @param file 上传的文件
* @return 访问路径
* @throws IOException
*/
public static final String upload2Cos(MultipartFile file , CosConfig cosConfig) throws IOException {
try {
return upload2Cos( file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION,cosConfig);
} catch (Exception e) {
throw new IOException(e.getMessage(), e);
}
}
/**
* 文件上传
*
* @param file 上传的文件
* @param allowedExtension 上传文件类型
* @return 返回上传成功的文件名
* @throws FileSizeLimitExceededException 如果超出最大大小
* @throws FileNameLengthLimitExceededException 文件名太长
* @throws IOException 比如读写文件出错时
* @throws InvalidExtensionException 文件校验异常
*/
public static final String upload2Cos( MultipartFile file, String[] allowedExtension, CosConfig cosConfig)
throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
InvalidExtensionException {
int fileNamelength = file.getOriginalFilename().length();
if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) {
throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
}
assertAllowed(file, allowedExtension);
String fileName = extractFilename(file);
// 1 初始化用户身份信息(secretId, secretKey)。
COSCredentials cred = new BasicCOSCredentials(cosConfig.getSecretId(), cosConfig.getSecretKey());
// 2 设置 bucket 的区域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
// clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
Region region = new Region(cosConfig.getRegion());
ClientConfig clientConfig = new ClientConfig(region);
// 3 生成 cos 客户端。
COSClient cosClient = new COSClient(cred, clientConfig);
// 指定要上传到 COS 上对象键
String key = "exampleobject";
ObjectMetadata objectMetadata = new ObjectMetadata();
PutObjectRequest putObjectRequest = new PutObjectRequest(cosConfig.getBucketName(), fileName,file.getInputStream(),objectMetadata);
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
log.info("{}","https://link-wechat-1251309172.cos.ap-nanjing.myqcloud.com/"+fileName);
log.info("腾讯cos上传信息:{}",new ObjectMapper().writeValueAsString(putObjectResult));
cosClient.shutdown();
return fileName;
}
public static final String uploadFile(String baseDir, MultipartFile file) public static final String uploadFile(String baseDir, MultipartFile file)
throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException { throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException {
......
...@@ -124,6 +124,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter ...@@ -124,6 +124,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
.antMatchers("/wecom/ticket/**").anonymous() .antMatchers("/wecom/ticket/**").anonymous()
.antMatchers("/wecom/user/getUserInfo").anonymous() .antMatchers("/wecom/user/getUserInfo").anonymous()
.antMatchers("/common/uploadFile2Cos").anonymous()
// 除上面外的所有请求全部需要鉴权认证 // 除上面外的所有请求全部需要鉴权认证
.anyRequest().authenticated() .anyRequest().authenticated()
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!