韩源-末日生存手册

基于nodejs的前端直传oss

基于nodejs的前端直传oss

阿里云oss权限设置、跨域设置

权限设置

跨域设置

安装ali-oss

npm install ali-oss

获取client对象

getClient() {
      let OSS = require('ali-oss')
      this.client = new OSS({
          //服务器地区
        region: 'oss-cn-beijing'(填写你自己的),
        //Accesstoken中的keyid
        accessKeyId: ''(填写你自己的),
          //Accesstoken中的KeySecret
        accessKeySecret: ''(填写你自己的),
          //bucket实例名称
        bucket: ''(填写你自己的),
      })
    }

文件上传

handleFile() {
    //获取oss对象、client实例
      this.getClient()
    //取得用户选择的文件
      this.file = this.$refs.upload.files[0]
      var _this = this
      async function put() {
        try {
          let result = await _this.client.put(
              //传入文件名
            _this.$refs.upload.files[0].name,
              //传入文件
            _this.file
          )
          //获得结果中返回的url
          _this.avatar = result.url
          _this.changeMessage()
        } catch (e) {
          console.log(e)
        }
      }
      put()
    }

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »