切换OSS为阿里云OSS,文件上传成功,文件访问还有问题
This commit is contained in:
@@ -36,6 +36,7 @@
|
|||||||
<version>1.0</version>
|
<version>1.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
<!--JWT(Json Web Token)登录支持-->
|
<!--JWT(Json Web Token)登录支持-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.jsonwebtoken</groupId>
|
<groupId>io.jsonwebtoken</groupId>
|
||||||
@@ -63,6 +64,11 @@
|
|||||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.aliyun.oss</groupId>
|
||||||
|
<artifactId>aliyun-sdk-oss</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!--weixin-java-common-->
|
<!--weixin-java-common-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.binarywang</groupId>
|
<groupId>com.github.binarywang</groupId>
|
||||||
@@ -92,7 +98,6 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
@@ -24,7 +24,7 @@ public class MyTenantLineHandler implements TenantLineHandler {
|
|||||||
/**
|
/**
|
||||||
* 排除过滤的表
|
* 排除过滤的表
|
||||||
*/
|
*/
|
||||||
private static final String[] TABLE_FILTER = {"sys_menu", "sys_tenant_package", "sys_tenant", "sys_dict", "sys_dict_data"};
|
private static final String[] TABLE_FILTER = {"sys_user", "sys_menu", "sys_tenant_package", "sys_tenant", "sys_dict", "sys_dict_data"};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 排除过滤的表前缀
|
* 排除过滤的表前缀
|
||||||
@@ -38,6 +38,9 @@ public class MyTenantLineHandler implements TenantLineHandler {
|
|||||||
if (StrUtil.isBlankIfStr(tenantId)) {
|
if (StrUtil.isBlankIfStr(tenantId)) {
|
||||||
return new NullValue();
|
return new NullValue();
|
||||||
}
|
}
|
||||||
|
if (StrUtil.isBlankIfStr(tenantId)) {
|
||||||
|
tenantId = "9999";
|
||||||
|
}
|
||||||
return new StringValue(tenantId);
|
return new StringValue(tenantId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package com.starry.admin.common.oss;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Configuration
|
||||||
|
@ConfigurationProperties(prefix = "aliyun")
|
||||||
|
@PropertySource(value = {"classpath:oss.properties"})
|
||||||
|
public class OssProperties implements InitializingBean {
|
||||||
|
|
||||||
|
public String endpoint;
|
||||||
|
public String accessKeyId;
|
||||||
|
public String accessKeySecret;
|
||||||
|
public String bucketName;
|
||||||
|
|
||||||
|
|
||||||
|
public static String ENDPOINT = "";
|
||||||
|
public static String KEY_ID = "";
|
||||||
|
public static String KEY_SECRET = "";
|
||||||
|
public static String BUCKET_NAME = "";
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterPropertiesSet() throws Exception {
|
||||||
|
ENDPOINT = getEndpoint();
|
||||||
|
KEY_ID = getAccessKeyId();
|
||||||
|
KEY_SECRET = getAccessKeySecret();
|
||||||
|
BUCKET_NAME = getBucketName();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package com.starry.admin.common.oss.controller;
|
||||||
|
|
||||||
|
import com.starry.admin.common.oss.service.IOssFileService;
|
||||||
|
import com.starry.common.result.R;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
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.RestController;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author admin
|
||||||
|
* cos存储前端控制器
|
||||||
|
* @since 2022/11/13 17:51
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/cos")
|
||||||
|
public class CosController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
IOssFileService ossFileService;
|
||||||
|
|
||||||
|
|
||||||
|
@ApiOperation(value = "照片上传")
|
||||||
|
@PostMapping("/upload/image")
|
||||||
|
public R uploadImage(MultipartFile file) throws Exception {
|
||||||
|
if (!file.isEmpty()) {
|
||||||
|
String house = ossFileService.upload(file.getInputStream(), "house", file.getOriginalFilename());
|
||||||
|
return R.ok(house);
|
||||||
|
}
|
||||||
|
return R.error("上传照片异常,请联系管理员");
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiOperation(value = "获取cos临时密钥")
|
||||||
|
@GetMapping("/temp-key")
|
||||||
|
public R getTempKey() throws FileNotFoundException {
|
||||||
|
FileInputStream inputStream = new FileInputStream(new File("C:\\Users\\admin\\Pictures\\0001.jpg"));
|
||||||
|
ossFileService.upload(inputStream, "test", "0001.png");
|
||||||
|
return R.ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package com.starry.admin.common.oss.service;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
public interface IOssFileService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件上传阿里云
|
||||||
|
*
|
||||||
|
* @param inputStream InputStream
|
||||||
|
* @param module String
|
||||||
|
* @param originalFilename 文件名称
|
||||||
|
*/
|
||||||
|
String upload(InputStream inputStream, String module, String originalFilename);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除文件
|
||||||
|
*
|
||||||
|
* @param url 文件地址
|
||||||
|
*/
|
||||||
|
void remove(String url);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
package com.starry.admin.common.oss.service.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import cn.hutool.core.date.DateTime;
|
||||||
|
import cn.hutool.core.io.FileTypeUtil;
|
||||||
|
import cn.hutool.core.util.IdUtil;
|
||||||
|
import com.aliyun.oss.ClientException;
|
||||||
|
import com.aliyun.oss.OSS;
|
||||||
|
import com.aliyun.oss.OSSClientBuilder;
|
||||||
|
import com.aliyun.oss.OSSException;
|
||||||
|
import com.aliyun.oss.model.CannedAccessControlList;
|
||||||
|
import com.aliyun.oss.model.PutObjectRequest;
|
||||||
|
import com.starry.admin.common.exception.CustomException;
|
||||||
|
import com.starry.admin.common.oss.OssProperties;
|
||||||
|
import com.starry.admin.common.oss.service.IOssFileService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class OssFileServiceImpl implements IOssFileService {
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String upload(InputStream inputStream, String module, String filename) {
|
||||||
|
// 创建OSSClient实例。
|
||||||
|
OSS ossClient = new OSSClientBuilder().build(OssProperties.ENDPOINT, OssProperties.KEY_ID, OssProperties.KEY_SECRET);
|
||||||
|
log.info("OSSClient实例创建成功");
|
||||||
|
try {
|
||||||
|
//判断oss实例是否存在:如果不存在则创建,如果存在则获取
|
||||||
|
if (!ossClient.doesBucketExist(OssProperties.BUCKET_NAME)) {
|
||||||
|
//创建bucket
|
||||||
|
ossClient.createBucket(OssProperties.BUCKET_NAME);
|
||||||
|
log.info("bucket存储空间【{}】创建成功", OssProperties.BUCKET_NAME);
|
||||||
|
//设置oss实例的访问权限:公共读
|
||||||
|
ossClient.setBucketAcl(OssProperties.BUCKET_NAME, CannedAccessControlList.PublicRead);
|
||||||
|
log.info("【{}】存储空间访问权限设置为公共读成功", OssProperties.BUCKET_NAME);
|
||||||
|
}
|
||||||
|
//构建日期路径:avatar/2019/02/26/文件名
|
||||||
|
String folder = new DateTime().toString("yyyy/MM/dd");
|
||||||
|
//文件名:uuid.扩展名
|
||||||
|
filename = IdUtil.fastSimpleUUID() + FileTypeUtil.getType(inputStream);
|
||||||
|
//文件根路径
|
||||||
|
String key = module + "/" + folder + "/" + filename;
|
||||||
|
// 创建PutObjectRequest对象。
|
||||||
|
PutObjectRequest putObjectRequest = new PutObjectRequest(OssProperties.BUCKET_NAME, key, inputStream);
|
||||||
|
// 创建PutObject请求。
|
||||||
|
ossClient.putObject(putObjectRequest);
|
||||||
|
log.info("oss文件上传成功");
|
||||||
|
//阿里云文件绝对路径
|
||||||
|
String endpoint = OssProperties.ENDPOINT.substring(OssProperties.ENDPOINT.lastIndexOf("//") + 2);
|
||||||
|
//返回文件的访问路径
|
||||||
|
return "https://" + OssProperties.BUCKET_NAME + "." + endpoint + "/" + key;
|
||||||
|
} catch (OSSException oe) {
|
||||||
|
log.error("OSSException 文件上传失败:", oe);
|
||||||
|
throw new CustomException("OSS文件上传异常,{}" + oe.getMessage());
|
||||||
|
} catch (ClientException ce) {
|
||||||
|
log.error("ClientException 文件上传失败:{}", ExceptionUtils.getStackTrace(ce));
|
||||||
|
throw new CustomException("OSS文件上传异常,{}" + ce.getMessage());
|
||||||
|
} finally {
|
||||||
|
if (ossClient != null) {
|
||||||
|
ossClient.shutdown();
|
||||||
|
log.info("关闭ossClient");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove(String url) {
|
||||||
|
OSS ossClient = new OSSClientBuilder().build(OssProperties.ENDPOINT, OssProperties.KEY_ID, OssProperties.KEY_SECRET);
|
||||||
|
log.info("OSSClient实例创建成功");
|
||||||
|
try {
|
||||||
|
String endpoint = OssProperties.ENDPOINT.substring(OssProperties.ENDPOINT.lastIndexOf("//") + 2);
|
||||||
|
//文件名(服务器上的文件路径)
|
||||||
|
String host = "https://" + OssProperties.BUCKET_NAME + "." + endpoint + "/";
|
||||||
|
String objectName = url.substring(host.length());
|
||||||
|
// 删除文件或目录。如果要删除目录,目录必须为空。
|
||||||
|
ossClient.deleteObject(OssProperties.BUCKET_NAME, objectName);
|
||||||
|
log.info("{}文件删除成功", objectName);
|
||||||
|
} catch (OSSException oe) {
|
||||||
|
log.error("OSSException 文件删除失败", oe);
|
||||||
|
throw new CustomException("OSS文件删除异常,{}" + oe.getMessage());
|
||||||
|
} catch (ClientException ce) {
|
||||||
|
log.error("ClientException 文件删除失败:{}", ExceptionUtils.getStackTrace(ce));
|
||||||
|
throw new CustomException("OSS文件删除异常,{}" + ce.getMessage());
|
||||||
|
} finally {
|
||||||
|
if (ossClient != null) {
|
||||||
|
// 关闭OSSClient。
|
||||||
|
ossClient.shutdown();
|
||||||
|
log.info("关闭ossClient");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ package com.starry.admin.modules.system.controller;
|
|||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.starry.admin.common.component.JwtToken;
|
import com.starry.admin.common.component.JwtToken;
|
||||||
import com.starry.admin.common.domain.LoginUser;
|
import com.starry.admin.common.domain.LoginUser;
|
||||||
|
import com.starry.admin.common.oss.service.IOssFileService;
|
||||||
import com.starry.admin.modules.system.entity.SysRoleEntity;
|
import com.starry.admin.modules.system.entity.SysRoleEntity;
|
||||||
import com.starry.admin.modules.system.entity.SysUserEntity;
|
import com.starry.admin.modules.system.entity.SysUserEntity;
|
||||||
import com.starry.admin.modules.system.service.SysRoleService;
|
import com.starry.admin.modules.system.service.SysRoleService;
|
||||||
@@ -14,7 +15,6 @@ import com.starry.common.annotation.Log;
|
|||||||
import com.starry.common.constant.UserConstants;
|
import com.starry.common.constant.UserConstants;
|
||||||
import com.starry.common.enums.BusinessType;
|
import com.starry.common.enums.BusinessType;
|
||||||
import com.starry.common.result.R;
|
import com.starry.common.result.R;
|
||||||
import com.starry.common.utils.file.CosClientUtils;
|
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
@@ -47,6 +47,9 @@ public class SysUserController {
|
|||||||
@Resource
|
@Resource
|
||||||
private JwtToken tokenService;
|
private JwtToken tokenService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private IOssFileService ossFileService;
|
||||||
|
|
||||||
@ApiOperation(value = "注册用户")
|
@ApiOperation(value = "注册用户")
|
||||||
@PostMapping(value = "register")
|
@PostMapping(value = "register")
|
||||||
public R register(@RequestBody SysUserEntity user) {
|
public R register(@RequestBody SysUserEntity user) {
|
||||||
@@ -133,7 +136,7 @@ public class SysUserController {
|
|||||||
@PostMapping("/profile/avatar")
|
@PostMapping("/profile/avatar")
|
||||||
public R uploadAvatar(@RequestParam("avatarfile") MultipartFile file) throws Exception {
|
public R uploadAvatar(@RequestParam("avatarfile") MultipartFile file) throws Exception {
|
||||||
if (!file.isEmpty()) {
|
if (!file.isEmpty()) {
|
||||||
String avatar = CosClientUtils.upload(file, "avatar");
|
String avatar = ossFileService.upload(file.getInputStream(), "avatar", file.getOriginalFilename());
|
||||||
if (userService.updateUserAvatar(SecurityUtils.getUserId(), avatar)) {
|
if (userService.updateUserAvatar(SecurityUtils.getUserId(), avatar)) {
|
||||||
// 更新缓存用户头像
|
// 更新缓存用户头像
|
||||||
LoginUser loginUser = SecurityUtils.getLoginUser();
|
LoginUser loginUser = SecurityUtils.getLoginUser();
|
||||||
|
|||||||
@@ -90,13 +90,4 @@ xl:
|
|||||||
authCode:
|
authCode:
|
||||||
# 登录验证码是否开启,开发环境配置false方便测试
|
# 登录验证码是否开启,开发环境配置false方便测试
|
||||||
enable: false
|
enable: false
|
||||||
# 腾讯云cos配置
|
|
||||||
cos:
|
|
||||||
baseUrl: https://admin-125966.cos.ap-guangzhou.myqcloud.com
|
|
||||||
secretId: AKIDdHsLgtxoSs3sWw73lz
|
|
||||||
secretKey: zZxBD0b4QcZGmdFcotm
|
|
||||||
regionName: ap-guangzhou
|
|
||||||
bucketName: admin-125966
|
|
||||||
folderPrefix: /upload
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -85,12 +85,4 @@ xl:
|
|||||||
login:
|
login:
|
||||||
authCode:
|
authCode:
|
||||||
# 登录验证码是否开启,开发环境配置false方便测试
|
# 登录验证码是否开启,开发环境配置false方便测试
|
||||||
enable: false
|
enable: false
|
||||||
# 腾讯云cos配置
|
|
||||||
cos:
|
|
||||||
baseUrl: https://admin-125966.cos.ap-guangzhou.myqcloud.com
|
|
||||||
secretId: AKIDdHsLgtxoSs3sWw73lz
|
|
||||||
secretKey: zZxBD0b4QcZGmdFcotm
|
|
||||||
regionName: ap-guangzhou
|
|
||||||
bucketName: admin-125966
|
|
||||||
folderPrefix: /upload
|
|
||||||
@@ -90,13 +90,5 @@ xl:
|
|||||||
authCode:
|
authCode:
|
||||||
# 登录验证码是否开启,开发环境配置false方便测试
|
# 登录验证码是否开启,开发环境配置false方便测试
|
||||||
enable: false
|
enable: false
|
||||||
# 腾讯云cos配置
|
|
||||||
cos:
|
|
||||||
baseUrl: https://admin-125966.cos.ap-guangzhou.myqcloud.com
|
|
||||||
secretId: AKIDdHsLgtxoSs3sWw73lz
|
|
||||||
secretKey: zZxBD0b4QcZGmdFcotm
|
|
||||||
regionName: ap-guangzhou
|
|
||||||
bucketName: admin-125966
|
|
||||||
folderPrefix: /upload
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
4
play-admin/src/main/resources/oss.properties
Normal file
4
play-admin/src/main/resources/oss.properties
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
aliyun.endpoint=
|
||||||
|
aliyun.accessKeyId=
|
||||||
|
aliyun.accessKeySecret=
|
||||||
|
aliyun.bucketName=
|
||||||
@@ -93,31 +93,13 @@
|
|||||||
<groupId>com.github.gavlyukovskiy</groupId>
|
<groupId>com.github.gavlyukovskiy</groupId>
|
||||||
<artifactId>p6spy-spring-boot-starter</artifactId>
|
<artifactId>p6spy-spring-boot-starter</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!--腾讯云 COS 对象存储-->
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.qcloud</groupId>
|
|
||||||
<artifactId>cos_api</artifactId>
|
|
||||||
<exclusions>
|
|
||||||
<exclusion>
|
|
||||||
<artifactId>tencentcloud-sdk-java-common</artifactId>
|
|
||||||
<groupId>com.tencentcloudapi</groupId>
|
|
||||||
</exclusion>
|
|
||||||
<exclusion>
|
|
||||||
<artifactId>gson</artifactId>
|
|
||||||
<groupId>com.google.code.gson</groupId>
|
|
||||||
</exclusion>
|
|
||||||
</exclusions>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.code.gson</groupId>
|
<groupId>com.google.code.gson</groupId>
|
||||||
<artifactId>gson</artifactId>
|
<artifactId>gson</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.qcloud</groupId>
|
|
||||||
<artifactId>cos-sts_api</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<!-- Transmittable ThreadLocal -->
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.alibaba</groupId>
|
<groupId>com.alibaba</groupId>
|
||||||
<artifactId>transmittable-thread-local</artifactId>
|
<artifactId>transmittable-thread-local</artifactId>
|
||||||
|
|||||||
@@ -1,56 +0,0 @@
|
|||||||
package com.starry.common.config;
|
|
||||||
|
|
||||||
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.region.Region;
|
|
||||||
import lombok.Data;
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author admin
|
|
||||||
* 腾讯云cos对象存储配置类
|
|
||||||
* @since 2022/10/28
|
|
||||||
*/
|
|
||||||
@Data
|
|
||||||
@Component
|
|
||||||
@ConfigurationProperties(prefix = "cos")
|
|
||||||
public class CosConfig {
|
|
||||||
/**
|
|
||||||
* 存储桶访问路径
|
|
||||||
**/
|
|
||||||
private String baseUrl;
|
|
||||||
/**
|
|
||||||
* 腾讯云账号秘钥
|
|
||||||
**/
|
|
||||||
private String secretId;
|
|
||||||
/**
|
|
||||||
* 密码秘钥
|
|
||||||
**/
|
|
||||||
private String secretKey;
|
|
||||||
/**
|
|
||||||
* 存储桶地区
|
|
||||||
**/
|
|
||||||
private String regionName;
|
|
||||||
/**
|
|
||||||
* 存储桶名称
|
|
||||||
**/
|
|
||||||
private String bucketName;
|
|
||||||
/**
|
|
||||||
* 上传的根目录
|
|
||||||
**/
|
|
||||||
private String folderPrefix;
|
|
||||||
|
|
||||||
public COSClient getCosClient() {
|
|
||||||
// 初始化用户信息
|
|
||||||
COSCredentials cosCredentials = new BasicCOSCredentials(this.secretId, this.secretKey);
|
|
||||||
// 设置地域
|
|
||||||
Region region = new Region(this.regionName);
|
|
||||||
ClientConfig config = new ClientConfig(region);
|
|
||||||
// 生成COS客户端
|
|
||||||
return new COSClient(cosCredentials, config);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,83 +0,0 @@
|
|||||||
package com.starry.common.controller;
|
|
||||||
|
|
||||||
import com.starry.common.result.R;
|
|
||||||
import com.starry.common.utils.file.CosClientUtils;
|
|
||||||
import com.tencent.cloud.CosStsClient;
|
|
||||||
import com.tencent.cloud.Response;
|
|
||||||
import io.swagger.annotations.ApiOperation;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
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.RestController;
|
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
|
||||||
|
|
||||||
import java.util.TreeMap;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author admin
|
|
||||||
* cos存储前端控制器
|
|
||||||
* @since 2022/11/13 17:51
|
|
||||||
*/
|
|
||||||
@Slf4j
|
|
||||||
@RestController
|
|
||||||
@RequestMapping("/cos")
|
|
||||||
public class CosController {
|
|
||||||
|
|
||||||
@Value("${cos.secretId}")
|
|
||||||
private String secretId;
|
|
||||||
@Value("${cos.secretKey}")
|
|
||||||
private String secretKey;
|
|
||||||
@Value("${cos.regionName}")
|
|
||||||
private String regionName;
|
|
||||||
@Value("${cos.bucketName}")
|
|
||||||
private String bucketName;
|
|
||||||
|
|
||||||
@ApiOperation(value = "照片上传")
|
|
||||||
@PostMapping("/upload/image")
|
|
||||||
public R uploadImage(MultipartFile file) throws Exception {
|
|
||||||
if (!file.isEmpty()) {
|
|
||||||
String avatar = CosClientUtils.upload(file, "house");
|
|
||||||
return R.ok(avatar);
|
|
||||||
}
|
|
||||||
return R.error("上传照片异常,请联系管理员");
|
|
||||||
}
|
|
||||||
|
|
||||||
@ApiOperation(value = "获取cos临时密钥")
|
|
||||||
@GetMapping("/temp-key")
|
|
||||||
public R getTempKey() {
|
|
||||||
TreeMap<String, Object> config = new TreeMap<>();
|
|
||||||
try {
|
|
||||||
// 替换为您的云 api 密钥 SecretId
|
|
||||||
config.put("secretId", secretId);
|
|
||||||
// 替换为您的云 api 密钥 SecretKey
|
|
||||||
config.put("secretKey", secretKey);
|
|
||||||
// 临时密钥有效时长,单位是秒,默认 1800 秒,目前主账号最长 2 小时(即 7200 秒),子账号最长 36 小时(即 129600)秒
|
|
||||||
config.put("durationSeconds", 1800);
|
|
||||||
// 换成您的 bucket
|
|
||||||
config.put("bucket", bucketName);
|
|
||||||
// 换成 bucket 所在地区
|
|
||||||
config.put("region", regionName);
|
|
||||||
|
|
||||||
// 只允许用户访问 upload/house 目录下的资源
|
|
||||||
config.put("allowPrefixes", new String[]{"upload/house/*"});
|
|
||||||
|
|
||||||
// 密钥的权限列表。必须在这里指定本次临时密钥所需要的权限。
|
|
||||||
String[] allowActions = new String[]{
|
|
||||||
// 简单上传
|
|
||||||
"name/cos:PutObject",
|
|
||||||
// 表单上传、小程序上传
|
|
||||||
"name/cos:PostObject",
|
|
||||||
// 分块上传
|
|
||||||
"name/cos:InitiateMultipartUpload", "name/cos:ListMultipartUploads", "name/cos:ListParts", "name/cos:UploadPart", "name/cos:CompleteMultipartUpload"};
|
|
||||||
config.put("allowActions", allowActions);
|
|
||||||
|
|
||||||
Response response = CosStsClient.getCredential(config);
|
|
||||||
return R.ok(response);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("getTempKey error", e);
|
|
||||||
throw new IllegalArgumentException("no valid secret !");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,95 +0,0 @@
|
|||||||
package com.starry.common.utils.file;
|
|
||||||
|
|
||||||
|
|
||||||
import com.qcloud.cos.COSClient;
|
|
||||||
import com.qcloud.cos.model.PutObjectRequest;
|
|
||||||
import com.qcloud.cos.model.UploadResult;
|
|
||||||
import com.qcloud.cos.transfer.TransferManager;
|
|
||||||
import com.qcloud.cos.transfer.Upload;
|
|
||||||
import com.starry.common.config.CosConfig;
|
|
||||||
import com.starry.common.utils.SpringUtils;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.apache.commons.io.FilenameUtils;
|
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.concurrent.ArrayBlockingQueue;
|
|
||||||
import java.util.concurrent.ThreadPoolExecutor;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author admin
|
|
||||||
* 腾讯云COS文件上传工具类
|
|
||||||
* @since 2022/10/31
|
|
||||||
*/
|
|
||||||
@Slf4j
|
|
||||||
public class CosClientUtils {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取配置信息
|
|
||||||
*/
|
|
||||||
private static final CosConfig cosConfig = SpringUtils.getBean(CosConfig.class);
|
|
||||||
|
|
||||||
public static String upload(MultipartFile file, String dir) throws Exception {
|
|
||||||
String originalFilename = file.getOriginalFilename();
|
|
||||||
// 文件名
|
|
||||||
String name = FilenameUtils.getBaseName(originalFilename) + "_" + System.currentTimeMillis() + originalFilename.substring(originalFilename.lastIndexOf("."));
|
|
||||||
// 目录
|
|
||||||
String folderName = cosConfig.getFolderPrefix() + "/" + dir + "/";
|
|
||||||
String key = folderName + name;
|
|
||||||
File localFile = null;
|
|
||||||
try {
|
|
||||||
localFile = transferToFile(file);
|
|
||||||
String filePath = uploadFileToCos(localFile, key);
|
|
||||||
log.info("upload COS successful: {}", filePath);
|
|
||||||
return filePath;
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new Exception("文件上传失败");
|
|
||||||
} finally {
|
|
||||||
localFile.delete();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 用缓冲区来创建临时文件
|
|
||||||
* 使用 MultipartFile.transferTo()
|
|
||||||
*
|
|
||||||
* @param multipartFile
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private static File transferToFile(MultipartFile multipartFile) throws IOException {
|
|
||||||
String originalFilename = multipartFile.getOriginalFilename();
|
|
||||||
String prefix = originalFilename.split("\\.")[0];
|
|
||||||
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
|
|
||||||
File file = File.createTempFile(prefix, suffix);
|
|
||||||
multipartFile.transferTo(file);
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 上传文件到COS
|
|
||||||
*
|
|
||||||
* @param localFile
|
|
||||||
* @param key
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private static String uploadFileToCos(File localFile, String key) throws InterruptedException {
|
|
||||||
PutObjectRequest putObjectRequest = new PutObjectRequest(cosConfig.getBucketName(), key, localFile);
|
|
||||||
// 获取连接
|
|
||||||
COSClient cosClient = cosConfig.getCosClient();
|
|
||||||
// 创建线程池
|
|
||||||
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(8, 16,
|
|
||||||
4, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10), new ThreadPoolExecutor.AbortPolicy());
|
|
||||||
// 传入一个threadPool, 若不传入线程池, 默认TransferManager中会生成一个单线程的线程池
|
|
||||||
TransferManager transferManager = new TransferManager(cosClient, threadPool);
|
|
||||||
// 返回一个异步结果Upload, 可同步的调用waitForUploadResult等待upload结束, 成功返回UploadResult, 失败抛出异常
|
|
||||||
Upload upload = transferManager.upload(putObjectRequest);
|
|
||||||
UploadResult uploadResult = upload.waitForUploadResult();
|
|
||||||
transferManager.shutdownNow();
|
|
||||||
cosClient.shutdown();
|
|
||||||
String filePath = cosConfig.getBaseUrl() + uploadResult.getKey();
|
|
||||||
return filePath;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
37
pom.xml
37
pom.xml
@@ -20,6 +20,7 @@
|
|||||||
<module>play-admin</module>
|
<module>play-admin</module>
|
||||||
<module>play-common</module>
|
<module>play-common</module>
|
||||||
<module>play-generator</module>
|
<module>play-generator</module>
|
||||||
|
<module>play-oss</module>
|
||||||
|
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
@@ -50,12 +51,18 @@
|
|||||||
<easyexcel.version>2.2.11</easyexcel.version>
|
<easyexcel.version>2.2.11</easyexcel.version>
|
||||||
<!-- weichat-->
|
<!-- weichat-->
|
||||||
<weixin-java.version>4.6.0</weixin-java.version>
|
<weixin-java.version>4.6.0</weixin-java.version>
|
||||||
|
|
||||||
|
|
||||||
<!--腾讯云 COS 对象存储-->
|
<!--腾讯云 COS 对象存储-->
|
||||||
<cos-version>5.6.205</cos-version>
|
<cos-version>5.6.205</cos-version>
|
||||||
|
<!--阿里云 COS 对象存储-->
|
||||||
|
<aliyun-oss.version>3.17.4</aliyun-oss.version>
|
||||||
<!--腾讯云 COS 临时密钥-->
|
<!--腾讯云 COS 临时密钥-->
|
||||||
<cos_sts-version>3.1.1</cos_sts-version>
|
<cos_sts-version>3.1.1</cos_sts-version>
|
||||||
<!-- gson -->
|
<!-- gson -->
|
||||||
<gson-version>2.10.1</gson-version>
|
<gson-version>2.10.1</gson-version>
|
||||||
|
<redisson.version>3.24.3</redisson.version>
|
||||||
|
<lock4j.version>2.2.5</lock4j.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<!-- 依赖声明 -->
|
<!-- 依赖声明 -->
|
||||||
@@ -127,12 +134,7 @@
|
|||||||
<artifactId>easyexcel</artifactId>
|
<artifactId>easyexcel</artifactId>
|
||||||
<version>${easyexcel.version}</version>
|
<version>${easyexcel.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!--腾讯云 COS 对象存储-->
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.qcloud</groupId>
|
|
||||||
<artifactId>cos_api</artifactId>
|
|
||||||
<version>${cos-version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.code.gson</groupId>
|
<groupId>com.google.code.gson</groupId>
|
||||||
<artifactId>gson</artifactId>
|
<artifactId>gson</artifactId>
|
||||||
@@ -145,12 +147,6 @@
|
|||||||
<version>1.9.1</version>
|
<version>1.9.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!--腾讯云 COS 临时密钥-->
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.qcloud</groupId>
|
|
||||||
<artifactId>cos-sts_api</artifactId>
|
|
||||||
<version>${cos_sts-version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!--通用模块-->
|
<!--通用模块-->
|
||||||
<dependency>
|
<dependency>
|
||||||
@@ -170,6 +166,11 @@
|
|||||||
<artifactId>play-admin</artifactId>
|
<artifactId>play-admin</artifactId>
|
||||||
<version>1.0</version>
|
<version>1.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.aliyun.oss</groupId>
|
||||||
|
<artifactId>aliyun-sdk-oss</artifactId>
|
||||||
|
<version>${aliyun-oss.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!--weixin-java-common-->
|
<!--weixin-java-common-->
|
||||||
<dependency>
|
<dependency>
|
||||||
@@ -196,6 +197,18 @@
|
|||||||
<version>${weixin-java.version}</version>
|
<version>${weixin-java.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.redisson</groupId>
|
||||||
|
<artifactId>redisson-spring-boot-starter</artifactId>
|
||||||
|
<version>${redisson.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>lock4j-redisson-spring-boot-starter</artifactId>
|
||||||
|
<version>${lock4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
|
|||||||
Reference in New Issue
Block a user