腾讯云cos文件上传服务,Log注解新增

This commit is contained in:
syruan 2024-04-02 17:46:55 +08:00
parent 7a8b15a320
commit 97eb2e5040
10 changed files with 352 additions and 138 deletions

View File

@ -41,16 +41,16 @@ public interface RemoteUserService
/** /**
* 发送短信 * 发送短信
* @param phone * @param phone 手机号码
* @return * @return 验证码
*/ */
@PostMapping("/sms/codeLogin") @PostMapping("/sms/codeLogin")
public R<Boolean> sendCode(@RequestParam("phone") String phone); public R<Boolean> sendCode(@RequestParam("phone") String phone);
/** /**
* 验证码校验 * 验证码校验
* @param phone * @param phone 手机号
* @param code * @param code 验证码
* @return * @return
*/ */
@PostMapping("/sms/checkCode") @PostMapping("/sms/checkCode")
@ -58,8 +58,8 @@ public interface RemoteUserService
/** /**
* 验证码校验 * 验证码校验
* @param phone * @param phone 手机号
* @param msg * @param msg 内容
* @return * @return
*/ */
@PostMapping("/sms/send") @PostMapping("/sms/send")

View File

@ -56,8 +56,14 @@ public enum BusinessType
* 清空数据 * 清空数据
*/ */
CLEAN, CLEAN,
/** /**
* 查询 * 查询
*/ */
QUERY, QUERY,
/**
* 物资库存
*/
MATERIAL
} }

View File

@ -17,6 +17,13 @@
<dependencies> <dependencies>
<!-- 腾讯云cos 存储桶服务 -->
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos_api</artifactId>
<version>5.6.54</version>
</dependency>
<!-- SpringCloud Alibaba Nacos --> <!-- SpringCloud Alibaba Nacos -->
<dependency> <dependency>
<groupId>com.alibaba.cloud</groupId> <groupId>com.alibaba.cloud</groupId>

View File

@ -0,0 +1,41 @@
package com.bonus.sgzb.file.config;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* Description: 腾讯云cos配置
*
* @Author 阮世耀
* @Create 2023/12/3 16:26
* @Version 1.0
*/
@Component
public class ConstantPropertiesUtil implements InitializingBean {
@Value("${tencent.cos.file.region}")
private String region;
@Value("${tencent.cos.file.secretid}")
private String secretId;
@Value("${tencent.cos.file.secretkey}")
private String secretKey;
@Value("${tencent.cos.file.bucketname}")
private String bucketName;
public static String END_POINT;
public static String ACCESS_KEY_ID;
public static String ACCESS_KEY_SECRET;
public static String BUCKET_NAME;
@Override
public void afterPropertiesSet() throws Exception {
END_POINT = region;
ACCESS_KEY_ID = secretId;
ACCESS_KEY_SECRET = secretKey;
BUCKET_NAME = bucketName;
}
}

View File

@ -1,82 +1,82 @@
package com.bonus.sgzb.file.config; //package com.bonus.sgzb.file.config;
//
import org.springframework.boot.context.properties.ConfigurationProperties; //import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean; //import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; //import org.springframework.context.annotation.Configuration;
import io.minio.MinioClient; //import io.minio.MinioClient;
//
/** ///**
* Minio 配置信息 // * Minio 配置信息
* // *
* @author ruoyi // * @author ruoyi
*/ // */
@Configuration //@Configuration
@ConfigurationProperties(prefix = "minio") //@ConfigurationProperties(prefix = "minio")
public class MinioConfig //public class MinioConfig
{ //{
/** // /**
* 服务地址 // * 服务地址
*/ // */
private String url; // private String url;
//
/** // /**
* 用户名 // * 用户名
*/ // */
private String accessKey; // private String accessKey;
//
/** // /**
* 密码 // * 密码
*/ // */
private String secretKey; // private String secretKey;
//
/** // /**
* 存储桶名称 // * 存储桶名称
*/ // */
private String bucketName; // private String bucketName;
//
public String getUrl() // public String getUrl()
{ // {
return url; // return url;
} // }
//
public void setUrl(String url) // public void setUrl(String url)
{ // {
this.url = url; // this.url = url;
} // }
//
public String getAccessKey() // public String getAccessKey()
{ // {
return accessKey; // return accessKey;
} // }
//
public void setAccessKey(String accessKey) // public void setAccessKey(String accessKey)
{ // {
this.accessKey = accessKey; // this.accessKey = accessKey;
} // }
//
public String getSecretKey() // public String getSecretKey()
{ // {
return secretKey; // return secretKey;
} // }
//
public void setSecretKey(String secretKey) // public void setSecretKey(String secretKey)
{ // {
this.secretKey = secretKey; // this.secretKey = secretKey;
} // }
//
public String getBucketName() // public String getBucketName()
{ // {
return bucketName; // return bucketName;
} // }
//
public void setBucketName(String bucketName) // public void setBucketName(String bucketName)
{ // {
this.bucketName = bucketName; // this.bucketName = bucketName;
} // }
//
@Bean // @Bean
public MinioClient getMinioClient() // public MinioClient getMinioClient()
{ // {
return MinioClient.builder().endpoint(url).credentials(accessKey, secretKey).build(); // return MinioClient.builder().endpoint(url).credentials(accessKey, secretKey).build();
} // }
} //}

