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)
}
}
想问下这个 fs 怎么安装?
留眼,应该会用上...