aliyun 对象存储oos及视频点播

一.对象存储

1.准备

阿里云官网:https://www.aliyun.com/?spm=5176.12818093.nav-right.dofficial.488716d0lAQiWy

1.1 充几毛钱就行

aliyun 对象存储oos及视频点播_第1张图片

1.2找到对象存储oos

aliyun 对象存储oos及视频点播_第2张图片

1.3 免费开通,进入管理控制台,获取keyId和keySecret

aliyun 对象存储oos及视频点播_第3张图片

1.4 创建bucket(拿到bucketName和Endpoint)

aliyun 对象存储oos及视频点播_第4张图片
上述步骤可以拿到以下参数

		String endpoint =PropertiesUtils.END_POINT;
        String accessKeyId = PropertiesUtils.KEY_ID;
        String accessKeySecret = PropertiesUtils.KEY_SECRET;
        String bucketName = PropertiesUtils.BUCKET_NAME;

1.5 上传文件至对象存储oss(代码实现)

@Service
public class OssServiceImpl implements OssService {
    @Override
    public String uploadFile(MultipartFile file) {
        String endpoint =PropertiesUtils.END_POINT;
        String accessKeyId = PropertiesUtils.KEY_ID;
        String accessKeySecret = PropertiesUtils.KEY_SECRET;
        String bucketName = PropertiesUtils.BUCKET_NAME;
        String url=null;
        try {
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            InputStream inputStream = file.getInputStream();
            String fileName = file.getOriginalFilename();
            ossClient.putObject("mario-plus", fileName, inputStream);
            ossClient.shutdown();
            url = "https://"+bucketName+"."+endpoint+"/"+fileName;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return url;
    }
}

上述代码,通过endpoint, accessKeyId, accessKeySecret三个参数获取OSS对象,使用oss对象调用putObject( )方法上传文件,上传成功后需要自行组装url
url = “https://”+bucketName+"."+endpoint+"/"+fileName;
通过该url可以直接显示或下载文件

二. 视频点播

先踩一个坑:
上传视频的时候,需要阿里的一个依赖 aliyun-sdk-vod-upload,但是该jar并没有开源,所以要手动下载导入到maven仓库中才能正常使用
附上地址:https://help.aliyun.com/document_detail/51992.html?spm=a2c4g.11186623.6.1029.2dab6cecZfMGvO
aliyun 对象存储oos及视频点播_第5张图片
导入jar进maven仓库参考 https://blog.csdn.net/m0_46701838/article/details/106136831 注意版本

上述解压过程同时会拿到上传视频参考文件
aliyun 对象存储oos及视频点播_第6张图片

2.1 上传视频

@Test
    public void test(){
        UploadVideoRequest request = new UploadVideoRequest(keyId, keySecret, "fileName", filePath);
        request.setPartSize(2 * 1024 * 1024L);
        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");
        } 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");
        }
    }

2.2 点播视频

参考官网开发文档:https://help.aliyun.com/document_detail/61063.html?spm=a2c4g.11186623.6.909.1aaa24d8qlLJN0

初始化(获取DefaultAcsClient对象)

	    //使用AccessKey初始化,获取到client对象
        String regionId ="cn-shanghai";
        DefaultProfile profile = DefaultProfile.getProfile(regionId,keyId, keySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);

根据视频id获取播放地址

		GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId("cf45f43797b044cb834743ff0d1dd82d");//视频id
        GetPlayInfoResponse response = client.getAcsResponse(request);
        List<GetPlayInfoResponse.PlayInfo> 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());

你可能感兴趣的:(aliyun 对象存储oos及视频点播)