const request = require('request-promise')
const config = require('./config');
const fs = require('fs')
module.exports = async (ctx) => {
const files = ctx.request.files //koa2后台接收到文件组,需要npm koa-body且multipart : true
let file = files[0]
try {
let options = {
uri: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + config.appId +'&secret=' + config.appSecret + '',
json:true
}
let {access_token} = await request(options) //得到access_token
let fileName = `example.jpg`
let filePath = `dir/${fileName}`
options = {
method: 'POST',
uri: 'https://api.weixin.qq.com/tcb/uploadfile?access_token=' + access_token + '',
body: {
"env": 'cloud-a8eeXX',
"path": filePath,
},
json: true
}
let res = await request(options) //获得文件上传许可以及各种参数
options = {
method: 'POST',
uri: res.url,
formData: {
"Signature": res.authorization,
"key": filePath,
"x-cos-security-token": res.token,
"x-cos-meta-fileid": res.cos_file_id,
"file": {
value: fs.createReadStream(file.path),
options: {
filename: fileName,
contentType: file.type
}
}
}
}
ctx.body = await request(options) //上传文件,cox.body返回结果给前端
} catch (err) {
console.log(err.stack)
}
}
你好,为什么我把代码copy以后把环境变量换成我的运行不了?