错误:Firebase的云函数生成EACCES(ghostscript)

时间:2018-06-12 12:07:32

标签: javascript google-cloud-functions ghostscript

我尝试使用Firebase云功能创建PDF文件的缩略图。 在调用gs后,我收到以下错误:

  

2018-06-12T11:29:08.685Z E makeThumbnail:错误:产生EACCES

     

at exports._errnoException(util.js:1020:11)

     

在ChildProcess.spawn(internal / child_process.js:328:11)

     

在exports.spawn(child_process.js:370:9)

     

at Object.exec(/user_code/node_modules/gs/index.js:86:28)

     

在Promise(/user_code/index.js:95:12)

     

at mkdirp.then.then(/user_code/index.js:86:12)

     

2018-06-12T11:29:08.698166767Z D makeThumbnail:函数执行耗时780毫秒,完成状态:'错误'

是否有必要使用像ghostscript这样的组件来使用Spark以外的计划?

另外,我的代码。也许我只是在代码中看不到我的问题

const functions = require('firebase-functions');
const mkdirp = require('mkdirp-promise');
const gcs = require('@google-cloud/storage')();
const admin = require('firebase-admin');

const spawn = require('child-process-promise').spawn;
const path = require('path');
const os = require('os');
const fs = require('fs');
const gs = require('gs');
const THUMB_MAX_HEIGHT = 200;
const THUMB_MAX_WIDTH = 200;
const THUMB_PREFIX = 'thumb_';
const gs_exec_path = path.join(__dirname, './lambda-ghostscript/bin/gs');

try{admin.initializeApp(functions.config().firebase); } catch(e) {}

exports.makeThumbnail = functions.storage.object().onFinalize((object) => {

  const filePath = object.name;
  const contentType = object.contentType; 
  const fileDir = path.dirname(filePath);
  const fileName = path.basename(filePath);
  const thumbFilePath = path.normalize(path.join(fileDir, `${THUMB_PREFIX} ${fileName}`));
  const tempLocalFile = path.join(os.tmpdir(), filePath);
  const tempLocalDir = path.dirname(tempLocalFile);
  const tempLocalThumbFile = path.join(os.tmpdir(), thumbFilePath);

  const tmp_dir = os.tmpdir();


  if (fileName.startsWith(THUMB_PREFIX)) {
    console.log('Is thumbnail');
    return null;
  }


  const bucket = gcs.bucket(object.bucket);
  const file = bucket.file(filePath);
  const thumbFile = bucket.file(thumbFilePath);
  const metadata = {
    contentType: contentType,
  };

  return mkdirp(tmp_dir).then(() => {
     console.log("Dir Created");

    console.log(tempLocalFile);
    return file.download({destination: tempLocalFile});
  }).then(() => {
    console.log("File downloaded");
    if(!contentType.startsWith("image/")){

      return new Promise((resolve, reject) => {
      const pg= 1;
      gs().batch().nopause()
        .option(`-dFirstPage=${pg}`)
        .option(`-dLastPage=${pg}`)
        .executablePath(gs_exec_path)
        .device('png16m')
        .output(tempLocalThumbFile+".png")
        .input(tempLocalFile)
        .exec(err => err ? reject(err) : resolve());
      });
    }
    else
    {
      var args = [ tempLocalFile, '-thumbnail', `${THUMB_MAX_WIDTH}x${THUMB_MAX_HEIGHT}>`, tempLocalThumbFile ];
      return spawn('convert', args, {capture: ['stdout', 'stderr']});
    }
  }).then(() => {

    return bucket.upload(tempLocalThumbFile, { destination: thumbFilePath });
  }).then(() => {

    fs.unlinkSync(tempLocalFile);
    fs.unlinkSync(tempLocalThumbFile);

    return result[0];
  });

});

1 个答案:

答案 0 :(得分:1)

经过数小时的摸索,反复地重复运行相同的代码,我终于找到了问题!

您定义的可执行文件路径不正确。应该是'gs'

这是一个完整的gs()调用示例:

gs()
    .batch()
    .option('-dFirstPage=2')
    .option('-dLastPage=2')
    .nopause()
    .res(90)
    .executablePath('gs')
    .device('jpeg')
    .output(tempNewPath2)
    .input(tempFilePath)
    .exec((err, stdout, stderr) => {
      if (!err) {
        console.log('gs executed w/o error');
        console.log('stdout', stdout);
        console.log('stderr', stderr);
        resolve();
      } else {
        console.log('gs error:', err);
        reject(err);
      }
    });

要获得更多帮助,您可以查看我为该问题创建的样本回购 https://github.com/krharsh17/ghostscript-firebase-sample