首先需要引入腾讯云的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);
}
}
);