首页 > 解决方案 > 错误:Firebase 的 Cloud Functions 生成 EACCES(ghostscript)

问题描述

我尝试使用 Firebase Cloud Functions 创建 PDF 文件的缩略图。调用 gs 后,出现以下错误:

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

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

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

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

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

在承诺 (/user_code/index.js:95:12)

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

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

使用Spark以外的计划是否需要使用ghostscript之类的组件?

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

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];
  });

});

标签: javascriptgoogle-cloud-functionsghostscript

解决方案


经过数小时的挠头,一遍又一遍地毫无意义地运行相同的代码,我终于找到了问题所在!

您定义的可执行路径不正确。应该是'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


推荐阅读