腾讯云对象存储-后台-上传
This commit is contained in:
@@ -61,6 +61,12 @@
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-context</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.qcloud</groupId>
|
||||
<artifactId>cos_api</artifactId>
|
||||
<version>${tencentcloud-cos-sdk-java.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
|
@@ -92,6 +92,7 @@
|
||||
<sharding-jdbc-core.version>4.1.1</sharding-jdbc-core.version>
|
||||
<commons-lang.version>2.6</commons-lang.version>
|
||||
<tencentcloud-sdk-java.version>3.1.781</tencentcloud-sdk-java.version>
|
||||
<tencentcloud-cos-sdk-java.version>5.6.181</tencentcloud-cos-sdk-java.version>
|
||||
<rocketmq-spring-boot.version>2.2.3</rocketmq-spring-boot.version>
|
||||
<kaptcha.version>2.3.2</kaptcha.version>
|
||||
<hippo4j-core.version>1.5.0</hippo4j-core.version>
|
||||
@@ -540,6 +541,14 @@
|
||||
<artifactId>sa-token-spring-boot-starter</artifactId>
|
||||
<version>${sa-token.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/com.qcloud/cos_api -->
|
||||
<dependency>
|
||||
<groupId>com.qcloud</groupId>
|
||||
<artifactId>cos_api</artifactId>
|
||||
<version>${tencentcloud-cos-sdk-java.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
@@ -0,0 +1,41 @@
|
||||
package com.accompany.admin.controller;
|
||||
|
||||
import com.accompany.business.util.ReplaceDomainUtil;
|
||||
import com.accompany.common.result.BusiResult;
|
||||
import com.accompany.common.tencent.cos.TencentCosUploadService;
|
||||
import lombok.SneakyThrows;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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 org.springframework.web.multipart.MultipartHttpServletRequest;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/admin/tencent/cos")
|
||||
public class TencentCosUploadController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private TencentCosUploadService uploadService;
|
||||
|
||||
@SneakyThrows
|
||||
@PostMapping("uploadPatch")
|
||||
public BusiResult<List<String>> uploadPatch(HttpServletRequest request) {
|
||||
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
||||
Map<String, MultipartFile> multipartFileMap = multipartRequest.getFileMap();
|
||||
List<String> filePathList = new ArrayList<>();
|
||||
for (Map.Entry<String, MultipartFile> entry : multipartFileMap.entrySet()) {
|
||||
MultipartFile multipartFile = entry.getValue();
|
||||
String fileName = multipartFile.getOriginalFilename();
|
||||
String fileUrl = uploadService.uploadByStream(multipartFile.getInputStream(), fileName);
|
||||
filePathList.add(ReplaceDomainUtil.handler(fileUrl));
|
||||
}
|
||||
return BusiResult.success(filePathList);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,33 @@
|
||||
package com.accompany.common.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author: liaozetao
|
||||
* @date: 2023/8/14 11:43
|
||||
* @description:
|
||||
*/
|
||||
@Data
|
||||
@RefreshScope
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "tencent.cos")
|
||||
public class TencentCosConfig {
|
||||
|
||||
private String secretId;
|
||||
|
||||
private String secretKey;
|
||||
|
||||
private String appId;
|
||||
|
||||
private String region;
|
||||
|
||||
private String bucket;
|
||||
|
||||
private String urlPrefix;
|
||||
|
||||
}
|
@@ -0,0 +1,41 @@
|
||||
package com.accompany.common.tencent.cos;
|
||||
|
||||
import com.accompany.common.config.TencentCosConfig;
|
||||
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.region.Region;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author: liaozetao
|
||||
* @date: 2023/8/14 11:55
|
||||
* @description:
|
||||
*/
|
||||
@Configuration
|
||||
public class TencentCosConfiguration {
|
||||
|
||||
@Autowired
|
||||
private TencentCosConfig config;
|
||||
|
||||
@Bean
|
||||
public COSClient cosClient(){
|
||||
// 1 初始化用户身份信息(secretId, secretKey)。
|
||||
// SECRETID 和 SECRETKEY 请登录访问管理控制台 https://console.cloud.tencent.com/cam/capi 进行查看和管理
|
||||
COSCredentials cred = new BasicCOSCredentials(config.getSecretId(), config.getSecretKey());
|
||||
// 2 设置 bucket 的地域, COS 地域的简称请参见 https://cloud.tencent.com/document/product/436/6224
|
||||
// clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
|
||||
Region region = new Region(config.getRegion());
|
||||
ClientConfig clientConfig = new ClientConfig(region);
|
||||
// 这里建议设置使用 https 协议
|
||||
// 从 5.6.54 版本开始,默认使用了 https
|
||||
clientConfig.setHttpProtocol(HttpProtocol.https);
|
||||
// 3 生成 cos 客户端。
|
||||
return new COSClient(cred, clientConfig);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
package com.accompany.common.tencent.cos;
|
||||
|
||||
import com.accompany.common.config.TencentCosConfig;
|
||||
import com.qcloud.cos.COSClient;
|
||||
import com.qcloud.cos.model.PutObjectRequest;
|
||||
import com.qcloud.cos.model.PutObjectResult;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class TencentCosUploadService {
|
||||
|
||||
@Autowired
|
||||
private TencentCosConfig config;
|
||||
@Autowired
|
||||
private COSClient client;
|
||||
|
||||
@SneakyThrows
|
||||
public String uploadByStream(InputStream is, String fileName) {
|
||||
PutObjectRequest request = new PutObjectRequest(config.getBucket(), fileName, is, null);
|
||||
PutObjectResult result = client.putObject(request);
|
||||
log.info("[腾讯云cos] {} 上传成功, requestId {}", fileName, result.getRequestId());
|
||||
return config.getUrlPrefix() + "/" + fileName;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user