原以为对象存储很难 毕竟之前我用了下 发现完全不会
今天静下心来研究了下 发现其实挺简单的
直接搜索
点击这个
这个秘钥很重要 需要保存好
点击配置
加上这三个 一共是五个数据 我们都是要用到的
导入依赖
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos_api</artifactId>
<version>5.6.24</version>
</dependency>
配置文件
#API密钥secretId
spring.qcloud.secretId=
#API密钥secretKey
spring.qcloud.secretKey=
#存储桶所属地域
spring.qcloud.region=ap-nanjing
#存储桶空间名称
spring.qcloud.bucketName=
#存储桶访问域名
spring.qcloud.url=
#上传文件前缀路径(eg:/images/)
spring.qcloud.prefix=/images/
若是这样配置 就会在相应了目录下 生成这样的文件夹
上传文件的工具类
/**
* 腾讯云对象存储工具类
*/
public class QCloudCosUtils {
//API密钥secretId
private String secretId;
//API密钥secretKey
private String secretKey;
//存储桶所属地域
private String region;
//存储桶空间名称
private String bucketName;
//存储桶访问域名
private String url;
//上传文件前缀路径(eg:/images/)
private String prefix;
public String getSecretId() {
return secretId;
}
public void setSecretId(String secretId) {
this.secretId = secretId;
}
public String getSecretKey() {
return secretKey;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
public String getBucketName() {
return bucketName;
}
public void setBucketName(String bucketName) {
this.bucketName = bucketName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
/**
* 上传File类型的文件
* @param file
* @return 上传文件在存储桶的链接
*/
public String upload(File file) {
//生成唯一文件名
String newFileName = generateUniqueName(file.getName());
//文件在存储桶中的key
String key = prefix+newFileName;
//声明客户端
COSClient cosClient=null;
try {
//初始化用户身份信息(secretId,secretKey)
COSCredentials cosCredentials = new BasicCOSCredentials(secretId, secretKey);
//设置bucket的区域
ClientConfig clientConfig = new ClientConfig(new Region(region));
//生成cos客户端
cosClient = new COSClient(cosCredentials, clientConfig);
//创建存储对象的请求
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
//执行上传并返回结果信息
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
return url+key;
} catch (CosClientException e) {
e.printStackTrace();
} finally {
cosClient.shutdown();
}
return null;
}
/**
* upload()重载方法
* @param multipartFile
* @return 上传文件在存储桶的链接
*/
public String upload(MultipartFile multipartFile) {
System.out.println(multipartFile);
//生成唯一文件名
String newFileName = generateUniqueName(multipartFile.getOriginalFilename());
//文件在存储桶中的key
String key = prefix+newFileName;
//声明客户端
COSClient cosClient = null;
//准备将MultipartFile类型转为File类型
File file = null;
try {
//生成临时文件
file = File.createTempFile("temp",null);
//将MultipartFile类型转为File类型
multipartFile.transferTo(file);
//初始化用户身份信息(secretId,secretKey)
COSCredentials cosCredentials = new BasicCOSCredentials(secretId, secretKey);
//设置bucket的区域
ClientConfig clientConfig = new ClientConfig(new Region(region));
//生成cos客户端
cosClient = new COSClient(cosCredentials, clientConfig);
//创建存储对象的请求
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
//执行上传并返回结果信息
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
return url+key;
} catch (IOException e) {
e.printStackTrace();
} finally {
cosClient.shutdown();
}
return null;
}
/**
* 根据UUID生成唯一文件名
* @param originalName
* @return
*/
public String generateUniqueName(String originalName) {
return UUID.randomUUID() + originalName.substring(originalName.lastIndexOf("."));
}
}
配置注入
/**
* 配置注入
*/
@Configuration
public class QCloudCosUtilsConfig {
@ConfigurationProperties(prefix = "spring.qcloud")
@Bean
public QCloudCosUtils qcloudCosUtils() {
return new QCloudCosUtils();
}
}
接口类
/**
*上唇文件接口
*/
public interface FileUploadService {
/**
* 处理浏览器文件上传请求
* @param multipartFile
* @return
*/
String upload(MultipartFile multipartFile);
/**
* 处理普通文件上传
* @param file
* @return
*/
String upload(File file);
}
接口实现类
@Service
public class FileUploadServiceImpl implements FileUploadService {
@Autowired
private QCloudCosUtils qCloudCosUtils;
@Override
public String upload(MultipartFile multipartFile) {
return qCloudCosUtils.upload(multipartFile);
}
@Override
public String upload(File file) {
return qCloudCosUtils.upload(file);
}
}
直接controller调用就好啦
如果需要回显 service会返回对应的图片路径 直接就能访问了