1. 程式人生 > 程式設計 >SpringBoot+阿里雲OSS實現線上視訊播放的示例

SpringBoot+阿里雲OSS實現線上視訊播放的示例

阿里雲 OSS 是一種雲端儲存技術,你可以理解為雲盤,我們的目標是將視訊儲存到雲端,然後在前端讀取並播放視訊。

OSS

首先登陸首頁,建立一個儲存桶:https://oss.console.aliyun.com

SpringBoot+阿里雲OSS實現線上視訊播放的示例

然後找到讀寫許可權:

SpringBoot+阿里雲OSS實現線上視訊播放的示例

將讀寫許可權設定為公共讀即可:

SpringBoot+阿里雲OSS實現線上視訊播放的示例

在 RAM 中新建一個使用者:

SpringBoot+阿里雲OSS實現線上視訊播放的示例

為其新增許可權,選擇 OSS 的許可權:

SpringBoot+阿里雲OSS實現線上視訊播放的示例

然後點進去這個使用者,找到 AccessKey:

SpringBoot+阿里雲OSS實現線上視訊播放的示例

建立之後記下來 secret ,因為他只出現一次,如果沒記住也沒事,可以重新建立新的 key

下面開始編寫服務端程式碼:

POM

<!-- 阿里雲oss -->
<dependency>
  <groupId>com.aliyun.oss</groupId>
  <artifactId>aliyun-sdk-oss</artifactId>
  <version>3.10.2</version>
</dependency>
package com.lsu.file.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.AppendObjectRequest;
import com.aliyun.oss.model.AppendObjectResult;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.lsu.server.dto.FileDto;
import com.lsu.server.dto.ResponseDto;
import com.lsu.server.enums.FileUseEnum;
import com.lsu.server.service.FileService;
import com.lsu.server.util.Base64ToMultipartFile;
import com.lsu.server.util.UuidUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;

/**
 * @author wsuo
 */
@RestController
@RequestMapping("/admin")
public class OssController {

  private static final Logger LOG = LoggerFactory.getLogger(FileController.class);

  @Value("${oss.accessKeyId}")
  private String accessKeyId;

  @Value("${oss.accessKeySecret}")
  private String accessKeySecret;

  @Value("${oss.endpoint}")
  private String endpoint;

  @Value("${oss.bucket}")
  private String bucket;

  @Value("${oss.domain}")
  private String ossDomain;

  public static final String BUSINESS_NAME = "OSS檔案上傳";

  @Resource
  private FileService fileService;

  @PostMapping("/oss-append")
  public ResponseDto<FileDto> fileUpload(@RequestBody FileDto fileDto) throws Exception {
    LOG.info("上傳檔案開始");
    String use = fileDto.getUse();
    String key = fileDto.getKey();
    String suffix = fileDto.getSuffix();
    Integer shardIndex = fileDto.getShardIndex();
    Integer shardSize = fileDto.getShardSize();
    String shardBase64 = fileDto.getShard();
    MultipartFile shard = Base64ToMultipartFile.base64ToMultipart(shardBase64);

    FileUseEnum useEnum = FileUseEnum.getByCode(use);
    String dir = useEnum.name().toLowerCase();

    String path = dir +
        "/" +
        key +
        "." +
        suffix;

    // 建立OSSClient例項。
    OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);

    ObjectMetadata meta = new ObjectMetadata();
    // 指定上傳的內容型別。
    meta.setContentType("text/plain");

    // 通過AppendObjectRequest設定多個引數。
    AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucket,path,new ByteArrayInputStream(shard.getBytes()),meta);

    appendObjectRequest.setPosition((long) ((shardIndex - 1) * shardSize));
    AppendObjectResult appendObjectResult = ossClient.appendObject(appendObjectRequest);
    // 檔案的64位CRC值。此值根據ECMA-182標準計算得出。
    System.out.println(appendObjectResult.getObjectCRC());
    System.out.println(JSONObject.toJSONString(appendObjectResult));

    ossClient.shutdown();

    LOG.info("儲存檔案記錄開始");
    fileDto.setPath(path);
    fileService.save(fileDto);

    ResponseDto<FileDto> responseDto = new ResponseDto<>();
    fileDto.setPath(ossDomain + path);
    responseDto.setContent(fileDto);
    return responseDto;
  }


  @PostMapping("/oss-simple")
  public ResponseDto<FileDto> fileUpload(@RequestParam MultipartFile file,String use) throws Exception {
    LOG.info("上傳檔案開始");
    FileUseEnum useEnum = FileUseEnum.getByCode(use);
    String key = UuidUtil.getShortUuid();
    String fileName = file.getOriginalFilename();
    String suffix = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
    String dir = useEnum.name().toLowerCase();
    String path = dir + "/" + key + "." + suffix;

    // 建立OSSClient例項。
    OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeySecret);
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucket,new ByteArrayInputStream(file.getBytes()));
    ossClient.putObject(putObjectRequest);

    ResponseDto<FileDto> responseDto = new ResponseDto<>();
    FileDto fileDto = new FileDto();
    fileDto.setPath(ossDomain + path);
    responseDto.setContent(fileDto);

    return responseDto;
  }
}

