11package 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 ;
36import com .qcloud .cos .COSClient ;
7+ import com .qcloud .cos .http .HttpMethodName ;
48import com .qcloud .cos .model .ciModel .common .MediaVod ;
59import com .qcloud .cos .model .ciModel .job .*;
610import com .qcloud .cos .model .ciModel .job .v2 .GetPlayListRequest ;
913import com .qcloud .cos .utils .Jackson ;
1014
1115import java .io .*;
16+ import java .net .URL ;
17+ import java .net .URLEncoder ;
1218import 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
1627 */
1728public class GeneratePlayListDemo {
29+ private static String appId = "123456789" ;
30+ private static String bucket = "demo-123456789" ;
31+ private static String objectKey = "test.m3u8" ;
32+ private static String expires = "3600" ;
33+ private static byte [] secret = "YourSecret" .getBytes ();
1834
1935 public static void main (String [] args ) throws Exception {
2036 // 1 初始化用户身份信息(secretId, secretKey)。
2137 COSClient client = ClientUtils .getTestClient ();
2238 // 2 调用要使用的方法。
23- getPlayList (client );
39+ getPlayListSimple (client );
2440 }
2541
2642 /**
2743 * generatePlayList 提交生成播放列表任务
2844 */
29- public static void generatePlayList (COSClient client ) {
45+ public static void generatePlayList (COSClient client ) {
3046 //1.创建任务请求对象
3147 MediaJobsRequestV2 request = new MediaJobsRequestV2 ();
32- request .setBucketName ("demo-1234567890" );
48+ request .setBucketName (bucket );
3349 //2.添加请求参数 参数详情请见api接口文档
3450 request .setTag ("GeneratePlayList" );
35- request .getInput ().setObject ("1.mp4" );
51+ request .getInput ().setObject (objectKey );
3652 MediaContainerObject container = request .getOperation ().getTranscode ().getContainer ();
3753 container .setFormat ("hls" );
3854 container .getClipConfig ().setDuration ("5" );
@@ -55,51 +71,77 @@ public static void generatePlayList(COSClient client) {
5571 /**
5672 * describeMediaJob 根据jobId查询任务信息
5773 */
58- public static void describeMediaJob (COSClient client ) {
74+ public static void describeMediaJob (COSClient client ) {
5975 //1.创建任务请求对象
6076 MediaJobsRequestV2 request = new MediaJobsRequestV2 ();
6177 //2.添加请求参数 参数详情请见api接口文档
62- request .setBucketName ("demo-1234567890" );
78+ request .setBucketName (bucket );
6379 request .setJobId ("j8b360cd0142511efac6425779c0*****" );
6480 //3.调用接口,获取任务响应对象
6581 MediaJobResponseV2 response = client .describeMediaJobV2 (request );
6682 System .out .println (Jackson .toJsonString (response ));
6783 }
6884
69- /**
70- * getPlayList 接口用于获取私有 M3U8 ts 资源的下载授权
71- */
85+
86+
7287 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- }
83- }
88+ String bucketName = bucket ;
89+ String key = objectKey ;
90+ // 设置签名过期时间(可选), 若未进行设置则默认使用 ClientConfig 中的签名过期时间(1小时)
91+ Instant now = Instant .now ();
92+ Instant expire = now .plus (Long .parseLong (expires ), ChronoUnit .SECONDS );
93+ Date expirationDate = Date .from (expire );
94+ Map <String , String > params = new HashMap <String , String >();
95+ params .put ("ci-process" , "getplaylist" );
96+ params .put ("expires" , "43200" );
97+ Map <String , String > headers = new HashMap <String , String >();
8498
85- public static String inputStreamToString ( InputStream inputStream ) throws IOException {
86- if ( inputStream == null ) {
87- return "" ;
88- }
99+ HttpMethodName method = HttpMethodName . GET ;
100+ URL url = client . generatePresignedUrl ( bucketName , key , expirationDate , method , headers , params );
101+ System . out . println ( url . toString ()) ;
102+ }
89103
90- StringBuilder stringBuilder = new StringBuilder ();
91- InputStreamReader inputStreamReader = new InputStreamReader (inputStream , StandardCharsets .UTF_8 );
92- BufferedReader bufferedReader = new BufferedReader (inputStreamReader );
104+ public static void getPlayListSimple (COSClient client ) throws UnsupportedEncodingException {
105+ String bucketName = bucket ;
106+ String key = objectKey ;
107+ // 设置签名过期时间(可选), 若未进行设置则默认使用 ClientConfig 中的签名过期时间(1小时)
108+ Instant now = Instant .now ();
109+ Instant expire = now .plus (Long .parseLong (expires ), ChronoUnit .SECONDS );
110+ Date expirationDate = Date .from (expire );
111+ String token = generateToken (appId , bucket , objectKey , secret , expirationDate );
112+ Map <String , String > params = new HashMap <String , String >();
113+ params .put ("ci-process" , "getplaylist" );
114+ params .put ("expires" , "43200" );
115+ params .put ("token-type" , "JwtToken" );
116+ params .put ("token" , token );
117+ Map <String , String > headers = new HashMap <String , String >();
93118
94- String line ;
95- while (( line = bufferedReader . readLine ()) != null ) {
96- stringBuilder . append ( line );
97- }
119+ HttpMethodName method = HttpMethodName . GET ;
120+ URL url = client . generatePresignedUrl ( bucketName , key , expirationDate , method , headers , params );
121+ System . out . println ( url . toString () );
122+ }
98123
99- bufferedReader .close ();
100- inputStreamReader .close ();
101- inputStream .close ();
124+ public static String generateToken (String appId , String bucketId , String objectKey , byte [] secret , Date expires ) throws UnsupportedEncodingException {
125+ Instant now = Instant .now ();
126+ String encodedObjectKey ;
127+ encodedObjectKey = URLEncoder .encode (objectKey , "UTF-8" );
102128
103- return stringBuilder .toString ();
129+ Algorithm algorithm = Algorithm .HMAC256 (secret );
130+ JWTCreator .Builder builder = JWT .create ().withIssuer ("client" )
131+ .withIssuedAt (Date .from (now ))
132+ .withExpiresAt (expires )
133+ .withClaim ("Type" , "CosCiToken" )
134+ .withClaim ("AppId" , appId )
135+ .withClaim ("BucketId" , bucketId )
136+ .withClaim ("Object" , encodedObjectKey )
137+ .withClaim ("Issuer" , "client" )
138+ .withClaim ("IssuedTimeStamp" , now .getEpochSecond ())
139+ .withClaim ("ExpireTimeStamp" , expires .getTime () / 1000 )
140+ .withClaim ("UsageLimit" , 20 )
141+ .withClaim ("ProtectSchema" , "rsa1024" )
142+ // .withClaim("PublicKey", "xxx")
143+ .withClaim ("ProtectContentKey" , 0 );
144+ return builder .sign (algorithm );
104145 }
146+
105147}
0 commit comments