View File

@ -0,0 +1,41 @@
package com.bonus.sgzb.file.controller;
import com.bonus.sgzb.common.core.web.controller.BaseController;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.file.service.FileUploadTencentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
/**
* Description: 腾讯云文件上传接口
*
* @Author 阮世耀
* @Create 2023/12/3 16:33
* @Version 1.0
*/
@Api(tags = "腾讯云文件上传接口")
@RestController
@RequestMapping("/tencent/cos")
public class FileUploadTencentController extends BaseController {
@Resource
private FileUploadTencentService fileUploadTencentService;
@ApiOperation(value = "文件上传")
@PostMapping("uploadFile")
public AjaxResult uploadFile(
@ApiParam(name = "file", value = "文件", required = true)
@RequestParam("file") MultipartFile file) {
String uploadUrl = fileUploadTencentService.uploadFile(file);
return success(uploadUrl);
}
}

View File

@ -0,0 +1,20 @@
package com.bonus.sgzb.file.service;
import org.springframework.web.multipart.MultipartFile;
/**
* Description: 腾讯云上传服务
*
* @Author 阮世耀
* @Create 2023/12/3 16:28
* @Version 1.0
*/
public interface FileUploadTencentService {
/**
* 上传文件
* @param file 上传的文件
* @return 上传成功返回文件地址失败返回null
*/
String uploadFile(MultipartFile file);
}

View File