這部分內容可以參考阿里雲的幫助手冊:https://help.aliyun.com/document_detail/32011.html?spm=a2c4g.11174283.6.915.443b7da2mfhbKq

上面寫的是兩個介面:

  • 追加上傳:/oss-append
  • 簡單上傳:/oss-simple

注意這裡的引數都已經在 yml 檔案中定義了:

SpringBoot+阿里雲OSS實現線上視訊播放的示例

上面的 KeyId 和 KeySecret 就是之前在建立使用者時給的那兩個,填上就行了。

在前端我們就可以傳送請求獲取資料,注意這裡的物件是我自定義的,大家可以根據專案需求自行設定。

_this.$ajax.post(process.env.VUE_APP_SERVER + '/file/admin/oss-simple',formData).then(response => {
 Loading.hide();
 let resp = response.data;
 _this.afterUpload(resp);
 // 清空原來控制元件中的值
 $("#" + _this.inputId + "-input").val("");
})

視訊點播

VOD 是另一種視訊儲存的形式,它的功能更豐。阿里雲視訊點播(VOD)是集音視訊上傳、自動化轉碼處理、媒體資源管理、分發加速於一體的全鏈路音視訊點播服務。

我們同樣需要一個 VOD 的使用者,給它賦予 VOD 的許可權:

SpringBoot+阿里雲OSS實現線上視訊播放的示例

SDK 的使用可以參考文件:https://help.aliyun.com/document_detail/61063.html?spm=a2c4g.11186623.6.921.418f192bTDCIJN

我們可以在轉碼組設定自己的模板,然後通過 ID 在程式碼中使用:

SpringBoot+阿里雲OSS實現線上視訊播放的示例

上傳視訊比較簡單,和 OSS 很像,但是播放視訊要多一個條件,在獲取播放連結之前要先取得許可權認證,所以下面單獨寫了一個 /get-auth/{vod} 介面,其中的引數就是 vod 的 ID,這個 ID 在我們上傳視訊之後會作為返回值返回的。

package com.lsu.file.controller.admin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSSClient;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetMezzanineInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.lsu.server.dto.FileDto;
import com.lsu.server.dto.ResponseDto;
import com.lsu.server.enums.FileUseEnum;
import com.lsu.server.service.FileService;
import com.lsu.server.util.Base64ToMultipartFile;
import com.lsu.server.util.VodUtil;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author wsuo
 */
@RestController
@RequestMapping("/admin")
public class VodController {

  private static final Logger LOG = LoggerFactory.getLogger(FileController.class);

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

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

  public static final String BUSINESS_NAME = "VOD視訊上傳";

  @Resource
  private FileService fileService;

