zoukankan      html  css  js  c++  java
  • 七牛云

    1、使用python获取上传使用的token值

    • 安装七牛云
      pip install qiniu==7.2.6

    • 获取token,这里是使用的工用方法,在utils下创建MyBaseView.py文件

    
    from qiniu import Auth
    
    # 七牛云上传视屏生成token
    def qiniu_token():
        # 需要填写你的 Access Key 和 Secret Key
        access_key = 'RHBatQttI9aPJvTMslxxSCilmmYZzOL2At0Tx6gm'
        secret_key = '1S860TXxZP5ihW21aNZyvXdKSZCrWbknBIy6DU4K'
        # 构建鉴权对象
        q = Auth(access_key, secret_key)
        # 要上传的空间
        bucket_name = '12345678900'
        # 3600为token过期时间,秒为单位。3600等于一小时
        token = q.upload_token(bucket_name, expires=3600)
        return token 
    
    • 在views.py里使用qiniu_token
    
    class QiNiuToken(APIView):
    
        def get(self,request):
            token = qiniu_token()
            print(token)
            data = {
                'code':0,
                'msg':'获取token成功',
                'data':{
                    'uptoken':token
                }
            }
            return Response(data
    
    • 定义URl
    from django.urls import path
    from . import views
    ​
    urlpatterns = [
        path('uptoken/', views.QiNiuToken.as_view()),  # 获取七牛云上传凭证
    ]
    

    2、在vue里面写入的内容

    <template>
      <div>
          <input type="file" name='upFile' id="upFile" @change='changeFile($event)'>
          <input type="button" name="开始上传" value="开始上传" @click='uploadFile()'>
          <img v-if="coverUrl" :src="coverUrl" alt="封面">
          <el-progress :percentage="filePercent"></el-progress>
          {{filePercent}}
      </div>
    </template>
    <script>
    import * as qiniu from "qiniu-js";
     
    export default {
      name:'qiniu',
      data() {
        return {
          file:null,
          videoUrl:null,
          coverUrl:null,
          filePercent:0
        };
      },
      methods: {
        changeFile(e){
        //   获取文件
          this.file = e.target.files[0];
        },
          // 获取uptoken
        get_uptoken() {
              axios({
                  url: 'http://127.0.0.1:8000/day01/uptoken/',
                  method: 'get'
              }).then(res=>{
                  this.uptoken = res.data.token
              })
          },
        uploadFile() {
        // 当前VUE中的this 是指向实例,相当于父级,指向指不到子级中。所需需要一个变量 _this 存储this得指向。
          let _this = this
        // 获取身份的token
          let token = _this.uptoken
        // 上传时的配置
          var config = {
            useCdnDomain: true
          };
        //  设置文件的配置
          var putExtra = {
            fname: "",
            params: {},
            mimeType: null
          };
    ​
        //   实例化七牛云的上传实例
          var observable = qiniu.upload(_this.file, null, token, putExtra, config);
        //   设置实例的监听对象
          var observer = {
            //   接收上传进度信息
            next(res) {
                // 上传进度
                _this.filePercent = parseInt(res.total.percent) 
                if(_this.filePercent==100){
                    console.log("success")
                } 
            },
            // 接收上传错误信息
            error(err) {
              console.log(err)
            },
    ​
            // 接收上传完成后的信息
            complete(res) {
                 console.log(res.key)
            }
          };
          // 上传开始
          var subscription = observable.subscribe(observer); 
        }
        
      },
        mounted() {
        this.get_uptoken()
       }
    }
    </script>
    
    
    
  • 相关阅读:
    基于thinkphp3.2.3开发的CMS内容管理系统(二)- Rbac用户权限
    phpstrom 快捷键
    基于thinkphp3.2.3开发的CMS内容管理系统
    html中的字幕滚动marquee属性
    学会这些网站优化技巧,秒变seo专家
    服务器设置防火墙规则,实现远程桌面连接的ip限制
    IIS7.5中神秘的ApplicationPoolIdentity
    mysql 安装成功后如何设置密码?
    网站优化提高加载速度的14个技巧
    解决帝国cms系统后台管理员登录密码输入五次密码错误后需等候60分钟的方法
  • 原文地址:https://www.cnblogs.com/yimeng123/p/14389345.html
Copyright © 2011-2022 走看看