阿里云视频点播VOD

20 篇文章 0 订阅

依赖

<dependency>
    <groupId>com.aliyun.oss</groupId>
    <artifactId>aliyun-sdk-oss</artifactId>
    <version>3.10.2</version>
</dependency>

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-vod</artifactId>
    <version>2.15.11</version>
</dependency>

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-vod-upload</artifactId>
    <version>1.4.13</version>
</dependency>

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>4.5.1</version>
</dependency>

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.62</version>
</dependency>

配置

spring:
  servlet:
    multipart:
      max-file-size: 1024MB
      max-request-size: 2048MB
  cloud:
    alibaba:
      access-key: xxxxxxxx
      secret-key: xxxxxxxxxxxx
      vod:
        templateGroup: aaaaaaaaaaaaaa
        workflow: bbbbbbbbbbbbbb

属性类

@Data
@Component
public class VodProperties {
    @Value("${spring.cloud.alibaba.access-key}")
    private String accessKey;

    @Value("${spring.cloud.alibaba.secret-key}")
    private String secretKey;

    @Value("${spring.cloud.alibaba.vod.templateGroup}")
    private String templateGroup;

    @Value("${spring.cloud.alibaba.vod.workflow}")
    private String workflow;
}

Service

public interface VideoService {
    /**
     * 视频上传
     *
     * @param in 文件流
     * @param originalFileName 文件名
     * @return 视频id
     */
    String videoUpload(InputStream in, String originalFileName);
}

实现类

@Service
@Slf4j
public class VideoServiceImpl implements VideoService {
    @Autowired
    private VodProperties vodProperties;

    @Override
    public String videoUpload(InputStream in, String originalFileName) {
        String title = originalFileName.substring(originalFileName.lastIndexOf("."));
        UploadStreamRequest request = new UploadStreamRequest(vodProperties.getAccessKey(),
                vodProperties.getSecretKey(), title, originalFileName, in);
        request.setTemplateGroupId(vodProperties.getTemplateGroup());
        request.setWorkflowId(vodProperties.getWorkflow());
        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadStreamResponse response = uploader.uploadStream(request);
        String videoId = response.getVideoId();
        if (StringUtils.isBlank(videoId)) {
            log.error("{}: {}", ResultCode.VIDEO_UPLOAD_ALIYUN_ERRROR.getMessage(), response.getMessage());
            throw new CustomException(ResultCode.VIDEO_UPLOAD_ALIYUN_ERRROR);
        }
        return videoId;
    }

    public Long addCategory(Long parentId, String cateName) throws Exception {
        if (StringUtils.isNotBlank(cateName)) {
            return null;
        }
        String regionId = "cn-shanghai";
        DefaultProfile profile = DefaultProfile.getProfile(regionId,
                vodProperties.getAccessKey(), vodProperties.getSecretKey());
        DefaultAcsClient client = new DefaultAcsClient(profile);
        AddCategoryRequest request = new AddCategoryRequest();
        if (parentId != null) {
            request.setParentId(parentId);
        }
        request.setCateName(cateName);

        request.setParentId(-1L);
        request.setCateName("CateName");
        AddCategoryResponse response = client.getAcsResponse(request);
        return response.getCategory().getCateId();
    }
}

访问接口

@RestController
@RequestMapping("/admin/vod/video")
@Slf4j
@Api(description = "阿里云视频点播")
public class VideoController {
    @Autowired
    private VideoService videoService;

    @PostMapping("upload")
    public Result upload(@ApiParam(value = "视频文件", required = true) @RequestParam(value = "file") MultipartFile file) {
        String videoId;
        try {
            videoId = videoService.videoUpload(file.getInputStream(), file.getOriginalFilename());
        } catch (IOException e) {
            log.error("{}: {}", ResultCode.VIDEO_UPLOAD_TOMCAT_ERROR.getMessage(), e.getMessage());
            return Result.error(ResultCode.VIDEO_UPLOAD_TOMCAT_ERROR);
        }
        return Result.ok().data(videoId);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值