全部产品
存储与CDN 数据库 安全 应用服务 数加·人工智能 数加·大数据基础服务 互联网中间件 视频服务 开发者工具 解决方案 物联网
视频点播

Java版

更新时间:2017-11-13 11:22:27

简介

以下文档描述的是视频点播上传流程中在服务端(Java)获取上传地址和凭证的方法。

环境要求

Java 6+

Maven

安装

  • 添加maven仓库

    1. <repositories>
    2. <repository>
    3. <id>sonatype-nexus-staging</id>
    4. <name>Sonatype Nexus Staging</name>
    5. <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
    6. <releases>
    7. <enabled>true</enabled>
    8. </releases>
    9. <snapshots>
    10. <enabled>true</enabled>
    11. </snapshots>
    12. </repository>
    13. </repositories>
  • 添加Jar包依赖

    注:aliyun-java-sdk-core包版本号必须>=3.2.2

    1. <dependency>
    2. <groupId>com.aliyun</groupId>
    3. <artifactId>aliyun-java-sdk-core</artifactId>
    4. <version>3.2.2</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>com.aliyun</groupId>
    8. <artifactId>aliyun-java-sdk-vod</artifactId>
    9. <version>2.6.0</version>
    10. </dependency>

接口说明

  • 引用

    1. import com.aliyuncs.DefaultAcsClient;
    2. import com.aliyuncs.profile.DefaultProfile;
    3. import com.aliyuncs.exceptions.ClientException;
    4. import com.aliyuncs.exceptions.ServerException;
    5. import com.aliyuncs.vod.model.v20170321.CreateUploadVideoRequest;
    6. import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
    7. import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoRequest;
    8. import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoResponse;
  • 初始化

    1. DefaultAcsClient aliyunClient;
    2. aliyunClient = new DefaultAcsClient(
    3. DefaultProfile.getProfile("cn-shanghai",accessKeyId,accessKeySecret));

    注: accessKeyId和accessKeySecret是全局配置参数,需要按实际值填写。

    1. private static String accessKeyId = "";
    2. private static String accessKeySecret = "";
  • 函数

    1. 获取视频上传凭证和地址

    1. private static String createUploadVideo(DefaultAcsClient client) {
    2. CreateUploadVideoRequest request = new CreateUploadVideoRequest();
    3. CreateUploadVideoResponse response = null;
    4. try {
    5. /*必选,视频源文件名称(必须带后缀, 支持 ".3gp", ".asf", ".avi", ".dat", ".dv", ".flv", ".f4v", ".gif", ".m2t", ".m3u8", ".m4v", ".mj2", ".mjpeg", ".mkv", ".mov", ".mp4", ".mpe", ".mpg", ".mpeg", ".mts", ".ogg", ".qt", ".rm", ".rmvb", ".swf", ".ts", ".vob", ".wmv", ".webm"".aac", ".ac3", ".acm", ".amr", ".ape", ".caf", ".flac", ".m4a", ".mp3", ".ra", ".wav", ".wma")*/
    6. request.setFileName("源文件名称.mp4");
    7. //必选,视频标题
    8. request.setTitle("视频标题");
    9. //可选,分类ID
    10. request.setCateId(0);
    11. //可选,视频标签,多个用逗号分隔
    12. request.setTags("标签1,标签2");
    13. //可选,视频描述
    14. request.setDescription("视频描述");
    15. response = client.getAcsResponse(request);
    16. } catch (ServerException e) {
    17. System.out.println("CreateUploadVideoRequest Server Exception:");
    18. e.printStackTrace();
    19. return null;
    20. } catch (ClientException e) {
    21. System.out.println("CreateUploadVideoRequest Client Exception:");
    22. e.printStackTrace();
    23. return null;
    24. }
    25. System.out.println("RequestId:"+response.getRequestId());
    26. System.out.println("UploadAuth:"+response.getUploadAuth());
    27. System.out.println("UploadAddress:"+response.getUploadAddress());
    28. return response.getVideoId();
    29. }

    2. 刷新视频上传凭证

    1. private static void refreshUploadVideo(DefaultAcsClient client, String videoId) {
    2. RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
    3. RefreshUploadVideoResponse response = null;
    4. try {
    5. request.setVideoId(videoId);
    6. response = client.getAcsResponse(request);
    7. } catch (ServerException e) {
    8. System.out.println("RefreshUploadVideoRequest Server Exception:");
    9. e.printStackTrace();
    10. return;
    11. } catch (ClientException e) {
    12. System.out.println("RefreshUploadVideoRequest Client Exception:");
    13. e.printStackTrace();
    14. return;
    15. }
    16. System.out.println("RequestId:" + response.getRequestId());
    17. System.out.println("UploadAuth:" + response.getUploadAuth());
    18. }
  • 函数调用示例

    1. String videoId = createUploadVideo(aliyunClient);
    2. System.out.println("VideoId:" + videoId);
    3. //当网络异常导致文件上传失败时,可刷新上传凭证后再次执行上传操作
    4. refreshUploadVideo(aliyunClient, videoId);
本文导读目录