摘要:在使用做前端項目需求視頻直傳阿里云服務器參考文檔阿里云官方導入官方的事件獲取上傳權限采用上傳方式,后臺獲取相關權限去上傳去上傳獲取視頻信息得到視頻封面圖片獲取視頻信息正在獲取縮略圖這里加定時器,等待響應下面是設置上傳進度分片大小
在使用vue做前端項目需求視頻直傳阿里云服務器
參考文檔(阿里云官方)
https://help.aliyun.com/docum...
導入官方sdk
input的@change事件
change(event) { let file = event.target.files[0] let url = URL.createObjectURL(file) this.getVideoUploadAuth(file) //獲取上傳權限 }
采用UploadAuthAndAddress上傳方式,后臺php獲取相關權限
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() //去上傳 }) }
去上傳
videoUploadFn() { uploader.startUpload(); _videoUploadSuccess.ok = false let interval = setInterval(() => { if(_videoUploadSuccess.ok) { clearInterval(interval) this.getVideoInfo() // 獲取視頻信息得到視頻封面圖片 } , 10); }
獲取視頻信息
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設置
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
完。
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/98650.html
摘要:月日,在云棲大會深圳峰會上,阿里云宣布年將戰略投入邊緣計算技術領域。月日,阿里巴巴集團宣布將列為繼云計算后新的主賽道,并戰略投入邊緣計算,在全球形成一朵無處不在的云。 3月28日,在2018云棲大會·深圳峰會上,阿里云宣布2018年將戰略投入邊緣計算技術領域。 showImg(https://segmentfault.com/img/bV7fOA?w=975&h=584); 同時,推出...
摘要:前端項目,基于框架實現阿里云圖片上傳單張安裝依賴包編寫后端提供數據直接使用使用的直傳完。 前端項目,基于vue框架實現阿里云圖片上傳(單張) 1.安裝依賴包 npm install ali-oss 2.編寫alioss.js var OSS = require(ali-oss) export function client() { var client = new OSS({ ...
閱讀 3274·2023-04-25 18:03
閱讀 1143·2021-11-15 11:38
閱讀 5522·2021-10-25 09:45
閱讀 840·2021-09-24 09:48
閱讀 2272·2021-09-22 15:34
閱讀 1734·2019-08-30 15:44
閱讀 2675·2019-08-30 13:12
閱讀 604·2019-08-29 16:05