微信小程序-上传视频到阿里云oss

微信小程序-上传视频到阿里云oss

        addMyContent() {
                console.log('上传视频');
                //  environment  当前服务器环境
                // this.$store.state.device_info.device_did //用户id
                this.g_dirname = environment + '/ds/' + this.$store.state.device_info.uid + '/'
                this.getAccessKeyId();
        },
        getAccessKeyId() {
            let auth = {
                auth_token: this.$store.state.token,
                device_type: 71,
                auth_id: this.$store.state.device_info.device_did
            };
            let params = {
                expire_tm: '900',
                bt: 'wcmp'
            };
            this.request.getAccessKeyId(params, auth).then(res => {
                if (res.rst === 'ok') {
                    let data = res.data[0]['Credentials'];
                    let time = new Date().getTime() + 10 * 60 * 1000;
                    var Credentials = {
                        AccessKeyId: data['AccessKeyId'],
                        AccessKeySecret: data['AccessKeySecret'],
                        SecurityToken: data['SecurityToken'],
                        time: time
                    };
                    this.Credentials = Credentials;
                    wx.setStorageSync('refresh_ali_sts', Credentials);
                    this.chooseVideo();
                }
            });
        },
        //选择视频
        chooseVideo() {
                uni.chooseVideo({
                count: 1,
                sourceType: ['camera', 'album'],
                camera: 'back',
                success: res => {
                    uni.showLoading({
                        title:'上传中...',
                        icon:'none'
                    })
                    var duration = Math.floor(res.duration);
                    console.log('duration', duration);
                    if (duration <= 4) {
                        wx.showModal({
                            title: '错误提醒',
                            content: '请上传大于4秒的视频',
                            showCancel: false
                        });
                        uni.hideLoading();
                    } else if (duration > 60) {
                        wx.showModal({
                            title: '错误提醒',
                            content: '视频过长,请上传一分钟内的视频',
                            showCancel: false
                        });
                        uni.hideLoading();
                    } else {
                        this.video_info = res
                        uni.getFileInfo({
                            filePath: res.tempFilePath,
                            digestAlgorithm: 'md5',
                            success: item => {
                                console.log(item);
                                let md5 = item.digest; //md5值
                                let suffix = this.get_suffix(res.tempFilePath);
                                let g_object_name = this.g_dirname + md5 + suffix;
                                this.sourceUrl = this.baseUrl + g_object_name;  //baseUrl  阿里云地址
                                let video_name = md5 + suffix;
                                this.uploadFileVideo(res.tempFilePath, g_object_name, video_name,md5);
                            }
                        });
                    }
                }
            });
        },
         get_suffix(filename) {
            let pos = filename.lastIndexOf('.');
            let suffix = '';
            if (pos != -1) {
                suffix = filename.substring(pos);
            }
            return suffix;
        },
       uploadFileVideo(tempFilePath, g_object_name, video_name,md5) {
            let data = this.Credentials;
            let policyBase64 = Base64.encode(
                JSON.stringify({
                    expiration: '2029-01-01T12:00:00.000Z', // 设置该Policy的失效时间,超过这个失效时间之后,就没有办法通过这个policy上传文件了
                    conditions: [
                        ['content-length-range', 0, 1048576000] // 设置上传文件的大小限制
                    ]
                })
            );
            console.log(policyBase64);
            let message = policyBase64;
            var Crypto = require('../../utils/cryptojs/cryptojs.js').Crypto;  //这里使用cryptojs(https://github.com/gwjjeff/cryptojs/archive/master.zip)
            var bytes = Crypto.HMAC(Crypto.SHA1, message, data['AccessKeySecret'], { asBytes: true });
            var signature = Crypto.util.bytesToBase64(bytes);
            let params = {
                key: g_object_name,
                policy: policyBase64,
                OSSAccessKeyId: data['AccessKeyId'],
                success_action_status: '200', // 让服务端返回200,不然,默认会返回204
                signature: signature,
                'x-oss-security-token': data['SecurityToken']
            };
            console.log(params);
            let url = ''; //服务器阿里云地址
            this.request.uploadFileVideo(url, tempFilePath, params).then(res => {
                if (res.statusCode == 200) {
                     console.log(res);
                     //到这里就已经上传完了
                         ....
                }
            });
        },
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值