@ -0,0 +1,77 @@
package com.bonus.sgzb.file.service;
import com.bonus.sgzb.common.core.utils.uuid.IdUtils;
import com.bonus.sgzb.common.core.utils.uuid.UUID;
import com.bonus.sgzb.file.config.ConstantPropertiesUtil;
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.http.HttpProtocol;
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 org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
/**
* Description: 上传接口实现
*
* @Author 阮世耀
* @Create 2023/12/3 16:29
* @Version 1.0
*/
@Service
public class FileUploadTencentServiceImpl implements FileUploadTencentService {
/**
* 上传文件到腾讯云对象存储COS
* @param file 要上传的文件
* @return 上传成功返回文件的URL失败返回null
*/
@Override
public String uploadFile(MultipartFile file) {
// 1 初始化用户身份信息secretId, secretKey
String secretId = ConstantPropertiesUtil.ACCESS_KEY_ID;
String secretKey = ConstantPropertiesUtil.ACCESS_KEY_SECRET;
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
// 2 设置 bucket 的地域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
Region region = new Region(ConstantPropertiesUtil.END_POINT);
ClientConfig clientConfig = new ClientConfig();
// 这里建议设置使用 https 协议
clientConfig.setHttpProtocol(HttpProtocol.https);
clientConfig.setRegion(region);
// 3 生成 cos 客户端
COSClient cosClient = new COSClient(cred, clientConfig);
// 存储桶的命名格式为 BucketName-APPID此处填写的存储桶名称必须为此格式
String bucketName = ConstantPropertiesUtil.BUCKET_NAME;
// 对象键(Key)是对象在存储桶中的唯一标识 998u-09iu-09i-333
//在文件名称前面添加uuid值
String key = IdUtils.fastSimpleUuid() + file.getOriginalFilename();
//对上传文件分组根据当前日期 /2024/04/02
String dateTime = new DateTime().toString("yyyy/MM/dd");
key = dateTime+"/"+key;
try {
//获取上传文件输入流
InputStream inputStream = file.getInputStream();
ObjectMetadata objectMetadata = new ObjectMetadata();
PutObjectRequest putObjectRequest = new PutObjectRequest(
bucketName,
key,
inputStream,
objectMetadata);
// 高级接口会返回一个异步结果Upload
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
// 返回上传文件路径:https://ggkt-atguigu-1310644373.cos.ap-beijing.myqcloud.com/01.jpg
return "https://" + bucketName + "." + "cos" + "." + ConstantPropertiesUtil.END_POINT+".myqcloud.com" + "/" + key;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

View File

@ -1,49 +1,49 @@
package com.bonus.sgzb.file.service; //package com.bonus.sgzb.file.service;
//
import java.io.InputStream; //import java.io.InputStream;
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 org.springframework.web.multipart.MultipartFile; //import org.springframework.web.multipart.MultipartFile;
import com.alibaba.nacos.common.utils.IoUtils; //import com.alibaba.nacos.common.utils.IoUtils;
import com.bonus.sgzb.file.config.MinioConfig; //import com.bonus.sgzb.file.config.MinioConfig;
import com.bonus.sgzb.file.utils.FileUploadUtils; //import com.bonus.sgzb.file.utils.FileUploadUtils;
import io.minio.MinioClient; //import io.minio.MinioClient;
import io.minio.PutObjectArgs; //import io.minio.PutObjectArgs;
//
/** ///**
* Minio 文件存储 // * Minio 文件存储
* // *
* @author ruoyi // * @author ruoyi
*/ // */
@Service //@Service
public class MinioSysFileServiceImpl implements ISysFileService //public class MinioSysFileServiceImpl implements ISysFileService
{ //{
@Autowired // @Autowired
private MinioConfig minioConfig; // private MinioConfig minioConfig;
//
@Autowired // @Autowired
private MinioClient client; // private MinioClient client;
//
/** // /**
* Minio文件上传接口 // * Minio文件上传接口
* // *
* @param file 上传的文件 // * @param file 上传的文件
* @return 访问地址 // * @return 访问地址
* @throws Exception // * @throws Exception
*/ // */
@Override // @Override
public String uploadFile(MultipartFile file) throws Exception // public String uploadFile(MultipartFile file) throws Exception
{ // {
String fileName = FileUploadUtils.extractFilename(file); // String fileName = FileUploadUtils.extractFilename(file);
InputStream inputStream = file.getInputStream(); // InputStream inputStream = file.getInputStream();
PutObjectArgs args = PutObjectArgs.builder() // PutObjectArgs args = PutObjectArgs.builder()
.bucket(minioConfig.getBucketName()) // .bucket(minioConfig.getBucketName())
.object(fileName) // .object(fileName)
.stream(inputStream, file.getSize(), -1) // .stream(inputStream, file.getSize(), -1)
.contentType(file.getContentType()) // .contentType(file.getContentType())
.build(); // .build();
client.putObject(args); // client.putObject(args);
IoUtils.closeQuietly(inputStream); // IoUtils.closeQuietly(inputStream);
return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName; // return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
} // }
} //}

View File

@ -2,6 +2,28 @@
server: server:
port: 9300 port: 9300
# 腾讯云cos
tencent:
cos:
file:
# 存储桶所在地域
region: ap-guangzhou
# 存储桶所在地域
bucketregion: ap-guangzhou
# 存储桶名称
bucketname: prod-rental-1301524038
# API账号
secretid: AKIDrreCVaRKDtMcgfU5QW9iEfv67tMfldJn
# API密钥
secretkey: OXUgeMo0yhBRTGo6sVu3yiFX4rQtAzc3
file:
path: 1
prefix: 2
domain: 3
fdfs:
domain: 3
# Spring # Spring
spring: spring:
application: application: