diff --git a/bookshelfplus/pom.xml b/bookshelfplus/pom.xml
index 282d078..90c83c7 100644
--- a/bookshelfplus/pom.xml
+++ b/bookshelfplus/pom.xml
@@ -103,6 +103,12 @@
provided
+
+
+ com.qcloud
+ cos_api
+ 5.6.69
+
diff --git a/bookshelfplus/src/main/java/plus/bookshelf/App.java b/bookshelfplus/src/main/java/plus/bookshelf/App.java
index 0c24468..a883918 100644
--- a/bookshelfplus/src/main/java/plus/bookshelf/App.java
+++ b/bookshelfplus/src/main/java/plus/bookshelf/App.java
@@ -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);
+ }
}
diff --git a/bookshelfplus/src/main/java/plus/bookshelf/Common/TencentCloud/COS/CosProperties.java b/bookshelfplus/src/main/java/plus/bookshelf/Common/TencentCloud/COS/CosProperties.java
new file mode 100644
index 0000000..69afaa4
--- /dev/null
+++ b/bookshelfplus/src/main/java/plus/bookshelf/Common/TencentCloud/COS/CosProperties.java
@@ -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;
+}
diff --git a/bookshelfplus/src/main/java/plus/bookshelf/Common/TencentCloud/COS/GeneratePresignatureUrl.java b/bookshelfplus/src/main/java/plus/bookshelf/Common/TencentCloud/COS/GeneratePresignatureUrl.java
new file mode 100644
index 0000000..fc858f7
--- /dev/null
+++ b/bookshelfplus/src/main/java/plus/bookshelf/Common/TencentCloud/COS/GeneratePresignatureUrl.java
@@ -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 params = new HashMap();
+ params.put("bookshelf", "plus");
+
+ // 填写本次请求的头部,需与实际请求相同,能够防止用户篡改此签名的 HTTP 请求的头部
+ Map headers = new HashMap();
+ // 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();
+ }
+}
\ No newline at end of file
diff --git a/bookshelfplus/src/main/resources/cos.properties b/bookshelfplus/src/main/resources/cos.properties
new file mode 100644
index 0000000..b4fe5fd
--- /dev/null
+++ b/bookshelfplus/src/main/resources/cos.properties
@@ -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
\ No newline at end of file