小程序结合腾讯云对象存储

首先需要引入腾讯云的sdk:cos-wx-sdk-v5
下载地址:https://gitee.com/azhe2000/publicClassLibrary
小程序上传图片的代码就不贴了//懒
将下面的代码贴在要用的地方

 var cos = new COS({
        getAuthorization: function(params, callback) {
          //获取签名 必填参数
          // 方法二(适用于前端调试)
          var authorization = COS.getAuthorization({
            SecretId: "你的腾讯云存储桶密钥id",
            SecretKey: "你的腾讯云存储桶密钥key",
            // stsToken: that.sign.credentials.sessionToken,
            Method: params.Method,//这里不用理,直接从方法里面出来的
            Key: params.Key
          });
          console.log(params);
          callback(authorization);
        }
      });
      //_tempFilePath 图片路劲  调用微信小程序的上传图片接口获得
      var Key = _tempFilePath.substr(_tempFilePath.lastIndexOf("/") + 1); // 这里指定上传的文件名
      cos.postObject(
        {
          Bucket: '你的腾讯云存储桶名',
          Region: '所属地域',
          Key: Key,
          FilePath: _tempFilePath,
          onProgress: function(info) {
            console.log(JSON.stringify(info));
          }
        },
        function(err, data) {
          if (err && err.error) {
            console.log(err, "iiiiii");
            wx.showModal({
              title: "返回错误",
              content:
                "上传失败:" +
                err.error.Message +
                ";状态码:" +
                err.statusCode,
              showCancel: false
            });
          } else if (err) {
            wx.showModal({
              title: "上传出错",
              content: "上传出错:" + err + ";状态码:" + err.statusCode,
              showCancel: false
            });
          } else {
            wx.showToast({
              title: "上传中...",
              icon: "none",
              duration: 3000
            });
            let url = "https://" + data.Location;
            let _arr =that.data.imageList;
            _arr.push(url);
            that.setData({
                imageList: _arr
             });
            console.log("imgUrl==>"+url);
          }
        }
      );
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值