Skip to content

Commit 00e8030

Browse files
author
markjrzhang
committed
update demo
1 parent 784c791 commit 00e8030

File tree

2 files changed

+76
-48
lines changed

2 files changed

+76
-48
lines changed

src/main/java/com/qcloud/cos/demo/ci/GeneratePlayListDemo.java

Lines changed: 70 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.qcloud.cos.demo.ci;
22

3+
import com.auth0.jwt.JWT;
4+
import com.auth0.jwt.JWTCreator;
5+
import com.auth0.jwt.algorithms.Algorithm;
36
import com.qcloud.cos.COSClient;
7+
import com.qcloud.cos.http.HttpMethodName;
48
import com.qcloud.cos.model.ciModel.common.MediaVod;
59
import com.qcloud.cos.model.ciModel.job.*;
610
import com.qcloud.cos.model.ciModel.job.v2.GetPlayListRequest;
@@ -9,7 +13,14 @@
913
import com.qcloud.cos.utils.Jackson;
1014

1115
import java.io.*;
16+
import java.net.URL;
17+
import java.net.URLEncoder;
1218
import java.nio.charset.StandardCharsets;
19+
import java.time.Instant;
20+
import java.time.temporal.ChronoUnit;
21+
import java.util.Date;
22+
import java.util.HashMap;
23+
import java.util.Map;
1324

1425
/**
1526
* 媒体处理 边转边播接口相关demo
@@ -66,40 +77,70 @@ public static void describeMediaJob(COSClient client) {
6677
System.out.println(Jackson.toJsonString(response));
6778
}
6879

69-
/**
70-
* getPlayList 接口用于获取私有 M3U8 ts 资源的下载授权
71-
*/
80+
private static String appId = "123456789";
81+
private static String bucket = "demo-123456789";
82+
private static String objectKey = "test.m3u8";
83+
private static String expires = "3600";
84+
private static byte[] secret = "YourSecret".getBytes();
85+
7286
public static void getPlayList(COSClient client) {
73-
GetPlayListRequest request = new GetPlayListRequest();
74-
request.setBucketName("demo-1234567890");
75-
request.setObject("output/media/test.m3u8");
76-
request.setExpires("3600");
77-
try {
78-
InputStream response = client.getPlayList(request);
79-
System.out.println(inputStreamToString(response));
80-
} catch (IOException e) {
81-
e.printStackTrace();
82-
}
87+
String bucketName = bucket;
88+
String key = objectKey;
89+
// 设置签名过期时间(可选), 若未进行设置则默认使用 ClientConfig 中的签名过期时间(1小时)
90+
Instant now = Instant.now();
91+
Instant expire = now.plus(Long.parseLong(expires), ChronoUnit.SECONDS);
92+
Date expirationDate = Date.from(expire);
93+
Map<String, String> params = new HashMap<String, String>();
94+
params.put("ci-process", "getplaylist");
95+
params.put("expires", "43200");
96+
Map<String, String> headers = new HashMap<String, String>();
97+
98+
HttpMethodName method = HttpMethodName.GET;
99+
URL url = client.generatePresignedUrl(bucketName, key, expirationDate, method, headers, params);
100+
System.out.println(url.toString());
83101
}
84102

85-
public static String inputStreamToString(InputStream inputStream) throws IOException {
86-
if (inputStream == null) {
87-
return "";
88-
}
103+
public static void getPlayListSimple(COSClient client) throws UnsupportedEncodingException {
104+
String bucketName = bucket;
105+
String key = objectKey;
106+
// 设置签名过期时间(可选), 若未进行设置则默认使用 ClientConfig 中的签名过期时间(1小时)
107+
Instant now = Instant.now();
108+
Instant expire = now.plus(Long.parseLong(expires), ChronoUnit.SECONDS);
109+
Date expirationDate = Date.from(expire);
110+
String token = generateToken(appId, bucket, objectKey, secret, expirationDate);
111+
Map<String, String> params = new HashMap<String, String>();
112+
params.put("ci-process", "getplaylist");
113+
params.put("expires", "43200");
114+
params.put("token-type", "JwtToken");
115+
params.put("token",token);
116+
Map<String, String> headers = new HashMap<String, String>();
89117

90-
StringBuilder stringBuilder = new StringBuilder();
91-
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
92-
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
118+
HttpMethodName method = HttpMethodName.GET;
119+
URL url = client.generatePresignedUrl(bucketName, key, expirationDate, method, headers, params);
120+
System.out.println(url.toString());
121+
}
93122

94-
String line;
95-
while ((line = bufferedReader.readLine()) != null) {
96-
stringBuilder.append(line);
97-
}
123+
public static String generateToken(String appId, String bucketId, String objectKey, byte[] secret, Date expires) throws UnsupportedEncodingException {
124+
Instant now = Instant.now();
125+
String encodedObjectKey;
126+
encodedObjectKey = URLEncoder.encode(objectKey, "UTF-8");
98127

99-
bufferedReader.close();
100-
inputStreamReader.close();
101-
inputStream.close();
128+
Algorithm algorithm = Algorithm.HMAC256(secret);
129+
JWTCreator.Builder builder = JWT.create().withIssuer("client")
130+
.withIssuedAt(Date.from(now))
131+
.withExpiresAt(expires)
132+
.withClaim("Type", "CosCiToken")
133+
.withClaim("AppId", appId)
134+
.withClaim("BucketId", bucketId)
135+
.withClaim("Object", encodedObjectKey)
136+
.withClaim("Issuer", "client")
137+
.withClaim("IssuedTimeStamp", now.getEpochSecond())
138+
.withClaim("ExpireTimeStamp", expires.getTime()/1000)
139+
.withClaim("UsageLimit", 20)
140+
.withClaim("ProtectSchema", "rsa1024")
141+
// .withClaim("PublicKey", "xxx")
142+
.withClaim("ProtectContentKey", 0);
143+
return builder.sign(algorithm);
144+
}
102145

103-
return stringBuilder.toString();
104-
}
105146
}

src/main/java/com/qcloud/cos/demo/ci/LiveTranscodeDemo.java renamed to src/main/java/com/qcloud/cos/demo/ci/generatePrivateM3U8UrlDemo.java

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,29 @@
1212
import java.time.temporal.ChronoUnit;
1313
import java.util.Date;
1414

15-
public class LiveTranscodeDemo {
15+
public class generatePrivateM3U8UrlDemo {
1616
private static String appId = "123456789";
1717
private static String bucket = "demo-123456789";
1818
private static String objectKey = "test.m3u8";
1919
private static String expires = "3600";
2020
private static byte[] secret = "YourSecret".getBytes();
2121

22-
23-
public static void main(String[] args) {
24-
COSClient client = ClientUtils.getTestClient();
25-
String url = generateCosDomainPrivateM3U8Url(client);
26-
System.out.println("Generated url: " + url);
27-
}
28-
29-
public static String generateCosDomainPrivateM3U8Url(COSClient client) {
22+
public static String generateCosDomainPrivateM3U8Url(COSClient client) throws UnsupportedEncodingException {
3023
PrivateM3U8Request request = new PrivateM3U8Request();
3124
request.setBucketName(bucket);
3225
request.setObject(objectKey);
3326
request.setExpires(expires);
3427
request.setTokenType("JwtToken");
35-
String token =generateToken(appId, bucket, objectKey, secret,expires);
28+
String token = generateToken(appId, bucket, objectKey, secret,expires);
3629
request.setToken(token);
3730
return client.generateCosDomainPrivateM3U8Url(request);
3831
}
3932

40-
public static String generateToken(String appId, String bucketId, String objectKey, byte[] secret, String expires) {
41-
33+
public static String generateToken(String appId, String bucketId, String objectKey, byte[] secret, String expires) throws UnsupportedEncodingException {
4234
Instant now = Instant.now();
43-
Instant expire = now.plus(6, ChronoUnit.DAYS);
44-
35+
Instant expire = now.plus(Long.parseLong(expires), ChronoUnit.SECONDS);
4536
String encodedObjectKey;
46-
try {
47-
encodedObjectKey = URLEncoder.encode(objectKey, "UTF-8");
48-
} catch (UnsupportedEncodingException e) {
49-
throw new RuntimeException("Error encoding object key", e);
50-
}
37+
encodedObjectKey = URLEncoder.encode(objectKey, "UTF-8");
5138

5239
Algorithm algorithm = Algorithm.HMAC256(secret);
5340
JWTCreator.Builder builder = JWT.create().withIssuer("client")

0 commit comments

Comments
 (0)