2626 * 媒体处理 边转边播接口相关demo
2727 */
2828public 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 ();
2934
3035 public static void main (String [] args ) throws Exception {
3136 // 1 初始化用户身份信息(secretId, secretKey)。
3237 COSClient client = ClientUtils .getTestClient ();
3338 // 2 调用要使用的方法。
34- getPlayList (client );
39+ getPlayListSimple (client );
3540 }
3641
3742 /**
3843 * generatePlayList 提交生成播放列表任务
3944 */
40- public static void generatePlayList (COSClient client ) {
45+ public static void generatePlayList (COSClient client ) {
4146 //1.创建任务请求对象
4247 MediaJobsRequestV2 request = new MediaJobsRequestV2 ();
43- request .setBucketName ("demo-1234567890" );
48+ request .setBucketName (bucket );
4449 //2.添加请求参数 参数详情请见api接口文档
4550 request .setTag ("GeneratePlayList" );
46- request .getInput ().setObject ("1.mp4" );
51+ request .getInput ().setObject (objectKey );
4752 MediaContainerObject container = request .getOperation ().getTranscode ().getContainer ();
4853 container .setFormat ("hls" );
4954 container .getClipConfig ().setDuration ("5" );
@@ -66,22 +71,18 @@ public static void generatePlayList(COSClient client) {
6671 /**
6772 * describeMediaJob 根据jobId查询任务信息
6873 */
69- public static void describeMediaJob (COSClient client ) {
74+ public static void describeMediaJob (COSClient client ) {
7075 //1.创建任务请求对象
7176 MediaJobsRequestV2 request = new MediaJobsRequestV2 ();
7277 //2.添加请求参数 参数详情请见api接口文档
73- request .setBucketName ("demo-1234567890" );
78+ request .setBucketName (bucket );
7479 request .setJobId ("j8b360cd0142511efac6425779c0*****" );
7580 //3.调用接口,获取任务响应对象
7681 MediaJobResponseV2 response = client .describeMediaJobV2 (request );
7782 System .out .println (Jackson .toJsonString (response ));
7883 }
7984
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+
8586
8687 public static void getPlayList (COSClient client ) {
8788 String bucketName = bucket ;
@@ -100,47 +101,47 @@ public static void getPlayList(COSClient client) {
100101 System .out .println (url .toString ());
101102 }
102103
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 >();
117-
118- HttpMethodName method = HttpMethodName .GET ;
119- URL url = client .generatePresignedUrl (bucketName , key , expirationDate , method , headers , params );
120- System .out .println (url .toString ());
121- }
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 >();
122118
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" );
127-
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" )
119+ HttpMethodName method = HttpMethodName .GET ;
120+ URL url = client .generatePresignedUrl (bucketName , key , expirationDate , method , headers , params );
121+ System .out .println (url .toString ());
122+ }
123+
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" );
128+
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" )
141142// .withClaim("PublicKey", "xxx")
142- .withClaim ("ProtectContentKey" , 0 );
143- return builder .sign (algorithm );
144- }
143+ .withClaim ("ProtectContentKey" , 0 );
144+ return builder .sign (algorithm );
145+ }
145146
146147}
0 commit comments