1
0
mirror of https://gitee.com/bookshelfplus/bookshelfplus synced 2025-09-14 20:41:38 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee

添加腾讯云COS生成预授权URL功能

This commit is contained in:
2022-03-13 00:00:38 +08:00
parent 5f5c318da7
commit 1b78102d61
5 changed files with 154 additions and 0 deletions

View File

@@ -1,10 +1,13 @@
package plus.bookshelf;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import plus.bookshelf.Common.Enum.plus.bookshelf.TencentCloud.COS.CosProperties;
import plus.bookshelf.Common.TencentCloud.COS.GeneratePresignatureUrl;
/**
* Hello world!
@@ -25,4 +28,13 @@ public class App {
public String Home() {
return "首页";
}
@Autowired
CosProperties cosProperties;
@RequestMapping("/cos")
public String cos() {
GeneratePresignatureUrl generatePresignatureUrl = new GeneratePresignatureUrl(cosProperties);
return generatePresignatureUrl.getUrl("mydemo.jpg", 1);
}
}

View File

@@ -0,0 +1,28 @@
package plus.bookshelf.Common.Enum.plus.bookshelf.TencentCloud.COS;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "cos")
@PropertySource(value = "cos.properties")
public class CosProperties {
// @Value("${cos.accessKey}")
private String accessKey;
// @Value("${cos.secretKey}")
private String secretKey;
// @Value("${cos.regionName}")
private String regionName;
// @Value("${cos.bucketName}")
private String bucketName;
// @Value("${cos.keyName}")
private String keyName;
}

View File

@@ -0,0 +1,93 @@
package plus.bookshelf.Common.TencentCloud.COS;
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.http.HttpProtocol;
import com.qcloud.cos.region.Region;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class GeneratePresignatureUrl {
plus.bookshelf.Common.Enum.plus.bookshelf.TencentCloud.COS.CosProperties cosProperties;
public GeneratePresignatureUrl(plus.bookshelf.Common.Enum.plus.bookshelf.TencentCloud.COS.CosProperties cosProperties) {
this.cosProperties = cosProperties;
}
// refer: https://cloud.tencent.com/document/product/436/35217#.E5.88.9B.E5.BB.BA-cosclient
// 创建 COSClient 实例,这个实例用来后续调用请求
COSClient createCOSClient() {
// 设置用户身份信息。
// SECRETID 和 SECRETKEY 请登录访问管理控制台 https://console.cloud.tencent.com/cam/capi 进行查看和管理
String secretId = cosProperties.getAccessKey();
String secretKey = cosProperties.getSecretKey();
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
// ClientConfig 中包含了后续请求 COS 的客户端设置:
ClientConfig clientConfig = new ClientConfig();
// 设置 bucket 的地域
// COS_REGION 请参照 https://cloud.tencent.com/document/product/436/6224
clientConfig.setRegion(new Region(cosProperties.getRegionName()));
// 设置请求协议, http 或者 https
// 5.6.53 及更低的版本,建议设置使用 https 协议
// 5.6.54 及更高版本,默认使用了 https
clientConfig.setHttpProtocol(HttpProtocol.https);
// 以下的设置,是可选的:
// // 设置 socket 读取超时,默认 30s
// clientConfig.setSocketTimeout(30*1000);
// // 设置建立连接超时,默认 30s
// clientConfig.setConnectionTimeout(30*1000);
//
// // 如果需要的话,设置 http 代理ip 以及 port
// clientConfig.setHttpProxyIp("httpProxyIp");
// clientConfig.setHttpProxyPort(80);
// 生成 cos 客户端。
return new COSClient(cred, clientConfig);
}
public String getUrl(String objectKey, Integer expireMinute) {
// 调用 COS 接口之前必须保证本进程存在一个 COSClient 实例,如果没有则创建
// 详细代码参见本页:简单操作 -> 创建 COSClient
COSClient cosClient = createCOSClient();
// 存储桶的命名格式为 BucketName-APPID此处填写的存储桶名称必须为此格式
String bucketName = cosProperties.getBucketName();
// 对象键(Key)是对象在存储桶中的唯一标识。详情请参见 [对象键](https://cloud.tencent.com/document/product/436/13324)
String key = objectKey;
// 设置签名过期时间(可选), 若未进行设置则默认使用 ClientConfig 中的签名过期时间(1小时)
// 这里设置签名在半个小时后过期
// Date expirationDate = new Date(System.currentTimeMillis() + 30 * 60 * 1000);
Date expirationDate = new Date(System.currentTimeMillis() + expireMinute * 60 * 1000);
// 填写本次请求的参数,需与实际请求相同,能够防止用户篡改此签名的 HTTP 请求的参数
Map<String, String> params = new HashMap<String, String>();
params.put("bookshelf", "plus");
// 填写本次请求的头部,需与实际请求相同,能够防止用户篡改此签名的 HTTP 请求的头部
Map<String, String> headers = new HashMap<String, String>();
// headers.put("header1", "value1");
// 请求的 HTTP 方法,上传请求用 PUT下载请求用 GET删除请求用 DELETE
HttpMethodName method = HttpMethodName.GET;
URL url = cosClient.generatePresignedUrl(bucketName, key, expirationDate, method, headers, params);
System.out.println(url.toString());
// 确认本进程不再使用 cosClient 实例之后,关闭之
cosClient.shutdown();
return url.toString();
}
}

View File

@@ -0,0 +1,15 @@
# bookshelfplus-bookdownloaduser
# +XM{e@]t
# SecretId: AKIDaz80bw0nJYWEgqjLgkY4JHzPYQ2NSGn4
# SecretKey: 1lEQKxJPFo66q54lCNGsDH4brqYzA5j6
# SecretId
cos.accessKey=AKIDaz80bw0nJYWEgqjLgkY4JHzPYQ2NSGn4
# SecretKey
cos.secretKey=1lEQKxJPFo66q54lCNGsDH4brqYzA5j6
# 地域名
cos.regionName=ap-shanghai
# 存储桶名称
cos.bucketName=bookshelfplus-ebooks-1302260381
# 文件夹名称(可自定义)
cos.keyName=images