mirror of
https://gitee.com/bookshelfplus/bookshelfplus
synced 2025-09-03 23:52:51 +08:00
104 lines
4.9 KiB
Java
104 lines
4.9 KiB
Java
package plus.bookshelf.Common.TencentCloud.COS;
|
||
|
||
import com.qcloud.cos.COSClient;
|
||
import com.qcloud.cos.ClientConfig;
|
||
import com.qcloud.cos.auth.AnonymousCOSCredentials;
|
||
import com.qcloud.cos.auth.BasicCOSCredentials;
|
||
import com.qcloud.cos.auth.COSCredentials;
|
||
import com.qcloud.cos.endpoint.UserSpecifiedEndpointBuilder;
|
||
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;
|
||
}
|
||
|
||
// 生成预签名 URL refer: https://cloud.tencent.com/document/product/436/35217#.E5.88.9B.E5.BB.BA-cosclient
|
||
// 使用 CDN 加速域名 refer: https://intl.cloud.tencent.com/zh/document/product/436/40244
|
||
// 创建 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);
|
||
// // 使用 CDN 加速域名,无需再发送 COS 的认证信息
|
||
// COSCredentials cred = new AnonymousCOSCredentials();
|
||
|
||
// 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.54 及更高版本,默认使用了 https
|
||
clientConfig.setHttpProtocol(HttpProtocol.https);
|
||
|
||
// 设置 CDN 自定义域名
|
||
// get service 请求会使用这个域名,这个域名不能自定义
|
||
String serviceApiEndpoint = "service.cos.myqcloud.com";
|
||
String cdnEndpoint = "dl.bookshelf.plus";
|
||
UserSpecifiedEndpointBuilder endPointBuilder = new UserSpecifiedEndpointBuilder(cdnEndpoint, serviceApiEndpoint);
|
||
clientConfig.setEndpointBuilder(endPointBuilder);
|
||
|
||
// 以下的设置,是可选的:
|
||
|
||
// // 设置 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();
|
||
}
|
||
} |