66import java .net .ProtocolException ;
77import java .net .URL ;
88import java .util .Date ;
9+ import java .util .HashMap ;
10+ import java .util .Map ;
911
1012import com .qcloud .cos .COSClient ;
1113import com .qcloud .cos .ClientConfig ;
@@ -45,6 +47,12 @@ public static void GenerateSimplePresignedDownloadUrl() {
4547 Date expirationDate = new Date (System .currentTimeMillis () + 30 * 60 * 1000 );
4648 req .setExpiration (expirationDate );
4749
50+ // 填写本次请求的参数
51+ req .addRequestParameter ("param1" , "value1" );
52+
53+ // 填写本次请求的头部。Host 头部会自动补全,不需要填写
54+ req .putCustomRequestHeader ("header1" , "value1" );
55+
4856 URL url = cosclient .generatePresignedUrl (req );
4957 System .out .println (url .toString ());
5058
@@ -83,6 +91,13 @@ public static void GeneratePresignedDownloadUrlWithOverrideResponseHeader() {
8391 // 这里设置签名在半个小时后过期
8492 Date expirationDate = new Date (System .currentTimeMillis () + 30 * 60 * 1000 );
8593 req .setExpiration (expirationDate );
94+
95+ // 填写本次请求的参数
96+ req .addRequestParameter ("param1" , "value1" );
97+
98+ // 填写本次请求的头部。Host 头部会自动补全,不需要填写
99+ req .putCustomRequestHeader ("header1" , "value1" );
100+
86101 URL url = cosclient .generatePresignedUrl (req );
87102
88103 System .out .println (url .toString ());
@@ -114,17 +129,22 @@ public static void GeneratePresignedDownloadUrlAnonymous() {
114129 // 生成预签名的上传连接
115130 public static void GeneratePresignedUploadUrl () {
116131 // 1 初始化用户身份信息(secretId, secretKey)
117- COSCredentials cred = new BasicCOSCredentials ("AKIDXXXXXXXX " , "1A2Z3YYYYYYYYYY " );
132+ COSCredentials cred = new BasicCOSCredentials ("AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxx " , "******************************** " );
118133 // 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
119- ClientConfig clientConfig = new ClientConfig (new Region ("ap-beijing-1 " ));
134+ ClientConfig clientConfig = new ClientConfig (new Region ("ap-shanghai " ));
120135 // 3 生成cos客户端
121136 COSClient cosclient = new COSClient (cred , clientConfig );
122137 // bucket名需包含appid
123138 String bucketName = "mybucket-1251668577" ;
124139
125140 String key = "aaa.txt" ;
126141 Date expirationTime = new Date (System .currentTimeMillis () + 30 * 60 * 1000 );
127- URL url = cosclient .generatePresignedUrl (bucketName , key , expirationTime , HttpMethodName .PUT );
142+ // 填写本次请求的 header。Host 头部会自动补全,只需填入其他头部
143+ Map <String , String > headers = new HashMap <String ,String >();
144+ // 填写本次请求的 params。
145+ Map <String , String > params = new HashMap <String ,String >();
146+
147+ URL url = cosclient .generatePresignedUrl (bucketName , key , expirationTime , HttpMethodName .PUT , headers , params );
128148 try {
129149 HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
130150 connection .setDoOutput (true );
@@ -145,4 +165,7 @@ public static void GeneratePresignedUploadUrl() {
145165 }
146166
147167
168+ public static void main (String [] args ) {
169+ GeneratePresignedUploadUrl ();
170+ }
148171}
0 commit comments