集成阿里VOD功能 音/视频点播功能

集成阿里的VOD 功能 音/视频点播功能

  • 开通视频点播功能
  • 上传音视频文件
  • 配置域名
  • 为域名配置CNAME
    配置域名可以看文档:

https://help.aliyun.com/document_detail/86074.html?spm=a2c4g.11186623.6.577.107958fcFRqYiD

  • 点击视频看是够能够播放

配置好了之后 就可以开发服务端得代码了

@PropertySource(value = "classpath:vod.properties")
@EnableConfigurationProperties
@Component
public class AliyunVodHandler {


    private static final Logger logger = LoggerFactory.getLogger(AliyunVodHandler.class);


    /**
     * VOD  accessKeyId
     */
    @Value("${vod.accessKeyId}")
    private String accessKeyId;

    /**
     * VOD  accessKeySecret
     */
    @Value("${vod.accessKeySecret}")
    private String accessKeySecret;

    /**
     * VOD 服务接入区域
     */
    @Value("${vod.regionId}")
    private String regionId;

    @Value("${vod.version}")
    private String version;


    @Value("${vod.durationSeconds}")
    private Long durationSeconds;
    /**
     * RAM账号接入地区
     */
    @Value("${vod.ram.regionId}")
    private String ram_regionId;

    /**
     * RAM accesskey
     */
    @Value("${vod.ram.accessKeyId}")
    private String ram_accessKeyId;
    /**
     * RAM secret
     */
    @Value("${vod.ram.accessKeySecret}")
    private String ram_accessKeySecret;

    /**
     * RAM roleArn
     */
    @Value("${vod.ram.roleArn}")
    private String ram_roleArn;

    /**
     * RAM角色名称
     */
    @Value("${vod.ram.roleSessionName}")
    private String ram_roleSessionName;


    /**
     * action
     */
    @Value("${vod.ram.actionName}")
    private String actionName;


    //*************************************palyAuth方式鉴权************************************************end

    /**
     * 初始化 VOD
     *
     * @return
     * @throws ClientException
     */
    public DefaultAcsClient initVodClient() throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    /**
     * 获取播放凭证方法
     *
     * @param client
     * @return
     * @throws Exception
     */
    public GetVideoPlayAuthResponse getVideoPlayAuth(DefaultAcsClient client, @NotNull String videoId) throws Exception {
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoId);
        request.setAuthInfoTimeout(durationSeconds);//最大播放时间3000s
        return client.getAcsResponse(request);
    }


    /**
     * 获取播放凭证的VALUE值
     *
     * @return
     */
    public String getPlayAutoValue(@NotNull String videoId) throws Exception {
        DefaultAcsClient client = initVodClient();
        GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
        try {
            response = getVideoPlayAuth(client, videoId);
            //播放凭证
            logger.info(response.getPlayAuth() + "\n");
            logger.info("video title---->" + response.getVideoMeta().getTitle() + "\n");
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage().toString());
        }
        return response.getPlayAuth().toString();
    }

    ;

    //*************************************palyAuth方式鉴权************************************************end


    //*************************************STS方式鉴权************************************************start

    /**
     * 为用户赋予权限
     *
     * @return
     * @throws ClientException
     */
    public AssumeRoleResponse assumeRole() throws ClientException {
        try {
            IClientProfile profile = DefaultProfile.getProfile(ram_regionId, ram_accessKeyId, ram_accessKeySecret);
            DefaultAcsClient client = new DefaultAcsClient(profile);
            final AssumeRoleRequest request = new AssumeRoleRequest();
            request.setVersion(version);
            request.setRoleArn(ram_roleArn);
            request.setMethod(MethodType.POST);
            request.setRoleSessionName(ram_roleSessionName);
            request.setDurationSeconds(durationSeconds);
            request.setActionName(actionName);
            request.setProtocol(ProtocolType.HTTPS);
            final AssumeRoleResponse response = client.getAcsResponse(request);
            logger.info(response.getRequestId());
            return response;
        } catch (ClientException e) {
            throw e;
        }
    }


    /**
     * 返回封装的对象
     *
     * @return
     */
    public VodConfig stsResponse() {
        try {
            final AssumeRoleResponse response = assumeRole();
            AssumeRoleResponse.Credentials credentials = response.getCredentials();
            return  VodConfig.builder()
                    .accessKeyId(credentials.getAccessKeyId())
                    .accessKeySecret(credentials.getAccessKeySecret())
                    .securityToken(credentials.getSecurityToken())
                    .region(ram_regionId)
                    .build();
        } catch (ClientException e) {
            logger.error(e.getErrMsg());
            return null;
        }

    }


    //*******************获取上传视频的权限***********************************************************

    /**
     * 上传视频
     *
     * @return
     */
    public CreateUploadVideoResponse createUploadVideo(String title, String fileName, Long fileSize) throws Exception {
        IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        CreateUploadVideoRequest request = new CreateUploadVideoRequest();
        request.setSecurityToken(token());  //获取  token
        request.setTitle(title);
        request.setFileName(fileName);
        request.setFileSize(fileSize);
        try {
            CreateUploadVideoResponse response = client.getAcsResponse(request);
            logger.info(response.getRequestId());
            return response;
        } catch (ClientException e) {
            e.printStackTrace();
            return null;
        }
    }


    //*******************获取上传视频的权限***********************************************************

    /**
     * 获取TOKEN
     */
    public String token() throws Exception {
        final AssumeRoleResponse response = assumeRole();
        AssumeRoleResponse.Credentials credentials = response.getCredentials();
        String token = credentials.getSecurityToken();
        return token;
    }

    ;
    //*************************************STS方式鉴权************************************************end


    //********************************获取视频播放地址**************************************************start
    /*获取播放地址函数*/

    /**
     * 获取到的URL是鉴权过后的
     * 如果视频在播放过程中,过了时效,视频仍然可以播放
     * 如果时间到期,再次刷新,该地址就不能够播放
     * 需要重新获取新的URL地址播放
     * @param videoId
     * @return
     * @throws Exception
     */
    public GetPlayInfoResponse  getPlayInfo(String videoId) throws Exception {
        DefaultAcsClient client = initVodClient();
        GetPlayInfoResponse response = new GetPlayInfoResponse();
        try {
            GetPlayInfoRequest request = new GetPlayInfoRequest();
            request.setVideoId(videoId);
            request.setAuthTimeout(durationSeconds);
            request.setActionName("GetPlayInfo");
            response = client.getAcsResponse(request);
            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
            //播放地址
            for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
                logger.info("PlayInfo.PlayURL = {}", playInfo.getPlayURL()  +"\n");
            }
        } catch (Exception e) {
            logger.error("ErrorMessage = {}" + e.getLocalizedMessage());
        }
        return   response;

    }
    //********************************获取视频播放地址**************************************************end

}


vod.properties文件为:

#主账号信息
vod.regionId=cn-shanghai
vod.accessKeyId=
vod.accessKeySecret=
vod.version=2015-04-01
vod.durationSeconds=3600
#子账号信息
vod.ram.regionId=cn-shanghai
vod.ram.accessKeyId=
vod.ram.accessKeySecret=
vod.ram.roleArn=
vod.ram.roleSessionName=roleSessionName
vod.ram.actionName=AssumeRole

vod.config.java

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class VodConfig implements Serializable {


    private  String accessKeyId;

    private  String accessKeySecret;

    private  String securityToken;

    private  String region;


}

在对URL进行鉴权的时候,需要在域名管理中的访问控制中开启 url鉴权 设置key,并且设置失效时间

你可能感兴趣的:(JAVA)