- 云开发入门
重磅打造的小程序学习路径课,从微信小程序到微信云开发体系化的学习,带来更加顺畅的学习体验。
2021-11-19 - 小程序上传图片到腾讯云对象存储COS的简单代码
const fs = require('fs') const COS = require('cos-nodejs-sdk-v5') const cos = new COS({ SecretId: 'SecretId', SecretKey: 'SecretKey', }) module.exports = async (ctx) => { const image = ctx.request.files.image //这里获得小程序wx.uploadFile上传的文件,文件标识名为image if(!image) return let ext = image.type.split('/')[1] let path = image.path let key = `image-${Date.now()}.${ext}`;//保存在cos的文件名 let TaskId; function p() { return new Promise((resolve, reject) => { cos.putObject({ Bucket: 'Bucket-1251490133', /* 必须 */ Region: 'cn-north', Key: key, /* 必须 */ Body: fs.createReadStream(path), ContentLength: fs.statSync(path).size }, function (err, data) { console.log(err || data); if (err) { reject(); } else { resolve("url/" + key); } fs.unlinkSync(path); }); }); } try{ ctx.body = await p() }catch (err){ console.log(err) } } [图片]
2020-10-20