开发者指南
快速入门
-
新建 DocClient
DocClient是与Document Service交互的客户端,所有Document操作都是通过DocClient完成的。请您参考新建DocClient,完成初始化客户端的操作。
-
(可选)创建Notification(文档通知)。
成功创建通知后,DOC会在文档转码完成后向您指定的回调地址推送消息。
-
创建文档。
通过创建文档,DOC会将您的本地文档上传到百度智能云。
-
管理文档。
文档状态请参见DOC核心概念-文档状态。
文档上传成功后,以 documentID 为索引,您可以通过接口函数
getDocument()
查询文档的信息和状态,并可通过函数deleteDocument()
删除文档。 -
播放文档。
文档转码成功后,您可将获取到的播放地址及相关参数集成到html/Android/iOS代码中,在各个终端实现文档播放,具体请参见:
文档处理
当您把文档文档上传到百度智能云后,DOC会存储文档、对文档进行转码并生成HTML阅读代码,方便您在多个终端进行文档在线浏览。文档状态请参见DOC核心概念-文档状态。
创建文档
创建文档有两种方法:
- 通过本地文档创建。代码示例如下:
1public void createDocument(DocClient client, File file, String title, String format, String notification, String access, String targetType) {
2 CreateDocumentResponse resp = client.createDocument(file, title, format, notification, access, targetType);
3 System.out.println("documentId: " + resp.getDocumentId());
4}
- 通过BOS上的object创建。代码示例如下:
1public void createDocument(DocClient client, String bucket_name, String object_name, String title, String format, String notification, String access, String targetType) {
2 CreateDocumentResponse resp = client.createDocumentFromBos(bucket_name, object_name, title, format, notification,access, targetType);
3 System.out.println("documentId: " + resp.getDocumentId());
4}
注意:通过BOS创建文档存在如下限制条件:
- 源文档所在BOS bucket所属地域必须为华北-北京(bj);百度智能云文档转码服务尚不支持根据其它地域BOS object创建文档。
- 用户需要将源文档所在BOS bucket权限设置为公共读,或者在自定义权限设置中为百度智能云文档转码服务账号(183db8cd3d5a4bf9a94459f89a7a3a91)添加READ权限。
查询指定文档
通过文档的唯一标识 documentId 查询指定文档的详细信息。代码示例如下:
1public void getDocument(DocClient client, String documentId) {
2 GetDocumentResponse resp = client.getDocument(documentId);
3 System.out.println("documentId: " + resp.getDocumentId());
4 System.out.println("title: " + resp.getTitle());
5 System.out.println("format: " + resp.getFormat());
6 System.out.println("status: " + resp.getStatus());
7 if (resp.getStatus() == "PUBLISHED") {
8 System.out.println("pageCount: " + resp.getPublishInfo().getPageCount());
9 System.out.println("sizeInBytes: " + resp.getPublishInfo().getSizeInBytes());
10 System.out.println("coverUrl: " + resp.getPublishInfo().getCoverUrl());
11 System.out.println("publishTime: " + resp.getPublishInfo().getPublishTime());
12 }
13 if (resp.getStatus() == "UPLOADING") {
14 System.out.println("bucket: " + resp.getUploadInfo().getBucket());
15 System.out.println("object: " + resp.getUploadInfo().getObject());
16 System.out.println("bosEndpoint: " + resp.getPublishInfo().getBosEndpoint());
17 }
18 if (resp.getStatus() == "FAILED") {
19 System.out.println("errorCode: " + resp.getError().getCode());
20 System.out.println("errorMessage: " + resp.getError().getMessage());
21 }
22 System.out.println("notification: " + resp.getNotification());
23 System.out.println("createTime: " + resp.getCreateTime());
24}
删除文档
通过文档的唯一标识 documentId 删除指定文档。代码示例如下:
1public void deleteDocument(DocClient client, String documentId) {
2 client.deleteDocument(documentId);
3}
阅读文档
通过文档的唯一标识 documentId 获取指定文档的阅读信息,以便在PC/Android/iOS设备上阅读。仅对状态为PUBLISHED
的文档有效。获取到阅读信息后,请根据您的设备选择相应的方式进行在线浏览:
代码示例如下:
1public void readDocument(DocClient client, String documentId) {
2 ReadDocumentResponse resp = client.readDocument(documentId);
3 System.out.println("documentId: " + resp.getDocumentId());
4 System.out.println("host: " + resp.getHost());
5 System.out.println("token: " + resp.getToken());
6}
文档通知
创建通知
通过用户提供的回调地址进行创建通知。如果您在创建/申请文档时指定了通知,在文档处理完成后,DOC会向您指定的回调地址推送通知消息。
1public void createNotification(DocClient client, String name, String endpoint) {
2 client.createNotification(name, endpoint);
3}
查询指定通知
查询指定通知的详细信息。代码示例如下:
1public void getNotification(DocClient client, String name) {
2 GetNotificationResponse resp = client.getNotification(name);
3 System.out.println("name: " + resp.getName());
4 System.out.println("name: " + resp.getEndpoint());
5}
查询所有通知
查询已创建的全部通知。代码示例如下:
1public void listNotifications(DocClient client) {
2 ListNotificationsResponse resp = client.listNotifications();
3 for (Notification notification : resp.getNotifications()) {
4 System.out.println("name: " + notification.getName());
5 System.out.println("name: " + notification.getEndpoint());
6 }
7}
删除通知
删除指定通知。代码示例如下:
1public void deleteNotification(DocClient client, String name) {
2 client.deleteNotification(name);
3}
文档日志
您可以直接使用SDK中支持的logback作为slf4j的实现,也可以自行选择其他框架(例如log4j)。
默认日志
若您使用默认的logback,则需要配置logback.xml到classpath中。否则,日志级别默认为DEBUG。
1<configuration>
2 <property name="LOG_HOME" value="./log/"/>
3 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4 <!-- encoders are assigned the type
5 ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
6 <encoder>
7 <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
8 </encoder>
9 </appender>
10
11 <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
12 <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
13 <FileNamePattern>${LOG_HOME}/DocumentUnitTest.%d{yyyy-MM-dd}.log</FileNamePattern>
14 <MaxHistory>30</MaxHistory>
15 </rollingPolicy>
16 <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
17 <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
18 </encoder>
19 <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
20 <MaxFileSize>10MB</MaxFileSize>
21 </triggeringPolicy>
22 </appender>
23
24 <root level="info">
25 <appender-ref ref="STDOUT"/>
26 <appender-ref ref="FILE"/>
27 </root>
28</configuration>
自有日志模块
若您使用自己的日志实现模块,例如项目依赖于Maven,则可以参考下面的配置到pom.xml中来去除logback。
1<?xml version="1.0" encoding="utf-8"?>
2
3<dependency>
4 <groupId>com.baidubce</groupId>
5 <artifactId>bce-java-sdk</artifactId>
6 <version>${bce.sdk.version}</version>
7 <exclusions>
8 <exclusion>
9 <groupId>ch.qos.logback</groupId>
10 <artifactId>logback-classic</artifactId>
11 </exclusion>
12 <exclusion>
13 <groupId>ch.qos.logback</groupId>
14 <artifactId>logback-core</artifactId>
15 </exclusion>
16 <exclusion>
17 <groupId>org.slf4j</groupId>
18 <artifactId>jcl-over-slf4j</artifactId>
19 </exclusion>
20 </exclusions>
21</dependency>
#版本更新记录
v0.10.16
- 支持marker方式查询文档列表。
- 支持创建文档时设置文档权限access,有效值:PUBLIC、PRIVATE。PUBLIC,表示公开文档,设为PRIVATE时表示私有文档。
- 创建文档支持指定image/h5两种方式。