我放弃了,没有用URL对象,直接把这个对象文件放在云函数的目录里面了,相当于读取的本地文件
云函数中调用fs.readFileSync的问题?我在云函数中调用fs.readFileSync想读取一个在云存储中的文件,但是发现fs.readFileSync好像只能读取在云函数文件夹中的文件,不能读取在云存储中的文件,但是我查询了fs.readFileSync官方文档,里面说路径可以是URL,我尝试了各种方法传入URL,要么提示“TypeError [ERR_INVALID_URL_SCHEME]: The URL must be of scheme file”,或者“Error: ENOENT: no such file or directory, open 'https://7778-wxmltodocx-9gnoh53l4e4377ca-1309024106.tcb.qcloud.la/20220112/74gf2CAw6dBH8Kn4kSGswuQw1UPLYPCR.docx'”,代码如下,请各位高手老师指点!!!感谢!!!! const cloud = require('wx-server-sdk') const { createReport } = require('docx-templates') const fs = require('fs') const path = require('path') const dayjs = require('dayjs') const url = require('url') cloud.init({ env: 'wxmltodocx-9gnoh53l4e4377ca' }) // 云函数入口函数 exports.main = async (event, context) => { console.log(event) //读取本地传入的云函数ID,并将其转换成一个临时的URL const fileList = [event.downloadFileID] const result = await cloud.getTempFileURL({ fileList: fileList, }) console.log('换取临时文件网络地址是:',result.fileList[0].tempFileURL) const myURL = new URL(result.fileList[0].tempFileURL) console.log('转换后的文件网络地址是:',myURL) const myPath = myURL.toString() const template = fs.readFileSync(myPath)//读取文件 执行到这一步就反复出错了。 请各位老师指点。谢谢!
2022-09-28// 云函数入口文件 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, }) } 其实我之前的代码比较复杂,但是一直出错,我干脆就直接照搬照抄了官方文档里的示例,但是依然报错,报错的内容一模一样,我都茫然了
云端调用cloud.uploadFile把文件上传到微信小程序官方云存储时,报TypeError从别的接口接收到base64编码的图像,想在云端以base64作为encoding保存为文件后,读取该文件并上传到云存储,在上传时报告了异常: errCode: -1errMsg: "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); }
2022-01-07