小程序 阿里云oss私有Bucket内部Object的上传及下载

前言:目前阿里云公有Bucket的上传网上已有很多,下载只需直接访问连接。本文介绍私有Bucket的上传及下载。

1.上传

小程序要上传文件必须使用微信的 wx.uploadFile方法

因此需要配置阿里云参数

key:你想要的文件名

policy:用户表单上传的策略(Policy),是经过base64编码过的字符串。

accessid:用户请求的accessid。

signature:对变量policy签名后的字符串。

expire:上传策略失效时间,在PolicyText里指定。在失效时间之前,都可以利用此Policy上传文件,所以没有必要每次上传都去服务端获取签名。

在util.js编写通用请求方法


function uploadimag(path) {

  return new Promise((resove, reject) => {

    wx.request({

      method: 'GET',

      url: baseurl + '/user/aliyunSTS/getAliyunSignature',//请求服务器获取oss配置

      data: '',

      header: {

        'content-type': 'application/json',

        'jwt': wx.getStorageSync('jwt'),//我们使用jwt鉴权

      },

      success(res) {

        if (parseInt(res.data.status) == 0) {

          var key = res.data.retValue.dir + 'patient/' + new Date().getTime() + Math.floor(Math.random() * 999) + '.png';

          wx.uploadFile({

            url: 'https://' + point + '.oss-cn-hangzhou.aliyuncs.com', //开发者服务器 url

            filePath: path, //要上传文件资源的路径

            name: 'file', //必须填file

            formData: {

              'key': key,

              'policy': res.data.retValue.policy,

              'OSSAccessKeyId': res.data.retValue.accessid,

              'signature': res.data.retValue.signature,

              'success_action_status': '200',

            },

            success: function(re) {

              if (re.statusCode != 200) {

                reject('上传失败');

                console.log('上传失败');

              } else

                resove(key);

              console.log('上传成功');

            },

            fail: function(err) {

              reject('失败');

              console.log('失败');

            },

          })

        }

      }

    })

  })

}


util.uploadimag(img1).then(path =>console.log(path)//path为上传的图片名

).catch(console.log('上传成功'));

2.下载

服务器端配置:https://help.aliyun.com/document_detail/31857.html?spm=a2c4g.11186623.6.617.5e07715fJbCAum

由于小程序无法使用oss的sdk,可以配置第三方授权访问的url


function getimg(path) {

  return new Promise((resove, reject) => {

    wx.request({

      method: 'GET',

      url: baseurl + '/user/aliyunSTS/getAliyunGeneratePresignedUrl',//服务器配置获取接口

      data: {

        'path': path

      },

      header: {

        'content-type': 'application/json',

        'jwt': wx.getStorageSync('jwt'),

      },

      success(res) {

        if (parseInt(res.data.status) == 0) {

          resove(res.data.retValue);

        }

      },

      fail: function(err) {

        reject('失败');

      }

    })

  })

}

在其他地方直接调用


 // 获取图片路径,存入字典

  makeimg: function (oldpath) {

    var that = this;

    var dic = that.data.imgdic;

    util.getimg(oldpath).then(newpath => {

      dic[oldpath] = newpath;//返回的图片链接,可直接访问

      that.setData({

        imgdic: dic

      })

    }).catch(console.log('请求完成'));

  },

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容