Skip to content

Commit

Permalink
Merge pull request #209 from 563750789/master
Browse files Browse the repository at this point in the history
update demo
  • Loading branch information
563750789 authored Jul 24, 2024
2 parents f7df2da + ea7c875 commit 9e44844
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 80 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.qcloud</groupId>
<artifactId>cos_api</artifactId>
<version>5.6.220</version>
<version>5.6.221</version>
<packaging>jar</packaging>
<name>cos-java-sdk</name>
<description>java sdk for qcloud cos</description>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/qcloud/cos/COS.java
Original file line number Diff line number Diff line change
Expand Up @@ -3106,7 +3106,7 @@ SelectObjectContentResult selectObjectContent(SelectObjectContentRequest selectR
/**
* GenerateDocPreviewHtmlUrl 查询账号下已开通文档预览功能的bucket
*/
String GenerateDocPreviewUrl(DocHtmlRequest docJobRequest) throws URISyntaxException;
String generateDocPreviewUrl(DocHtmlRequest docJobRequest) throws URISyntaxException;

/**
* createWebpageAuditingJob 提交网页审核任务 https://cloud.tencent.com/document/product/460/63968
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/qcloud/cos/COSClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4299,8 +4299,12 @@ public Boolean createMediaProcessBucket(MediaBucketRequest mediaBucketRequest) {
return true;
}

@Override
public String GenerateDocPreviewUrl(DocHtmlRequest docJobRequest) throws URISyntaxException {
return generateDocPreviewUrl(docJobRequest);
}

@Override
public String generateDocPreviewUrl(DocHtmlRequest docJobRequest) throws URISyntaxException {
rejectNull(docJobRequest,
"The request parameter must be specified setting the object tags");
rejectNull(docJobRequest.getBucketName(),
Expand Down Expand Up @@ -5212,7 +5216,7 @@ public DeleteFileMetaIndexResponse deleteFileMetaIndex(DeleteFileMetaIndexReques
rejectNull(customRequest, "The request parameter must be specified setting the object tags");

CosHttpRequest<DeleteFileMetaIndexRequest> request = createRequest(customRequest.getAppId(), "/filemeta", customRequest , HttpMethodName.DELETE);

request.addHeader("Accept", "application/json");
this.setContent(request, CIJackson.toJsonBytes(customRequest), "application/json", false);
return invoke(request, new Unmarshallers.CICommonJsonUnmarshaller<DeleteFileMetaIndexResponse>(DeleteFileMetaIndexResponse.class));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public static void deleteDatasetBinding(COSClient client) {
// 设置数据集名称,同一个账户下唯一。;是否必传:是
request.setDatasetName("test");
// 设置资源标识字段,表示需要与数据集绑定的资源,当前仅支持COS存储桶,字段规则:cos://<BucketName>,其中BucketName表示COS存储桶名称,例如:cos://examplebucket-1250000000;是否必传:是
request.setURI("cos://examplebucket-1250000000");

request.setURI("cos://examplebucket-1250000000/object");
DeleteDatasetBindingResponse response = client.deleteDatasetBinding(request);
System.out.println(Jackson.toJsonString(response));
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/qcloud/cos/demo/ci/DocJobDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static void generateDocPreviewUrl(COSClient client) throws URISyntaxExcep
request.setDstType(DocHtmlRequest.DocType.html);
request.setObjectKey("1.pptx");
//3.调用接口,获取任务响应对象
String previewUrl = client.GenerateDocPreviewUrl(request);
String previewUrl = client.generateDocPreviewUrl(request);
System.out.println(previewUrl);
}

Expand Down
112 changes: 77 additions & 35 deletions src/main/java/com/qcloud/cos/demo/ci/GeneratePlayListDemo.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.qcloud.cos.demo.ci;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.http.HttpMethodName;
import com.qcloud.cos.model.ciModel.common.MediaVod;
import com.qcloud.cos.model.ciModel.job.*;
import com.qcloud.cos.model.ciModel.job.v2.GetPlayListRequest;
Expand All @@ -9,30 +13,42 @@
import com.qcloud.cos.utils.Jackson;

import java.io.*;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
* 媒体处理 边转边播接口相关demo
*/
public class GeneratePlayListDemo {
private static String appId = "123456789";
private static String bucket = "demo-123456789";
private static String objectKey = "test.m3u8";
private static String expires = "3600";
private static byte[] secret = "YourSecret".getBytes();

public static void main(String[] args) throws Exception {
// 1 初始化用户身份信息(secretId, secretKey)。
COSClient client = ClientUtils.getTestClient();
// 2 调用要使用的方法。
getPlayList(client);
getPlayListSimple(client);
}

/**
* generatePlayList 提交生成播放列表任务
*/
public static void generatePlayList(COSClient client) {
public static void generatePlayList(COSClient client) {
//1.创建任务请求对象
MediaJobsRequestV2 request = new MediaJobsRequestV2();
request.setBucketName("demo-1234567890");
request.setBucketName(bucket);
//2.添加请求参数 参数详情请见api接口文档
request.setTag("GeneratePlayList");
request.getInput().setObject("1.mp4");
request.getInput().setObject(objectKey);
MediaContainerObject container = request.getOperation().getTranscode().getContainer();
container.setFormat("hls");
container.getClipConfig().setDuration("5");
Expand All @@ -55,51 +71,77 @@ public static void generatePlayList(COSClient client) {
/**
* describeMediaJob 根据jobId查询任务信息
*/
public static void describeMediaJob(COSClient client) {
public static void describeMediaJob(COSClient client) {
//1.创建任务请求对象
MediaJobsRequestV2 request = new MediaJobsRequestV2();
//2.添加请求参数 参数详情请见api接口文档
request.setBucketName("demo-1234567890");
request.setBucketName(bucket);
request.setJobId("j8b360cd0142511efac6425779c0*****");
//3.调用接口,获取任务响应对象
MediaJobResponseV2 response = client.describeMediaJobV2(request);
System.out.println(Jackson.toJsonString(response));
}

/**
* getPlayList 接口用于获取私有 M3U8 ts 资源的下载授权
*/


public static void getPlayList(COSClient client) {
GetPlayListRequest request = new GetPlayListRequest();
request.setBucketName("demo-1234567890");
request.setObject("output/media/test.m3u8");
request.setExpires("3600");
try {
InputStream response = client.getPlayList(request);
System.out.println(inputStreamToString(response));
} catch (IOException e) {
e.printStackTrace();
}
}
String bucketName = bucket;
String key = objectKey;
// 设置签名过期时间(可选), 若未进行设置则默认使用 ClientConfig 中的签名过期时间(1小时)
Instant now = Instant.now();
Instant expire = now.plus(Long.parseLong(expires), ChronoUnit.SECONDS);
Date expirationDate = Date.from(expire);
Map<String, String> params = new HashMap<String, String>();
params.put("ci-process", "getplaylist");
params.put("expires", "43200");
Map<String, String> headers = new HashMap<String, String>();

public static String inputStreamToString(InputStream inputStream) throws IOException {
if (inputStream == null) {
return "";
}
HttpMethodName method = HttpMethodName.GET;
URL url = client.generatePresignedUrl(bucketName, key, expirationDate, method, headers, params);
System.out.println(url.toString());
}

StringBuilder stringBuilder = new StringBuilder();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
public static void getPlayListSimple(COSClient client) throws UnsupportedEncodingException {
String bucketName = bucket;
String key = objectKey;
// 设置签名过期时间(可选), 若未进行设置则默认使用 ClientConfig 中的签名过期时间(1小时)
Instant now = Instant.now();
Instant expire = now.plus(Long.parseLong(expires), ChronoUnit.SECONDS);
Date expirationDate = Date.from(expire);
String token = generateToken(appId, bucket, objectKey, secret, expirationDate);
Map<String, String> params = new HashMap<String, String>();
params.put("ci-process", "getplaylist");
params.put("expires", "43200");
params.put("token-type", "JwtToken");
params.put("token", token);
Map<String, String> headers = new HashMap<String, String>();

String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
HttpMethodName method = HttpMethodName.GET;
URL url = client.generatePresignedUrl(bucketName, key, expirationDate, method, headers, params);
System.out.println(url.toString());
}

bufferedReader.close();
inputStreamReader.close();
inputStream.close();
public static String generateToken(String appId, String bucketId, String objectKey, byte[] secret, Date expires) throws UnsupportedEncodingException {
Instant now = Instant.now();
String encodedObjectKey;
encodedObjectKey = URLEncoder.encode(objectKey, "UTF-8");

return stringBuilder.toString();
Algorithm algorithm = Algorithm.HMAC256(secret);
JWTCreator.Builder builder = JWT.create().withIssuer("client")
.withIssuedAt(Date.from(now))
.withExpiresAt(expires)
.withClaim("Type", "CosCiToken")
.withClaim("AppId", appId)
.withClaim("BucketId", bucketId)
.withClaim("Object", encodedObjectKey)
.withClaim("Issuer", "client")
.withClaim("IssuedTimeStamp", now.getEpochSecond())
.withClaim("ExpireTimeStamp", expires.getTime() / 1000)
.withClaim("UsageLimit", 20)
.withClaim("ProtectSchema", "rsa1024")
// .withClaim("PublicKey", "xxx")
.withClaim("ProtectContentKey", 0);
return builder.sign(algorithm);
}

}
1 change: 0 additions & 1 deletion src/main/java/com/qcloud/cos/demo/ci/SearchImageDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.qcloud.cos.model.ciModel.metaInsight.SearchImageResponse;
import com.qcloud.cos.utils.Jackson;

import java.util.List;

/**
* 图像检索 详情见https://cloud.tencent.com/document/product/460/106376
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,29 @@
import java.time.temporal.ChronoUnit;
import java.util.Date;

public class LiveTranscodeDemo {
public class generatePrivateM3U8UrlDemo {
private static String appId = "123456789";
private static String bucket = "demo-123456789";
private static String objectKey = "test.m3u8";
private static String expires = "3600";
private static byte[] secret = "YourSecret".getBytes();


public static void main(String[] args) {
COSClient client = ClientUtils.getTestClient();
String url = generateCosDomainPrivateM3U8Url(client);
System.out.println("Generated url: " + url);
}

public static String generateCosDomainPrivateM3U8Url(COSClient client) {
public static String generateCosDomainPrivateM3U8Url(COSClient client) throws UnsupportedEncodingException {
PrivateM3U8Request request = new PrivateM3U8Request();
request.setBucketName(bucket);
request.setObject(objectKey);
request.setExpires(expires);
request.setTokenType("JwtToken");
String token =generateToken(appId, bucket, objectKey, secret,expires);
String token = generateToken(appId, bucket, objectKey, secret,expires);
request.setToken(token);
return client.generateCosDomainPrivateM3U8Url(request);
}

public static String generateToken(String appId, String bucketId, String objectKey, byte[] secret, String expires) {

public static String generateToken(String appId, String bucketId, String objectKey, byte[] secret, String expires) throws UnsupportedEncodingException {
Instant now = Instant.now();
Instant expire = now.plus(6, ChronoUnit.DAYS);

Instant expire = now.plus(Long.parseLong(expires), ChronoUnit.SECONDS);
String encodedObjectKey;
try {
encodedObjectKey = URLEncoder.encode(objectKey, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Error encoding object key", e);
}
encodedObjectKey = URLEncoder.encode(objectKey, "UTF-8");

Algorithm algorithm = Algorithm.HMAC256(secret);
JWTCreator.Builder builder = JWT.create().withIssuer("client")
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/com/qcloud/cos/http/DefaultCosHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import com.qcloud.cos.internal.ResettableInputStream;
import com.qcloud.cos.internal.SdkBufferedInputStream;
import com.qcloud.cos.internal.CIWorkflowServiceRequest;
import com.qcloud.cos.internal.CIServiceRequest;
import com.qcloud.cos.retry.BackoffStrategy;
import com.qcloud.cos.retry.RetryPolicy;
import com.qcloud.cos.utils.CodecUtils;
Expand Down Expand Up @@ -233,7 +234,11 @@ private <X extends CosServiceRequest> HttpRequestBase buildHttpRequest(
} else if (httpMethodName.equals(HttpMethodName.GET)) {
httpRequestBase = new HttpGet();
} else if (httpMethodName.equals(HttpMethodName.DELETE)) {
httpRequestBase = new HttpEntityEnclosingDelete();
if (request.getOriginalRequest() instanceof CIServiceRequest) {
httpRequestBase = new HttpEntityEnclosingDelete();
} else {
httpRequestBase = new HttpDelete();
}
} else if (httpMethodName.equals(HttpMethodName.POST)) {
httpRequestBase = new HttpPost();
} else if (httpMethodName.equals(HttpMethodName.HEAD)) {
Expand Down Expand Up @@ -291,9 +296,8 @@ private <X extends CosServiceRequest> HttpRequestBase buildHttpRequest(
HttpEntityEnclosingRequestBase entityRequestBase =
(HttpEntityEnclosingRequestBase) httpRequestBase;
entityRequestBase.setEntity(reqEntity);
}

if ( httpMethodName.equals(HttpMethodName.DELETE)){
} else if (httpMethodName.equals(HttpMethodName.DELETE) &&
request.getOriginalRequest() instanceof CIServiceRequest) {
HttpEntityEnclosingRequestBase entityRequestBase =
(HttpEntityEnclosingDelete) httpRequestBase;
entityRequestBase.setEntity(reqEntity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6774,14 +6774,20 @@ protected void doEndElement(String uri, String name, String qName) {
case "Score":
response.setScore(getText());
break;
case "CompressionResult":
response.setCompressionResult(getText());
break;
case "SubLabel":
response.setSubLabel(getText());
break;
default:
break;
}
} else if (in("Response", "JobsDetail", "PornInfo")) {
ParserMediaInfoUtils.ParsingAuditingCommonInfo(response.getPornInfo(), name, getText());
} else if (in("Response", "JobsDetail", "PoliticsInfo")) {
ParserMediaInfoUtils.ParsingAuditingCommonInfo(response.getPoliticsInfo(), name, getText());
} else if (in("Response", "JobsDetail", "TerroristInfo")) {
} else if (in("Response", "JobsDetail", "TerroristInfo") || in("Response", "JobsDetail", "TerrorismInfo")) {
ParserMediaInfoUtils.ParsingAuditingCommonInfo(response.getTerroristInfo(), name, getText());
} else if (in("Response", "JobsDetail", "AdsInfo")) {
ParserMediaInfoUtils.ParsingAuditingCommonInfo(response.getAdsInfo(), name, getText());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.qcloud.cos.model.ciModel.metaInsight;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.qcloud.cos.internal.CIServiceRequest;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
Expand All @@ -19,6 +20,7 @@ public class DeleteDatasetBindingRequest extends CIServiceRequest {
/**
*资源标识字段,表示需要与数据集绑定的资源,当前仅支持COS存储桶,字段规则:cos://<BucketName>,其中BucketName表示COS存储桶名称,例如:cos://examplebucket-1250000000;是否必传:是
*/
@JsonProperty("URI")
private String uRI;

public String getDatasetName() { return datasetName; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.qcloud.cos.model.ciModel.metaInsight;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.qcloud.cos.internal.CIServiceRequest;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
Expand All @@ -19,6 +20,7 @@ public class DeleteFileMetaIndexRequest extends CIServiceRequest {
/**
*资源标识字段,表示需要建立索引的文件地址。;是否必传:是
*/
@JsonProperty("URI")
private String uRI;

public String getDatasetName() { return datasetName; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.qcloud.cos.model.ciModel.metaInsight;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.qcloud.cos.model.CiServiceResult;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
Expand All @@ -12,6 +13,7 @@ public class DeleteFileMetaIndexResponse extends CiServiceResult {
/**
*请求ID
*/
@JsonProperty("RequestId")
private String requestId;

public String getRequestId() { return requestId; }
Expand Down
Loading

0 comments on commit 9e44844

Please sign in to comment.