我之前用的是服務端java上傳,不過后來因為換了key(accessKeyId,accessKeySecret)值后發現上傳變得很慢,有時候會快一點,不過也不是太影響流程,但是不管快慢都會出現無法立即獲取到上傳后的播放地址,然后我進斷點看了一下是可以的,發現是因為斷點往下進行的慢,如果進斷點后直接走完結束掉也是無法立即獲取的,最后也沒有解決掉,所以換成前端上傳了
好該上班了,直接代碼走一下(代碼都是官方demo的)
一、第一步 根據官方文檔 引入js文件
這一塊要說明一下 VUE2.0與3.0引入可能會有區別,那就可以按照我的方式來引入
引入根據自己的實際開發:
二、引入官方DEMO中的js代碼 ,官方給了兩種方式,我用的是推薦的上傳地址和憑證方式
1.上傳地址和憑證方式(推薦使用)
2.STS方式
我先把整體代碼搬出來 ,我用的前端UI組件是 Ant Design of Vue
<template> <div class="container"> <div class="setting"> <div class="input-control"> <label for="timeout">請求過期時間(構造參數 timeout, 默認 60000):</label> <input type="text" id="timeout" v-model="timeout" placeholder="輸入過期時間, 單位毫秒"> </div> <div class="input-control"> <label for="partSize">分片大小(構造參數 partSize, 默認 1048576):</label> <input type="text" class="form-control" id="partSize" v-model="partSize" placeholder="輸入分片大小, 單位bit, 最小100k"> </div> <div class="input-control"> <label for="parallel">上傳分片數(構造參數 parallel, 默認 5):</label> <input type="text" class="form-control" id="parallel" v-model="parallel" placeholder="輸入並行上傳分片個數, 默認為5"> </div> <div class="input-control"> <label for="retryCount">網絡失敗重試次數(構造參數 retryCount, 默認 3):</label> <input type="text" class="form-control" id="retryCount" v-model="retryCount" placeholder="輸入網絡失敗重試次數, 默認為3"> </div> <div class="input-control"> <label for="retryDuration">網絡失敗重試間隔(構造參數 retryDuration, 默認 2):</label> <input type="text" class="form-control" id="retryDuration" v-model="retryDuration" placeholder="輸入網絡失敗重試間隔, 默認2秒"> </div> <div class="input-control"> <label for="region">配置項 region, 默認 cn-shanghai:</label> <select v-model="region"> <option>cn-shanghai</option> <option>eu-central-1</option> <option>ap-southeast-1</option> </select> </div> <div class="input-control"> <label for="userId">阿里雲賬號ID</label> <input type="text" class="form-control" v-model="userId" disabled placeholder="輸入阿里雲賬號ID"> 集成產品后需要使用用戶自己的賬號ID,<a href="https://help.aliyun.com/knowledge_detail/37196.html "target="_blank">如何獲取帳號ID</a> </div> </div> <div class="upload"> <div> <input type="file" id="fileUpload" @change="fileChange($event)"> <label class="status">上傳狀態: <span>{{statusText}}</span></label> </div> <div class="upload-type"> 上傳方式一, 使用 UploadAuth 上傳: <button @click="authUpload" :disabled="uploadDisabled">開始上傳</button> <button @click="pauseUpload" :disabled="pauseDisabled">暫停</button> <button :disabled="resumeDisabled" @click="resumeUpload">恢復上傳</button> <span class="progress">上傳進度: <i id="auth-progress">{{authProgress}}</i> %</span> </div> </div> <div class="info">uploadAuth及uploadAddress參數請查看<a href="https://help.aliyun.com/document_detail/55407.html" target="_blank">獲取上傳地址和憑證</a></div> </div> </template> <script> import axios from 'axios' export default { data () { return { timeout: '', partSize: '', parallel: '', retryCount: '', retryDuration: '', region: 'cn-shanghai', userId: '1303984639806000', file: null, authProgress: 0, uploadDisabled: true, resumeDisabled: true, pauseDisabled: true, uploader: null, statusText: '', } }, methods: { fileChange (e) { this.file = e.target.files[0] if (!this.file) { alert("請先選擇需要上傳的文件!") return } var Title = this.file.name var userData = '{"Vod":{}}' if (this.uploader) { this.uploader.stopUpload() this.authProgress = 0 this.statusText = "" } this.uploader = this.createUploader() console.log(userData) this.uploader.addFile(this.file, null, null, null, userData) this.uploadDisabled = false this.pauseDisabled = true this.resumeDisabled = true }, authUpload () { // 然后調用 startUpload 方法, 開始上傳 if (this.uploader !== null) { this.uploader.startUpload() this.uploadDisabled = true this.pauseDisabled = false } }, // 暫停上傳 pauseUpload () { if (this.uploader !== null) { this.uploader.stopUpload() this.resumeDisabled = false this.pauseDisabled = true } }, // 恢復上傳 resumeUpload () { if (this.uploader !== null) { this.uploader.startUpload() this.resumeDisabled = true this.pauseDisabled = false } }, createUploader (type) { let self = this let uploader = new AliyunUpload.Vod({ timeout: self.timeout || 60000, partSize: self.partSize || 1048576, parallel: self.parallel || 5, retryCount: self.retryCount || 3, retryDuration: self.retryDuration || 2, region: self.region, userId: self.userId, // 添加文件成功 addFileSuccess: function (uploadInfo) { self.uploadDisabled = false self.resumeDisabled = false self.statusText = '添加文件成功, 等待上傳...' console.log("addFileSuccess: " + uploadInfo.file.name) }, // 開始上傳 onUploadstarted: function (uploadInfo) { // 如果是 UploadAuth 上傳方式, 需要調用 uploader.setUploadAuthAndAddress 方法 // 如果是 UploadAuth 上傳方式, 需要根據 uploadInfo.videoId是否有值,調用點播的不同接口獲取uploadauth和uploadAddress // 如果 uploadInfo.videoId 有值,調用刷新視頻上傳憑證接口,否則調用創建視頻上傳憑證接口 // 注意: 這里是測試 demo 所以直接調用了獲取 UploadAuth 的測試接口, 用戶在使用時需要判斷 uploadInfo.videoId 存在與否從而調用 openApi // 如果 uploadInfo.videoId 存在, 調用 刷新視頻上傳憑證接口(https://help.aliyun.com/document_detail/55408.html) // 如果 uploadInfo.videoId 不存在,調用 獲取視頻上傳地址和憑證接口(https://help.aliyun.com/document_detail/55407.html) if (!uploadInfo.videoId) { let createUrl = 'https://demo-vod.cn-shanghai.aliyuncs.com/voddemo/CreateUploadVideo?Title=testvod1&FileName=aa.mp4&BusinessType=vodai&TerminalType=pc&DeviceModel=iPhone9,2&UUID=59ECA-4193-4695-94DD-7E1247288&AppVersion=1.0.0&VideoId=5bfcc7864fc14b96972842172207c9e6' axios.get(createUrl).then(({data}) => { let uploadAuth = data.UploadAuth let uploadAddress = data.UploadAddress let videoId = data.VideoId uploader.setUploadAuthAndAddress(uploadInfo, uploadAuth, uploadAddress,videoId) }) self.statusText = '文件開始上傳...' console.log("onUploadStarted:" + uploadInfo.file.name + ", endpoint:" + uploadInfo.endpoint + ", bucket:" + uploadInfo.bucket + ", object:" + uploadInfo.object) } else { // 如果videoId有值,根據videoId刷新上傳憑證 // https://help.aliyun.com/document_detail/55408.html?spm=a2c4g.11186623.6.630.BoYYcY let refreshUrl = 'https://demo-vod.cn-shanghai.aliyuncs.com/voddemo/RefreshUploadVideo?BusinessType=vodai&TerminalType=pc&DeviceModel=iPhone9,2&UUID=59ECA-4193-4695-94DD-7E1247288&AppVersion=1.0.0&Title=haha1&FileName=xxx.mp4&VideoId=' + uploadInfo.videoId axios.get(refreshUrl).then(({data}) => { let uploadAuth = data.UploadAuth let uploadAddress = data.UploadAddress let videoId = data.VideoId uploader.setUploadAuthAndAddress(uploadInfo, uploadAuth, uploadAddress,videoId) }) } }, // 文件上傳成功 onUploadSucceed: function (uploadInfo) { console.log("onUploadSucceed: " + uploadInfo.file.name + ", endpoint:" + uploadInfo.endpoint + ", bucket:" + uploadInfo.bucket + ", object:" + uploadInfo.object) self.statusText = '文件上傳成功!' }, // 文件上傳失敗 onUploadFailed: function (uploadInfo, code, message) { console.log("onUploadFailed: file:" + uploadInfo.file.name + ",code:" + code + ", message:" + message) self.statusText = '文件上傳失敗!' }, // 取消文件上傳 onUploadCanceled: function (uploadInfo, code, message) { console.log("Canceled file: " + uploadInfo.file.name + ", code: " + code + ", message:" + message) self.statusText = '文件已暫停上傳' }, // 文件上傳進度,單位:字節, 可以在這個函數中拿到上傳進度並顯示在頁面上 onUploadProgress: function (uploadInfo, totalSize, progress) { console.log("onUploadProgress:file:" + uploadInfo.file.name + ", fileSize:" + totalSize + ", percent:" + Math.ceil(progress * 100) + "%") let progressPercent = Math.ceil(progress * 100) self.authProgress = progressPercent self.statusText = '文件上傳中...' }, // 上傳憑證超時 onUploadTokenExpired: function (uploadInfo) { // 上傳大文件超時, 如果是上傳方式一即根據 UploadAuth 上傳時 // 需要根據 uploadInfo.videoId 調用刷新視頻上傳憑證接口(https://help.aliyun.com/document_detail/55408.html)重新獲取 UploadAuth // 然后調用 resumeUploadWithAuth 方法, 這里是測試接口, 所以我直接獲取了 UploadAuth let refreshUrl = 'https://demo-vod.cn-shanghai.aliyuncs.com/voddemo/RefreshUploadVideo?BusinessType=vodai&TerminalType=pc&DeviceModel=iPhone9,2&UUID=59ECA-4193-4695-94DD-7E1247288&AppVersion=1.0.0&Title=haha1&FileName=xxx.mp4&VideoId=' + uploadInfo.videoId axios.get(refreshUrl).then(({data}) => { let uploadAuth = data.UploadAuth uploader.resumeUploadWithAuth(uploadAuth) console.log('upload expired and resume upload with uploadauth ' + uploadAuth) }) self.statusText = '文件超時...' }, // 全部文件上傳結束 onUploadEnd: function (uploadInfo) { console.log("onUploadEnd: uploaded all the files") self.statusText = '文件上傳完畢' } }) return uploader } } } </script>
我自己的代碼
根據自己的組件進行修改,我沒有用官方DEMO上的分片大小、上傳分片數等數據,用的都是默認的沒有寫活,可根據情況來定
接下來是我對方法的理解
fileChange 是選擇文件調用的方法
開始上傳的方法
接下來就是createUploader下面的onUploadstarted開始上傳的方法
這個方法就是根據上面的判斷,uploadInfo.videoId 是否有值,來決定具體執行哪個方法
如果 uploadInfo.videoId 存在, 調用 刷新視頻上傳憑證接口(https://help.aliyun.com/document_detail/55408.html)
這個刷新視頻也是有DEMO的 https://help.aliyun.com/document_detail/61063.html?spm=a2c4g.11186623.6.909.38ed3815y2BIhl
如果 uploadInfo.videoId 不存在,調用 獲取視頻上傳地址和憑證接口(https://help.aliyun.com/document_detail/55407.html)
這兩個就是上圖需要替換的接口地址,需要你后台來進行編輯后,給前端接口地址
接口返回的也就是上圖和代碼所對應的
至此也就差不多結束了,可能有點亂,多多包涵
有問題可加微信聯系我一起學習探討 : 18237185359