|
1 | 1 | package com.qcloud.cos.demo.ci; |
2 | 2 |
|
| 3 | +import com.auth0.jwt.JWT; |
| 4 | +import com.auth0.jwt.JWTCreator; |
| 5 | +import com.auth0.jwt.algorithms.Algorithm; |
3 | 6 | import com.qcloud.cos.COSClient; |
| 7 | +import com.qcloud.cos.http.HttpMethodName; |
4 | 8 | import com.qcloud.cos.model.ciModel.common.MediaVod; |
5 | 9 | import com.qcloud.cos.model.ciModel.job.*; |
6 | 10 | import com.qcloud.cos.model.ciModel.job.v2.GetPlayListRequest; |
|
9 | 13 | import com.qcloud.cos.utils.Jackson; |
10 | 14 |
|
11 | 15 | import java.io.*; |
| 16 | +import java.net.URL; |
| 17 | +import java.net.URLEncoder; |
12 | 18 | 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; |
13 | 24 |
|
14 | 25 | /** |
15 | 26 | * 媒体处理 边转边播接口相关demo |
@@ -66,40 +77,70 @@ public static void describeMediaJob(COSClient client) { |
66 | 77 | System.out.println(Jackson.toJsonString(response)); |
67 | 78 | } |
68 | 79 |
|
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 | + |
72 | 86 | 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()); |
83 | 101 | } |
84 | 102 |
|
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>(); |
89 | 117 |
|
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 | +} |
93 | 122 |
|
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"); |
98 | 127 |
|
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 | +} |
102 | 145 |
|
103 | | - return stringBuilder.toString(); |
104 | | - } |
105 | 146 | } |
0 commit comments