阿里云上传视频流程

  1. 初始化一个类

 public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
        String regionId = "cn-shanghai";  // 点播服务接入地域
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

2.根据视频ID获取视频播放地址

 DefaultAcsClient client = init.initVodClient(
                "账号",
                "密码");
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        GetPlayInfoResponse response = new GetPlayInfoResponse();
        request.setVideoId("4aa66d40bc0671edaac36733a68f0102");
        response= client.getAcsResponse(request);
        List playInfoList = response.getPlayInfoList();
        //播放地址
        for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
            System.out.print("PlayInfo.PlayURL = " + playInfo.getPlayURL() + "\n");
        }
        //Base信息
        System.out.print("VideoBase.Title = " + response.getVideoBase().getTitle() + "\n");

   

3.所需要的所有依赖


  com.aliyun
  aliyun-java-sdk-core
  4.6.0


  com.aliyun
  aliyun-java-sdk-vod
  2.16.10


  com.alibaba
  fastjson
  1.2.83


  com.aliyun
  aliyun-java-sdk-kms
  2.10.1

4.获取视频的凭证

//获取视频的凭证
        DefaultAcsClient client = init.initVodClient(
                "账号",
                "密码");
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
        request.setVideoId("4aa66d40bc0671edaac36733a68f0102");
        response= client.getAcsResponse(request);
        System.out.println("凭证:"+response.getPlayAuth());

5.上传本地视频

//文件上传
        String title="a.sdk";//上传之后文件的名称
        String fileName="D:\\项目\\学院项目\\项目资料\\1-阿里云上传测试视频\\a.mp4"; //本地文件路径
        UploadVideoRequest request = new UploadVideoRequest(“账号", "密码", title, fileName);
        /* 可指定分片上传时每个分片的大小,默认为2M字节 */
        request.setPartSize(2 * 1024 * 1024L);
        /* 可指定分片上传时的并发线程数,默认为1,(注:该配置会占用服务器CPU资源,需根据服务器情况指定)*/
        request.setTaskNum(1);
        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadVideoResponse response = uploader.uploadVideo(request);
        System.out.print("RequestId=" + response.getRequestId() + "\n");  //请求视频点播服务的请求ID
        if (response.isSuccess()) {
            System.out.print("VideoId=" + response.getVideoId() + "\n"); //id
        } else {
            /* 如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因 */
            System.out.print("VideoId=" + response.getVideoId() + "\n");
            System.out.print("ErrorCode=" + response.getCode() + "\n");
            System.out.print("ErrorMessage=" + response.getMessage() + "\n");
        }

6.删除后面再补充

 try {
            DefaultAcsClient client = initVideo.initVodClient("ak",
                    "sk");
            DeleteVideoRequest request = new DeleteVideoRequest();
            DeleteVideoResponse response = new DeleteVideoResponse();
            request.setVideoIds(souceId);
            client.getAcsResponse(request);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            throw new GlxyExcption(20001,"删除视频失败");
        }

    }

你可能感兴趣的:(阿里云,java,云计算)