阿里云视频 1 视频点播

Submitted by Lizhe on Wed, 12/30/2020 - 10:25

本例是一个 阿里云 视频点播 (不是直播)的 helloworld

这里用户浏览器先后调用两个服务器

20201230065309

我们先看 golang 端

/Users/lizhe/works/aus/alivedioauth/go.mod

 

module vedioAuth

 

go 1.14

 

require (

github.com/tidwall/gjson v1.6.0

github.com/tidwall/sjson v1.1.1

github.com/aliyun/alibaba-cloud-sdk-go v1.61.813

)

/Users/lizhe/works/aus/alivedioauth/main.go

 

package main

 

import (

"net/http"

handler "vedioAuth/handler"

)

 

func main() {

http.HandleFunc("/", handler.WithArgHandler)

http.ListenAndServe(":8000", nil)

}

 

/Users/lizhe/works/aus/alivedioauth/handler/withArg.go

 

package handler

 

import (

"fmt"

"net/http"

"os"

 

"github.com/aliyun/alibaba-cloud-sdk-go/services/vod"

)

 

func WithArgHandler(writer http.ResponseWriter, request *http.Request) {

value := getAuth()

writer.Header().Set("Access-Control-Allow-Origin", "*")

fmt.Fprintf(writer, string(value))

}

 

func getAuth() string {

key := os.Getenv("key")

pass := os.Getenv("pass")

client, err := vod.NewClientWithAccessKey("cn-hangzhou", key, pass)

 

request := vod.CreateGetVideoPlayAuthRequest()

request.Scheme = "https"

 

request.VideoId = "xxxxxxxxxxxx"

 

response, err := client.GetVideoPlayAuth(request)

if err != nil {

fmt.Print(err.Error())

}

fmt.Printf("response is %#v\n", response)

 

PlayAuth := response.PlayAuth

 

return PlayAuth

}

 

/Users/lizhe/works/aus/alivedioauth/Dockerfile

 

FROM golang:alpine as golang

 

RUN mkdir -p /root/vedioAuth

RUN mkdir -p /root/vedioAuth/output

COPY ./ /root/vedioAuth

 

WORKDIR /root/vedioAuth/output

RUN go build /root/vedioAuth/main.go


 

FROM alpine as alpine

 

RUN mkdir -p /root/vedioAuth

COPY --from=golang --chown=root:root /root/vedioAuth/output /root/vedioAuth

 

CMD /root/vedioAuth/main


 

此时访问 8000 端口会得到一个 auth token

20201230890065726

最后做一个简单的 html,调用一下

/Users/lizhe/works/aus/alivedioauth/home.html

 

<!DOCTYPE HTML>

<html>

 

<head>

<meta charset="UTF-8">

<meta http-equiv="x-ua-compatible" content="IE=edge">

<meta name="viewport"

content="width=device-width, height=device-height, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" />

<title>Vod</title>

<link rel="stylesheet" href="https://g.alicdn.com/de/prismplayer/2.6.0/skins/default/aliplayer-min.css" />

<script type="text/javascript" src="https://g.alicdn.com/de/prismplayer/2.6.0/aliplayer-min.js"></script>

<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>

</head>

 

<body>

<div class="prism-player" id="J_prismPlayer"></div>

<script>

var token = ""

$(document).ready(function () {

htmlobj = $.ajax({ url: "http://xxxxxx.com:8000/", async: false });

token = htmlobj.responseText

var player = new Aliplayer({

id: "J_prismPlayer",

autoplay: true,

width: "640px",

height: "480px",

vid: "xxxxxx",

playauth: token,

cover: ''

});

});

 

</script>

</body>

 

服务器什么的也不用了,直接本地打开这个html就行了

20201230070024