package com.ruoyi.web.core.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.http.HttpProtocol;
|
import com.qcloud.cos.region.Region;
|
import lombok.Data;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Configuration;
|
|
@Configuration
|
@Data
|
public class FileUploaderConfig {
|
|
/**
|
* COS的SecretId
|
*/
|
@Value("${cos.client.accessKey}")
|
private String secretId;
|
/**
|
* COS的SecretKey
|
*/
|
@Value("${cos.client.secretKey}")
|
private String secretKey;
|
/**
|
* 文件上传后访问路径的根路径,后面要最佳文件名字与类型
|
*/
|
@Value("${cos.client.rootSrc}")
|
private String rootSrc;
|
/**
|
* 上传的存储桶的地域
|
*/
|
@Value("${cos.client.bucketAddr}")
|
private String bucketAddr;
|
/**
|
* 存储桶的名字,是自己在存储空间自己创建的,我创建的名字是:qq-test-1303******
|
*/
|
@Value("${cos.client.bucket}")
|
private String bucketName;
|
/**
|
* 文件存放位置
|
*/
|
@Value("${cos.client.location}")
|
private String location;
|
|
@Value("${file.url.prefix}")
|
private String fileUrlPrefix;
|
|
|
@Bean
|
public COSClient cosClient() {
|
// 1 初始化用户身份信息(secretId, secretKey)。
|
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
|
// 2.1 设置存储桶的地域(上文获得)
|
Region region = new Region(bucketAddr);
|
ClientConfig clientConfig = new ClientConfig(region);
|
// 2.2 使用https协议传输
|
clientConfig.setHttpProtocol(HttpProtocol.https);
|
// 生成 cos 客户端
|
return new COSClient(cred, clientConfig);
|
}
|
|
|
}
|