在使用vue作前端項目需求視頻直傳阿里雲服務器
參考文檔(阿里雲官方)
https://help.aliyun.com/docum...php
導入官方sdkhtml
<script src="/static/aliyun/aliyun-oss-sdk-5.2.0.min.js"></script> <script src="/static/aliyun/aliyun-upload-sdk-1.4.0.min.js"></script>
input的@change事件前端
change(event) { let file = event.target.files[0] let url = URL.createObjectURL(file) this.getVideoUploadAuth(file) //獲取上傳權限 }
採用UploadAuthAndAddress上傳方式,後臺php獲取相關權限vue
getVideoUploadAuth(file) { let path = document.getElementById('file').value let params = { video_title: file.name, video_url: path } axios.post('https://xxx/aliapi/upload.php', params).then(res => { uploadAuth = res.data.UploadAuth uploadAddress = res.data.UploadAddress videoId = res.data.VideoId uploader.addFile(file, null, null, null, userData); this.videoUploadFn() //去上傳 }) }
去上傳ios
videoUploadFn() { uploader.startUpload(); _videoUploadSuccess.ok = false let interval = setInterval(() => { if(_videoUploadSuccess.ok) { clearInterval(interval) this.getVideoInfo() // 獲取視頻信息獲得視頻封面圖片 } , 10); }
獲取視頻信息axios
getVideoInfo() { let sett this.$post(`https://xxxx/ali/info.php`,{videoId :_videoUploadSuccess.uploadInfo.videoId}).then(res => { res = res.data xmview.showLoading('正在獲取縮略圖..') if (res.Video.CoverURL) { this.posterimg = res.Video.CoverURL sessionStorage.setItem('video_content',_videoUploadSuccess.uploadInfo.videoId) sessionStorage.setItem('video_thumb',res.Video.CoverURL) xmview.hideLoading() clearTimeout(sett) } else { // 這裏加定時器,等待響應 sett = setTimeout(() => { this.getVideoInfo() }, 3000); } }) }
下面是sdk設置api
var uploadAuth = '' var uploadAddress = '' var videoId = '' var _videoUploadSuccess = { ok: false, uploadInfo: {} } var _uploadprogress = 0 // 上傳進度 const uploader = new AliyunUpload.Vod({ //分片大小默認1M,不能小於100K partSize: 1048576, //並行上傳分片個數,默認5 parallel: 5, //網絡緣由失敗時,從新上傳次數,默認爲3 retryCount: 3, //網絡緣由失敗時,從新上傳間隔時間,默認爲2秒 retryDuration: 2, // 開始上傳 'onUploadstarted': function(uploadInfo) { console.log("onUploadStarted:" + uploadInfo.file.name + ", endpoint:" + uploadInfo.endpoint + ", bucket:" + uploadInfo.bucket + ", object:" + uploadInfo.object); //上傳方式1, 須要根據uploadInfo.videoId是否有值,調用點播的不一樣接口獲取uploadauth和uploadAddress,若是videoId有值,調用刷新視頻上傳憑證接口,不然調用建立視頻上傳憑證接口 uploader.setUploadAuthAndAddress(uploadInfo, uploadAuth, uploadAddress, videoId); }, // 文件上傳成功 'onUploadSucceed': function(uploadInfo) { _videoUploadSuccess.ok = true _videoUploadSuccess.uploadInfo = uploadInfo console.log("onUploadSucceed: " + uploadInfo.file.name + ", endpoint:" + uploadInfo.endpoint + ", bucket:" + uploadInfo.bucket + ", object:" + uploadInfo.object); }, // 文件上傳失敗 'onUploadFailed': function(uploadInfo, code, message) { console.log("onUploadFailed: file:" + uploadInfo.file.name + ",code:" + code + ", message:" + message); }, // 文件上傳進度,單位:字節 'onUploadProgress': function(uploadInfo, totalSize, loadedPercent) { _uploadprogress = Math.ceil(loadedPercent * 100) console.log("onUploadProgress:file:" + uploadInfo.file.name + ", fileSize:" + totalSize + ", percent:" + Math.ceil(loadedPercent * 100) + "%"); }, // 上傳憑證超時 'onUploadTokenExpired': function(uploadInfo) { console.console.log("onUploadTokenExpired"); //上傳方式1 實現時,根據uploadInfo.videoId調用刷新視頻上傳憑證接口從新獲取UploadAuth uploader.resumeUploadWithAuth(uploadAuth); // 上傳方式2 實現時,重新獲取STS臨時帳號用於恢復上傳 // uploader.resumeUploadWithSTSToken(accessKeyId, accessKeySecret, secretToken, expireTime); }, //所有文件上傳結束 'onUploadEnd': function(uploadInfo) { console.log("onUploadEnd: uploaded all the files"); } }); const userData = '{"Vod":{"UserData":{"IsShowWaterMark":"false","Priority":"7"}}}'
2018-10-24
完。服務器