• 阿里云视频点播功能


    httpclient技术可以调用api地址http://vod.cn-shanghai.aliyunes.com/Action=GetPlayInfo&VideoId=1223

    API: 阿里云提供固定的地址,只需要调用这个固定的地址,向地址传递参数,实现功能。

    SDK:sdk对api方式进行封装,更方便使用,之前使用EasyExcel调用阿里云提供类或者接口里面的方法实现视频功能。

    1.引入maven依赖

         <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>aliyun-java-sdk-core</artifactId>
            </dependency>
            <dependency>
                <groupId>com.aliyun.oss</groupId>
                <artifactId>aliyun-sdk-oss</artifactId>
            </dependency>
            <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>aliyun-java-sdk-vod</artifactId>
            </dependency>
            <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>aliyun-sdk-vod-upload</artifactId>
            </dependency>

    2.由于aliyun-java-vod-upload-1.4.9.jar还未正式开源,需要直接引入jar包到项目中,这个依赖不能直接在maven中央仓库下载到,手动把依赖安装到本地仓库里面。使用maven命令进行安装。 进入lib 目录执行下面命令

    mvn install:install-file -DgroupId=com.aliyun -DartifactId=aliyun-sdk-vod-upload -Dversion=1.4.11 -Dpackaging=jar -Dfile=aliyun-java-vod-upload-1.4.11.jar

    3. application.properties

    # 服务端口
    server.port=8003
    # 服务名
    spring.application.name=service-vod
    
    # 环境设置:dev、test、prod
    spring.profiles.active=dev
    
    # nacos服务地址
    spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
    
    #阿里云 vod
    #不同的服务器,地址不同
    aliyun.vod.file.keyid=LTAI4GDTqjJkxrB353q44ejE
    aliyun.vod.file.keysecret=rdy8GeTHEYeKOGrY1EJ8pKGdQodMSv
    
    # 最大上传单个文件大小:默认1M
    spring.servlet.multipart.max-file-size=1024MB
    # 最大置总上传的数据大小 :默认10M
    spring.servlet.multipart.max-request-size=1024MB

     4.utils

    @Component
    public class ConstantVodUtils implements InitializingBean {
    
        @Value("${aliyun.vod.file.keyid}")
        private String keyid;
    
        @Value("${aliyun.vod.file.keysecret}")
        private String keysecret;
    
        public static String ACCESS_KEY_SECRET;
        public static String ACCESS_KEY_ID;
    
        @Override
        public void afterPropertiesSet() throws Exception {
            ACCESS_KEY_ID = keyid;
            ACCESS_KEY_SECRET = keysecret;
        }
    }
    public class InitVodCilent {
    
        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;
        }
    }

    5.controller

    @RestController
    @RequestMapping("/eduvod/video")
    @CrossOrigin
    public class VodController {
    
        @Autowired
        private VodService vodService;
    
        //上传视频到阿里云
        @PostMapping("uploadAlyiVideo")
        public R uploadAlyiVideo(MultipartFile file) {
            //返回上传视频id
            String videoId = vodService.uploadVideoAly(file);
            return R.ok().data("videoId",videoId);
        }
    
        //根据视频id删除阿里云视频
        @DeleteMapping("removeAlyVideo/{id}")
        public R removeAlyVideo(@PathVariable String id) {
            try {
                //初始化对象
                DefaultAcsClient client = InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
                //创建删除视频request对象
                DeleteVideoRequest request = new DeleteVideoRequest();
                //向request设置视频id
                request.setVideoIds(id);
    
                //调用初始化对象的方法实现删除
                client.getAcsResponse(request);
                return R.ok();
            }catch(Exception e) {
                e.printStackTrace();
                throw new GuliException(20001,"删除视频失败");
            }
        }
    
        //删除多个阿里云视频的方法
        //参数多个视频id  List videoIdList
        @DeleteMapping("delete-batch")
        public R deleteBatch(@RequestParam("videoIdList") List<String> videoIdList) {
            vodService.removeMoreAlyVideo(videoIdList);
            return R.ok();
        }
    }

    6.service

    @Service
    public class VodServiceImpl implements VodService {
    
        @Override
        public String uploadVideoAly(MultipartFile file) {
    
            try {
                //accessKeyId, accessKeySecret
                //fileName:上传文件原始名称
                // 01.03.09.mp4
                String fileName = file.getOriginalFilename();
                //title:上传之后显示名称
                String title = fileName.substring(0, fileName.lastIndexOf("."));
                //inputStream:上传文件输入流
                InputStream inputStream = file.getInputStream();
                UploadStreamRequest request = new UploadStreamRequest(ConstantVodUtils.ACCESS_KEY_ID,ConstantVodUtils.ACCESS_KEY_SECRET, title, fileName, inputStream);
    
                UploadVideoImpl uploader = new UploadVideoImpl();
                UploadStreamResponse response = uploader.uploadStream(request);
    
                String videoId = null;
                if (response.isSuccess()) {
                    videoId = response.getVideoId();
                } else { //如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因
                    videoId = response.getVideoId();
                }
                return videoId;
            }catch(Exception e) {
                e.printStackTrace();
                return null;
            }
    
        }
    
        @Override
        public void removeMoreAlyVideo(List videoIdList) {
            try {
                //初始化对象
                DefaultAcsClient client = InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
                //创建删除视频request对象
                DeleteVideoRequest request = new DeleteVideoRequest();
    
                //videoIdList值转换成 1,2,3
                String videoIds = StringUtils.join(videoIdList.toArray(), ",");
    
                //向request设置视频id
                request.setVideoIds(videoIds);
                //调用初始化对象的方法实现删除
                client.getAcsResponse(request);
            }catch(Exception e) {
                e.printStackTrace();
                throw new GuliException(20001,"删除视频失败");
            }
        }
    
        public static void main(String[] args) {
            List<String> list = new ArrayList<>();
            list.add("11");
            list.add("22");
            list.add("33");
            // 11,22,33
            String join = StringUtils.join(list.toArray(), ",");
            System.out.println(join);
        }
    }
  • 相关阅读:
    HTML5新增标签,表单及属性
    css3背景属性
    移动端隐藏滚动条
    css3选择符
    Hibernate读书笔记条件查询
    Hibernate读书笔记事件机制
    Hibernate读书笔记SQL查询
    Hibernate读书笔记HQL查询
    Hibernate读书笔记Hibernate知识总结
    Hibernate读书笔记缓存
  • 原文地址:https://www.cnblogs.com/liuyi13535496566/p/12820526.html
Copyright © 2020-2023  润新知