第一步:添加pom.xml文件
<!-- 腾讯云cos存储 -->
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos_api</artifactId>
<version>5.6.89</version>
</dependency>
第二步:配置yml文件
#腾讯云COS存储
tencentCosUtil:
#开发者拥有的项目身份识别 ID,用于身份认证,可在 API 密钥管理 页面获取
secretId: AK************************Op
#开发者拥有的项目身份密钥,可在 API 密钥管理 页面获取
secretKey: HMH0*****************zSn0GQT
#存储桶名称格式,用户在使用 API、SDK 时,需要按照此格式填写存储桶名称
bucketName: ******-131******84
#存储桶配置管理中访问域名的URL(格式:https://bucketName.cos.所在地域(例如:ap-nanjing).myqcloud.com/)
accessUrl: https://t*****bo*-131**4284.cos.**-n****ng.myqcloud.com/
第三步:创建COS工具类
@Component
public class TencentCosUtil {
private static String secretId;
private static String secretKey;
private static String bucketName;
private static String accessUrl;
@Value("${tencentCosUtil.secretId}")
public void setSecretId(String secretId) {
this.secretId = secretId;
}
@Value("${tencentCosUtil.secretKey}")
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
@Value("${tencentCosUtil.bucketName}")
public void setBucketName(String bucketName) {
this.bucketName = bucketName;
}
@Value("${tencentCosUtil.accessUrl}")
public void setAccessUrl(String accessUrl) {
this.accessUrl = accessUrl;
}
/**
* 上传文件
*
* @param fileMul
* @return
* @throws Exception
*/
public static String uploadFile(MultipartFile fileMul) throws Exception {
//临时文件路径(自定义)
String temporaryFile = "image";
//效验临时文件是否存在
File localFile = new File(temporaryFile);
if (!localFile.exists()) {
//临时文件不存在,创造临时文件
localFile.mkdirs();
}
//创造临时文件名称
String fileName = fileMul.getOriginalFilename();
String name = "/" + RandomStringUtils.randomNumeric(6) + System.currentTimeMillis() + System.nanoTime() + RandomStringUtils.randomNumeric(6) + fileName.substring(fileName.lastIndexOf('.'));
//创造临时文件图片
temporaryFile = temporaryFile + name;
FileOutputStream fos;
fos = new FileOutputStream(temporaryFile);
fos.write(fileMul.getBytes());
fos.flush();
fos.close();
// 使用COS
File file = new File(temporaryFile);
// 1 初始化用户身份信息(secretId, secretKey)。
// SECRETID和SECRETKEY请登录访问管理控制台 https://console.cloud.tencent.com/cam/capi 进行查看和管理
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
// 2 设置 bucket 的地域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
// clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
Region region = new Region("ap-nanjing");
ClientConfig clientConfig = new ClientConfig(region);
// 这里建议设置使用 https 协议
// 从 5.6.54 版本开始,默认使用了 https
clientConfig.setHttpProtocol(HttpProtocol.https);
// 3 生成 cos 客户端。
COSClient cosClient = new COSClient(cred, clientConfig);
//格式化时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, temporaryFile + sdf.format(new Date()) + name, file);
cosClient.putObject(putObjectRequest);
String saveUrl = accessUrl + putObjectRequest.getKey();
// 删除用户上传临时文件
File localImgFile = new File(temporaryFile);
localImgFile.delete();
return saveUrl;
}
第四步:暴露给前端调用,返回图片地址。
@PostMapping("/updateFile")
public ApiRestResponse updateFile(MultipartFile file) throws Exception {
String s = TencentCosUtil.uploadFile(file);
return ApiRestResponse.success(s);
}