使用
package com.ruoyi;
import java.io.File;
public class mytest {
private static String secretId = "AKID2oPDo111111111111dMp53mHfoYZGw";
private static String secretKey = "e6JTP5e222222222222GvhHYBl7C5g05";
private static String bucketName = "mht-12566666666";
private static String regionStr = "ap-beijing";
private static String domain = "https://mht-12566666666.cos.ap-beijing.myqcloud.com";
public static void main(String[] args) {
CosUtil cosUtil = new CosUtil(secretId, secretKey, bucketName, regionStr, 30L * 60L, domain);
File file = new File("C:\\Users\\13301\\Desktop\\微信截图_20220926222441.png");
Boolean put = cosUtil.put("20220926222441.png", file);
System.out.println(put ? "上传成功" : "上传失败");
//获取带签名的有有效期的图片链接,如果存储桶的权限为私有读,那么必须使用这个链接才能直接访问图片
String sourceUrl = cosUtil.getSourceUrl("20220926222441.png");
System.out.println(sourceUrl);
//获取不带签名的图片链接,只有存储桶的权限为公有读,才可以直接使用这个链接访问图片
String linkUrl = cosUtil.getLinkUrl("20220926222441.png");
System.out.println(linkUrl);
}
}
结果
上传成功
https://mht-12566666666.cos.ap-beijing.myqcloud.com/20220926222441.png?sign=q-sign-algorithm%3Dsha1%26q-ak%3DAKID2oPDoCYwmM0sb9nREwPdMp53mHfoYZGw%26q-sign-time%3D1664205380%3B1664207180%26q-key-time%3D1664205380%3B1664207180%26q-header-list%3Dhost%26q-url-param-list%3D%26q-signature%3D260170f52383e7e8ff2af5ece1edd03809ea0cc6
https://mht-12566666666.cos.ap-beijing.myqcloud.com/20220926222441.png
引入依赖
<!-- 腾讯云对象存储 SDK -->
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos_api</artifactId>
<version>5.6.105</version>
</dependency>
工具类
package com.ruoyi;
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.HttpMethodName;
import com.qcloud.cos.model.GeneratePresignedUrlRequest;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.region.Region;
import java.io.File;
import java.util.Date;
/**
* 腾讯云对象存储工具类
*
* @author DaenMax
*/
public class CosUtil {
private static String secretId;
private static String secretKey;
private static String bucketName;
private static String regionStr;
private static COSClient client;
private static Long expireTime;
private static String domain;
/**
* @param secretId
* @param secretKey
* @param bucketName 存储桶名
* @param regionStr 所属地域,例如 ap-beijing
* @param expireTime 带有sign的图片链接的有效期,单位秒数
* @param domain 访问域名,例如 https://mht-1266634666.cos.ap-beijing.myqcloud.com
*/
public CosUtil(String secretId, String secretKey, String bucketName, String regionStr, Long expireTime, String domain) {
CosUtil.secretId = secretId;
CosUtil.secretKey = secretKey;
CosUtil.bucketName = bucketName;
CosUtil.regionStr = regionStr;
CosUtil.expireTime = expireTime;
CosUtil.domain = domain;
COSCredentials cred = new BasicCOSCredentials(CosUtil.secretId, CosUtil.secretKey);
Region region = new Region(CosUtil.regionStr);
ClientConfig clientConfig = new ClientConfig(region);
client = new COSClient(cred, clientConfig);
}
/**
* 上传文件到服务器 如果key重复将被覆盖
*/
public Boolean put(String key, File file) {
try {
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
client.putObject(putObjectRequest);
} catch (Exception e) {
return false;
}
return true;
}
/**
* 获取带权限的下载链接
*/
public String getSourceUrl(String key) {
GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, key, HttpMethodName.GET);
// 设置签名过期时间(可选), 若未进行设置, 则默认使用 ClientConfig 中的签名过期时间(1小时)
Date expirationDate = new Date(System.currentTimeMillis() + expireTime * 1000L);
req.setExpiration(expirationDate);
return client.generatePresignedUrl(req).toString();
}
/**
* 获取不带权限的下载链接
*/
public String getLinkUrl(String key) {
return CosUtil.domain + "/" + key;
}
}
标签:存储,String,demo,CosUtil,cos,private,static,腾讯,com
From: https://www.cnblogs.com/daen/p/16732975.html