从别的接口接收到base64编码的图像,想在云端以base64作为encoding保存为文件后,读取该文件并上传到云存储,在上传时报告了异常:
- errCode: -1
- errMsg: "uploadFile:fail TypeError: Cannot destructure property `url` of 'undefined' or 'null'.; at uploadFile api; "
相关代码:
const fs = require('fs')
const path = require('path')
const cloud = require('wx-server-sdk')
//文件上传函数
function upload_res_file(options) {
fs.writeFileSync(path.join(__dirname, 'demo.jpg'), options.fileContent, {
encoding: 'base64',
mode: 0o777
});
const file_content = fs.createReadStream(path.join(__dirname, 'demo.jpg'));
cloud.uploadFile({
cloudPath: options.cloudPath + '.jpg',
fileContent: file_content,
}).then(options.success).catch(options.fail);
}
自己把问题解决了,这可能是腾讯接口的一个BUG,cloudPath出现了不支持的字符时,就会出现此问题,改成支持的字符即可。
transTempFileStream(Datastream, dir, id) {
const cloud = require('wx-server-sdk')
const fs = require('fs')
const path = require('path')
// var xlsx = require('node-xlsx');
let fileBase = Datastream[0].fileBase.data;
let name = Datastream[0].name;
let imgList = [''];
let ext = name.match(/\.[^.]+?$/)[0];
let rd = (Math.random() * (999999 - 100000 + 1) | 0) + 100000; //ccminiHelper.genRandomNum(100000, 999999);
fs.writeFileSync(path.join(__dirname, name), fileBase, {
encoding: 'base64',
mode: 0o777
});
const file_content = fs.createReadStream(path.join(__dirname, name));
cloud.uploadFile({
cloudPath: dir + id + '_' + rd + ext,
fileContent: file_content,
}).then(res => {
imgList[0] = res.fileID;
}).catch(error => {
console.log(error)
})
return imgList;
};
// 云函数入口文件 const cloud = require('wx-server-sdk') const { createReport } = require('docx-templates') const fs = require('fs') const path = require('path') // const dayjs = require('dayjs') cloud.init({ env: 'cloud.DYNAMIC_CURRENT_ENV' }) // 云函数入口函数 exports.main = async (event, context) => { const fileStream = fs.createReadStream(path.join(__dirname, 'verify_company.docx')) return await cloud.uploadFile({ cloudPath: 'verify_company.docx', fileContent: fileStream, }) }
其实我之前的代码比较复杂,但是一直出错,我干脆就直接照搬照抄了官方文档里的示例,但是依然报错,报错的内容一模一样,我都茫然了