云函数下载文件保存到临时文件再读取的时候报错,为什么?
发布于 5 年前 作者 ping62 13436 次浏览 来自 官方Issues
const cloud = require('wx-server-sdk')
const request = require('request')
const fs = require('fs')
const path = require('path')
 
return new Promise(function (resolve, reject) {
    request(options, (error, response, body) => {
      let jsonBody = JSON.parse(body)
      downUrl = jsonBody.download_url
      let p = path.join(__dirname, '/tmp/file.mp4')
      request(downUrl).pipe(fs.createWriteStream(p)).on('close', () => {
        const fileStream = fs.createReadStream(p)
        resolve(cloud.uploadFile({
          cloudPath: 'file.mp4',
          fileContent: fileStream,
        })
        )
 
      })
    })
  })

##报错日志

2019-08-10T03:38:05.684Z  { Error: ENOENT: no such file or directory, open '/var/user/tmp/file.mp4'

errno: -2,

code: 'ENOENT',

syscall: 'open',

path: '/var/user/tmp/file.mp4' }



END RequestId: 430c6bf0-bb20-11e9-9e13-5254007aa7a1

Report RequestId: 430c6bf0-bb20-11e9-9e13-5254007aa7a1 Duration:797ms Memory:256MB MaxMemoryUsed:3.015625MB

1 回复

问题已解决

return new Promise(function (resolve, reject) {
    request(options, (error, response, body) => {
      let jsonBody = JSON.parse(body)
      downUrl = jsonBody.download_url
      const fileName = '/tmp/file.mp4'
      // let p = path.join(__dirname, 'tmp/file.mp4')
      request(downUrl).pipe(fs.createWriteStream(fileName)).on('close', () => {
        const fileStream = fs.createReadStream(fileName)
        resolve(cloud.uploadFile({
          cloudPath: 'file.mp4',
          fileContent: fileStream,
        })
        )
 
      })
    })
  })
回到顶部