  @PostMapping("/vod")
  public ResponseDto<FileDto> fileUpload(@RequestBody FileDto fileDto) throws Exception {
    String use = fileDto.getUse();
    String key = fileDto.getKey();
    String suffix = fileDto.getSuffix();
    Integer shardIndex = fileDto.getShardIndex();
    Integer shardSize = fileDto.getShardSize();
    String shardBase64 = fileDto.getShard();
    MultipartFile shard = Base64ToMultipartFile.base64ToMultipart(shardBase64);

    FileUseEnum useEnum = FileUseEnum.getByCode(use);
    String dir = useEnum.name().toLowerCase();

    String path = dir +
        "/" +
        key +
        "." +
        suffix;

    //需要上傳到VOD的本地視訊檔案的完整路徑,需要包含副檔名
    String vod = "";
    String fileUrl = "";
    try {
      // 初始化VOD客戶端並獲取上傳地址和憑證
      DefaultAcsClient vodClient = VodUtil.initVodClient(accessKeyId,accessKeySecret);
      CreateUploadVideoResponse createUploadVideoResponse = VodUtil.createUploadVideo(vodClient,path);
      // 執行成功會返回VideoId、UploadAddress和UploadAuth
      vod = createUploadVideoResponse.getVideoId();
      JSONObject uploadAuth = JSONObject.parseObject(
          Base64.decodeBase64(createUploadVideoResponse.getUploadAuth()),JSONObject.class);
      JSONObject uploadAddress = JSONObject.parseObject(
          Base64.decodeBase64(createUploadVideoResponse.getUploadAddress()),JSONObject.class);
      // 使用UploadAuth和UploadAddress初始化OSS客戶端
      OSSClient ossClient = VodUtil.initOssClient(uploadAuth,uploadAddress);
      // 上傳檔案,注意是同步上傳會阻塞等待,耗時與檔案大小和網路上行頻寬有關
      if (shard != null) {
        VodUtil.uploadLocalFile(ossClient,uploadAddress,shard.getInputStream());
      }
      System.out.println("上傳視訊成功,vod : " + vod);
      GetMezzanineInfoResponse response = VodUtil.getMezzanineInfoResponse(vodClient,vod);
      System.out.println("獲取視訊資訊 response = " + JSON.toJSONString(response));
      fileUrl = response.getMezzanine().getFileURL();
      ossClient.shutdown();
    } catch (Exception e) {
      System.out.println("上傳視訊失敗,ErrorMessage : " + e.getLocalizedMessage());
    }

    fileDto.setPath(path);
    fileDto.setVod(vod);
    fileService.save(fileDto);
    ResponseDto<FileDto> responseDto = new ResponseDto<>();
    fileDto.setPath(fileUrl);
    responseDto.setContent(fileDto);
    return responseDto;
  }

  @RequestMapping(value = "/get-auth/{vod}",method = RequestMethod.GET)
  public ResponseDto<String> getAuth(@PathVariable String vod) {
    LOG.info("獲取播放授權開始");
    ResponseDto<String> responseDto = new ResponseDto<>();
    DefaultAcsClient client = VodUtil.initVodClient(accessKeyId,accessKeySecret);
    GetVideoPlayAuthResponse response;
    try {
      response = VodUtil.getVideoPlayAuthResponse(client,vod);
      String playAuth = response.getPlayAuth();
      //播放憑證
      LOG.info("授權碼 = {}",playAuth);
      responseDto.setContent(playAuth);
      //VideoMeta資訊
      LOG.info("VideoMeta資訊 = {}",response.getVideoMeta().getTitle());
    } catch (Exception e) {
      System.out.print("ErrorMessage = " + e.getLocalizedMessage());
    }
    LOG.info("獲取播放授權結束");
    return responseDto;
  }
}
methods: {
 playUrl(url) {
  let _this = this;
  console.log("開始播放:",url);
  // 如果已經有播放器了 就將播放器刪除
  if (_this.aliPlayer) {
   _this.aliPlayer = null;
   $("#" + _this.playerId + '-player').remove();
  }
  // 初始化播放器
  $("#" + _this.playerId).append("<div class=\"prism-player\" id=\"" + _this.playerId + "-player\"></div>");
  _this.aliPlayer = new Aliplayer({
   id: _this.playerId + '-player',width: '100%',autoplay: true,//支援播放地址播放,此播放優先順序最高
   source: url,cover: 'http://liveroom-img.oss-cn-qingdao.aliyuncs.com/logo.png'
  },function (player) {
   console.log("播放器建立好了")
  })
 },playVod(vod) {
  let _this = this;
  Loading.show();
  _this.$ajax.get(process.env.VUE_APP_SERVER + '/file/admin/get-auth/' + vod).then((response) => {
   Loading.hide();
   let resp = response.data;
   if (resp.success) {
    //如果已經有播放器了,則將播放器div刪除
    if (_this.aliPlayer) {
     _this.aliPlayer = null;
     $("#" + _this.playerId + '-player').remove();
    }
    // 初始化播放器
    $("#" + _this.playerId).append("<div class=\"prism-player\" id=\"" + _this.playerId + "-player\"></div>");
    _this.aliPlayer = new Aliplayer({
     id: _this.playerId + '-player',autoplay: false,vid: vod,playauth: resp.content,cover: 'http://liveroom-img.oss-cn-qingdao.aliyuncs.com/logo.png',encryptType: 1,//當播放私有加密流時需要設定。
    },function (player) {
     console.log('播放器建立好了。')
    });
   } else {
    Toast.warning('播放錯誤。')
   }
  });
 }
},

上述的前端程式碼只是一個例子,playVod 呼叫的是阿里的播放器。

到此這篇關於SpringBoot+阿里雲OSS實現線上視訊播放的示例的文章就介紹到這了,更多相關SpringBoot阿里雲OSS線上視訊內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!